Fix for rare mono runtime crash during shutdown sequence on Windows.
[mono.git] / mcs / class / System / System.Net / AuthenticationManager.cs
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 //
10
11 //
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.
30 //
31
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Configuration;
35 using System.Net.Configuration;
36
37 namespace System.Net
38 {
39         public class AuthenticationManager {
40                 static ArrayList modules;
41                 static object locker = new object ();
42
43                 private AuthenticationManager ()
44                 {
45                 }
46
47                 static void EnsureModules ()
48                 {
49                         lock (locker) {
50                                 if (modules != null)
51                                         return;
52                                 
53                                 modules = new ArrayList ();
54 #if NET_2_1
55                                 modules.Add (new NtlmClient ());
56                                 modules.Add (new DigestClient ());
57                                 modules.Add (new BasicClient ());
58 #elif 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);
69                                         }
70                                 }
71 #else
72                                 ConfigurationSettings.GetConfig ("system.net/authenticationModules");
73 #endif
74                         }
75                 }
76                 
77                 static ICredentialPolicy credential_policy = null;
78                 
79                 public static ICredentialPolicy CredentialPolicy
80                 {
81                         get {
82                                 return(credential_policy);
83                                                         }
84                         set {
85                                 credential_policy = value;
86                         }
87                 }
88                 
89                 static Exception GetMustImplement ()
90                 {
91                         return new NotImplementedException ();
92                 }
93                 
94                 [MonoTODO]
95                 public static StringDictionary CustomTargetNameDictionary
96                 {
97                         get {
98                                 throw GetMustImplement ();
99                         }
100                 }
101
102                 public static IEnumerator RegisteredModules {
103                         get {
104                                 EnsureModules ();
105                                 return modules.GetEnumerator ();
106                         }
107                 }
108
109                 internal static void Clear ()
110                 {
111                         EnsureModules ();
112                         lock (modules)
113                                 modules.Clear ();
114                 }
115                 
116                 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
117                 {
118                         if (request == null)
119                                 throw new ArgumentNullException ("request");
120
121                         if (credentials == null)
122                                 throw new ArgumentNullException ("credentials");
123
124                         if (challenge == null)
125                                 throw new ArgumentNullException ("challenge");
126
127                         return DoAuthenticate (challenge, request, credentials);
128                 }
129
130                 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
131                 {
132                         EnsureModules ();
133                         lock (modules) {
134                                 foreach (IAuthenticationModule mod in modules) {
135                                         Authorization auth = mod.Authenticate (challenge, request, credentials);
136                                         if (auth == null)
137                                                 continue;
138
139                                         auth.Module = mod;
140                                         return auth;
141                                 }
142                         }
143
144                         return null;
145                 }
146
147                 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
148                 {
149                         if (request == null)
150                                 throw new ArgumentNullException ("request");
151
152                         if (credentials == null)
153                                 return null;
154
155                         EnsureModules ();
156                         lock (modules) {
157                                 foreach (IAuthenticationModule mod in modules) {
158                                         Authorization auth = mod.PreAuthenticate (request, credentials);
159                                         if (auth == null)
160                                                 continue;
161
162                                         auth.Module = mod;
163                                         return auth;
164                                 }
165                         }
166
167                         return null;
168                 }
169
170                 public static void Register (IAuthenticationModule authenticationModule)
171                 {
172                         if (authenticationModule == null)
173                                 throw new ArgumentNullException ("authenticationModule");
174
175                         DoUnregister (authenticationModule.AuthenticationType, false);
176                         lock (modules)
177                                 modules.Add (authenticationModule);
178                 }
179
180                 public static void Unregister (IAuthenticationModule authenticationModule)
181                 {
182                         if (authenticationModule == null)
183                                 throw new ArgumentNullException ("authenticationModule");
184
185                         DoUnregister (authenticationModule.AuthenticationType, true);
186                 }
187
188                 public static void Unregister (string authenticationScheme)
189                 {
190                         if (authenticationScheme == null)
191                                 throw new ArgumentNullException ("authenticationScheme");
192                         
193                         DoUnregister (authenticationScheme, true);
194                 }
195
196                 static void DoUnregister (string authenticationScheme, bool throwEx)
197                 {
198                         EnsureModules ();
199                         lock (modules) {
200                                 IAuthenticationModule module = null;
201                                 foreach (IAuthenticationModule mod in modules) {
202                                         string modtype = mod.AuthenticationType;
203                                         if (String.Compare (modtype, authenticationScheme, true) == 0) {
204                                                 module = mod;
205                                                 break;
206                                         }
207                                 }
208
209                                 if (module == null) {
210                                         if (throwEx)
211                                                 throw new InvalidOperationException ("Scheme not registered.");
212                                 } else {
213                                         modules.Remove (module);
214                                 }
215                         }
216                 }
217         }
218 }
219