* ToolTask.cs (ProcessOuputTool): Move logging of tool
[mono.git] / mcs / class / System.Web / System.Web.Configuration / AuthenticationConfigHandler.cs
1 //
2 // System.Web.Configuration.AuthenticationSectionHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Configuration;
34 using System.Xml;
35
36 namespace System.Web.Configuration
37 {
38         class AuthenticationConfigHandler : IConfigurationSectionHandler
39         {
40                 public object Create (object parent, object context, XmlNode section)
41                 {
42                         AuthConfig config = new AuthConfig (parent);
43
44                         string mode = AttValue ("mode", section);
45                         if (mode != null)
46                                 config.SetMode (mode);
47                         
48                         if (section.Attributes != null && section.Attributes.Count != 0)
49                                 ThrowException ("Unrecognized attribute", section);
50
51                         XmlNodeList authNodes = section.ChildNodes;
52                         foreach (XmlNode child in authNodes) {
53                                 XmlNodeType ntype = child.NodeType;
54                                 if (ntype != XmlNodeType.Element)
55                                         continue;
56                                 
57                                 if (child.Name == "forms") {
58                                         config.CookieName = AttValue ("name", child);
59                                         config.CookiePath = AttValue ("path", child);
60                                         config.LoginUrl = AttValue ("loginUrl", child);
61                                         config.SetProtection (AttValue ("protection", child));
62                                         config.SetTimeout (AttValue ("timeout", child));
63 #if NET_1_1
64                                         string att = AttValue ("requireSSL", child);
65                                         if (att != null) {
66                                                 if (att == "true") {
67                                                         config.RequireSSL = true;
68                                                 } else if (att == "false") {
69                                                         config.RequireSSL = false;
70                                                 } else {
71                                                         HandlersUtil.ThrowException
72                                                                 ("Invalid value for RequireSSL", child);
73                                                 }
74                                         }
75
76                                         att = AttValue ("slidingExpiration", child);
77                                         if (att != null) {
78                                                 if (att == "true") {
79                                                         config.SlidingExpiration = true;
80                                                 } else if (att == "false") {
81                                                         config.SlidingExpiration = false;
82                                                 } else {
83                                                         HandlersUtil.ThrowException
84                                                                 ("Invalid value for SlidingExpiration", child);
85                                                 }
86                                         }
87 #endif
88
89                                         ReadCredentials (child.ChildNodes, config);
90                                         continue;
91                                 }
92
93                                 if (child.Name == "passport") {
94                                         continue;
95                                 }
96
97                                 HandlersUtil.ThrowException ("Unexpected element", child);
98                         }
99
100                         return config;
101                 }
102
103                 static void ReadCredentials (XmlNodeList nodes, AuthConfig config)
104                 {
105                         foreach (XmlNode child in nodes) {
106                                 XmlNodeType ntype = child.NodeType;
107                                 if (ntype != XmlNodeType.Element)
108                                         continue;
109
110                                 if (child.Name != "credentials")
111                                         HandlersUtil.ThrowException ("Unexpected element", child);
112
113                                 config.SetPasswordFormat (AttValue ("passwordFormat", child));
114                                 ReadUsers (child.ChildNodes, config.CredentialUsers);
115                         }
116                 }
117
118                 static void ReadUsers (XmlNodeList nodes, Hashtable users)
119                 {
120                         foreach (XmlNode child in nodes) {
121                                 XmlNodeType ntype = child.NodeType;
122                                 if (ntype != XmlNodeType.Element)
123                                         continue;
124
125                                 if (child.Name != "user")
126                                         HandlersUtil.ThrowException ("Unexpected element", child);
127
128                                 string name = AttValue ("name", child, false);
129                                 string password = AttValue ("password", child);
130                                 if (users.ContainsKey (name))
131                                         ThrowException ("User '" + name + "' already added.", child);
132
133                                 users [name] = password;
134                                 if (child.HasChildNodes)
135                                         ThrowException ("Child nodes not allowed here", child.FirstChild);
136                         }
137                 }
138                 // A few methods to save some typing
139                 static string AttValue (string name, XmlNode node, bool optional)
140                 {
141                         return HandlersUtil.ExtractAttributeValue (name, node, optional);
142                 }
143
144                 static string AttValue (string name, XmlNode node)
145                 {
146                         return HandlersUtil.ExtractAttributeValue (name, node, true);
147                 }
148
149                 static void ThrowException (string message, XmlNode node)
150                 {
151                         HandlersUtil.ThrowException (message, node);
152                 }
153                 //
154
155         }
156 }
157