use a 2.0 network stack for MONOTOUCH
[mcs.git] / class / System / System.Net / AuthenticationManager.cs
blob5afc54bcaceef7cc5067eff610f6e56140620eee
1 //
2 // System.Net.AuthenticationManager.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Configuration;
34 #if NET_2_0
35 using System.Net.Configuration;
36 using System.Collections.Specialized;
37 #endif
39 namespace System.Net
41 public class AuthenticationManager
43 static ArrayList modules;
44 static object locker = new object ();
46 private AuthenticationManager ()
50 static void EnsureModules ()
52 lock (locker) {
53 if (modules != null)
54 return;
56 modules = new ArrayList ();
57 #if !MONOTOUCH
58 #if NET_2_0 && CONFIGURATION_DEP
59 object cfg = ConfigurationManager.GetSection ("system.net/authenticationModules");
60 AuthenticationModulesSection s = cfg as AuthenticationModulesSection;
61 if (s != null) {
62 foreach (AuthenticationModuleElement element in s.AuthenticationModules) {
63 IAuthenticationModule module = null;
64 try {
65 Type type = Type.GetType (element.Type, true);
66 module = (IAuthenticationModule) Activator.CreateInstance (type);
67 } catch {}
68 modules.Add (module);
71 #else
72 ConfigurationSettings.GetConfig ("system.net/authenticationModules");
73 #endif
74 #endif
78 #if NET_2_0
79 static ICredentialPolicy credential_policy = null;
81 public static ICredentialPolicy CredentialPolicy
83 get {
84 return(credential_policy);
86 set {
87 credential_policy = value;
91 static Exception GetMustImplement ()
93 return new NotImplementedException ();
96 [MonoTODO]
97 public static StringDictionary CustomTargetNameDictionary
99 get {
100 throw GetMustImplement ();
103 #endif
105 public static IEnumerator RegisteredModules {
106 get {
107 EnsureModules ();
108 return modules.GetEnumerator ();
112 internal static void Clear ()
114 EnsureModules ();
115 lock (modules)
116 modules.Clear ();
119 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
121 if (request == null)
122 throw new ArgumentNullException ("request");
124 if (credentials == null)
125 throw new ArgumentNullException ("credentials");
127 if (challenge == null)
128 throw new ArgumentNullException ("challenge");
130 return DoAuthenticate (challenge, request, credentials);
133 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
135 EnsureModules ();
136 lock (modules) {
137 foreach (IAuthenticationModule mod in modules) {
138 Authorization auth = mod.Authenticate (challenge, request, credentials);
139 if (auth == null)
140 continue;
142 auth.Module = mod;
143 return auth;
147 return null;
150 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
152 if (request == null)
153 throw new ArgumentNullException ("request");
155 if (credentials == null)
156 return null;
158 EnsureModules ();
159 lock (modules) {
160 foreach (IAuthenticationModule mod in modules) {
161 Authorization auth = mod.PreAuthenticate (request, credentials);
162 if (auth == null)
163 continue;
165 auth.Module = mod;
166 return auth;
170 return null;
173 public static void Register (IAuthenticationModule authenticationModule)
175 if (authenticationModule == null)
176 throw new ArgumentNullException ("authenticationModule");
178 DoUnregister (authenticationModule.AuthenticationType, false);
179 lock (modules)
180 modules.Add (authenticationModule);
183 public static void Unregister (IAuthenticationModule authenticationModule)
185 if (authenticationModule == null)
186 throw new ArgumentNullException ("authenticationModule");
188 DoUnregister (authenticationModule.AuthenticationType, true);
191 public static void Unregister (string authenticationScheme)
193 if (authenticationScheme == null)
194 throw new ArgumentNullException ("authenticationScheme");
196 DoUnregister (authenticationScheme, true);
199 static void DoUnregister (string authenticationScheme, bool throwEx)
201 EnsureModules ();
202 lock (modules) {
203 IAuthenticationModule module = null;
204 foreach (IAuthenticationModule mod in modules) {
205 string modtype = mod.AuthenticationType;
206 if (String.Compare (modtype, authenticationScheme, true) == 0) {
207 module = mod;
208 break;
212 if (module == null) {
213 if (throwEx)
214 throw new InvalidOperationException ("Scheme not registered.");
215 } else {
216 modules.Remove (module);