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