hard code the authentication modules for monotouch
[mcs.git] / class / System / System.Net / AuthenticationManager.cs
blob798448f1e6252703b9d7569bacf14e6c2c2f300a
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 modules.Add (new BasicClient ());
59 modules.Add (new DigestClient ());
60 modules.Add (new NtlmClient ());
61 #else
62 #if NET_2_0 && CONFIGURATION_DEP
63 object cfg = ConfigurationManager.GetSection ("system.net/authenticationModules");
64 AuthenticationModulesSection s = cfg as AuthenticationModulesSection;
65 if (s != null) {
66 foreach (AuthenticationModuleElement element in s.AuthenticationModules) {
67 IAuthenticationModule module = null;
68 try {
69 Type type = Type.GetType (element.Type, true);
70 module = (IAuthenticationModule) Activator.CreateInstance (type);
71 } catch {}
72 modules.Add (module);
75 #else
76 ConfigurationSettings.GetConfig ("system.net/authenticationModules");
77 #endif
78 #endif
82 #if NET_2_0
83 static ICredentialPolicy credential_policy = null;
85 public static ICredentialPolicy CredentialPolicy
87 get {
88 return(credential_policy);
90 set {
91 credential_policy = value;
95 static Exception GetMustImplement ()
97 return new NotImplementedException ();
100 [MonoTODO]
101 public static StringDictionary CustomTargetNameDictionary
103 get {
104 throw GetMustImplement ();
107 #endif
109 public static IEnumerator RegisteredModules {
110 get {
111 EnsureModules ();
112 return modules.GetEnumerator ();
116 internal static void Clear ()
118 EnsureModules ();
119 lock (modules)
120 modules.Clear ();
123 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
125 if (request == null)
126 throw new ArgumentNullException ("request");
128 if (credentials == null)
129 throw new ArgumentNullException ("credentials");
131 if (challenge == null)
132 throw new ArgumentNullException ("challenge");
134 return DoAuthenticate (challenge, request, credentials);
137 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
139 EnsureModules ();
140 lock (modules) {
141 foreach (IAuthenticationModule mod in modules) {
142 Authorization auth = mod.Authenticate (challenge, request, credentials);
143 if (auth == null)
144 continue;
146 auth.Module = mod;
147 return auth;
151 return null;
154 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
156 if (request == null)
157 throw new ArgumentNullException ("request");
159 if (credentials == null)
160 return null;
162 EnsureModules ();
163 lock (modules) {
164 foreach (IAuthenticationModule mod in modules) {
165 Authorization auth = mod.PreAuthenticate (request, credentials);
166 if (auth == null)
167 continue;
169 auth.Module = mod;
170 return auth;
174 return null;
177 public static void Register (IAuthenticationModule authenticationModule)
179 if (authenticationModule == null)
180 throw new ArgumentNullException ("authenticationModule");
182 DoUnregister (authenticationModule.AuthenticationType, false);
183 lock (modules)
184 modules.Add (authenticationModule);
187 public static void Unregister (IAuthenticationModule authenticationModule)
189 if (authenticationModule == null)
190 throw new ArgumentNullException ("authenticationModule");
192 DoUnregister (authenticationModule.AuthenticationType, true);
195 public static void Unregister (string authenticationScheme)
197 if (authenticationScheme == null)
198 throw new ArgumentNullException ("authenticationScheme");
200 DoUnregister (authenticationScheme, true);
203 static void DoUnregister (string authenticationScheme, bool throwEx)
205 EnsureModules ();
206 lock (modules) {
207 IAuthenticationModule module = null;
208 foreach (IAuthenticationModule mod in modules) {
209 string modtype = mod.AuthenticationType;
210 if (String.Compare (modtype, authenticationScheme, true) == 0) {
211 module = mod;
212 break;
216 if (module == null) {
217 if (throwEx)
218 throw new InvalidOperationException ("Scheme not registered.");
219 } else {
220 modules.Remove (module);