Merge pull request #5198 from BrzVlad/fix-binprot-stats
[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 MOBILE
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 #pragma warning disable 618
73                                 ConfigurationSettings.GetConfig ("system.net/authenticationModules");
74 #pragma warning restore 618
75 #endif
76                         }
77                 }
78                 
79                 static ICredentialPolicy credential_policy = null;
80                 
81                 public static ICredentialPolicy CredentialPolicy
82                 {
83                         get {
84                                 return(credential_policy);
85                                                         }
86                         set {
87                                 credential_policy = value;
88                         }
89                 }
90                 
91                 static Exception GetMustImplement ()
92                 {
93                         return new NotImplementedException ();
94                 }
95                 
96                 [MonoTODO]
97                 public static StringDictionary CustomTargetNameDictionary
98                 {
99                         get {
100                                 throw GetMustImplement ();
101                         }
102                 }
103
104                 public static IEnumerator RegisteredModules {
105                         get {
106                                 EnsureModules ();
107                                 return modules.GetEnumerator ();
108                         }
109                 }
110
111                 [MonoTODO]
112                 internal static bool OSSupportsExtendedProtection {
113                         get {
114                                 return false;
115                         }
116                 }
117
118                 internal static void Clear ()
119                 {
120                         EnsureModules ();
121                         lock (modules)
122                                 modules.Clear ();
123                 }
124                 
125                 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
126                 {
127                         if (request == null)
128                                 throw new ArgumentNullException ("request");
129
130                         if (credentials == null)
131                                 throw new ArgumentNullException ("credentials");
132
133                         if (challenge == null)
134                                 throw new ArgumentNullException ("challenge");
135
136                         return DoAuthenticate (challenge, request, credentials);
137                 }
138
139                 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
140                 {
141                         EnsureModules ();
142                         lock (modules) {
143                                 foreach (IAuthenticationModule mod in modules) {
144                                         Authorization auth = mod.Authenticate (challenge, request, credentials);
145                                         if (auth == null)
146                                                 continue;
147
148                                         auth.ModuleAuthenticationType = mod.AuthenticationType;
149                                         return auth;
150                                 }
151                         }
152
153                         return null;
154                 }
155
156                 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
157                 {
158                         if (request == null)
159                                 throw new ArgumentNullException ("request");
160
161                         if (credentials == null)
162                                 return null;
163
164                         EnsureModules ();
165                         lock (modules) {
166                                 foreach (IAuthenticationModule mod in modules) {
167                                         Authorization auth = mod.PreAuthenticate (request, credentials);
168                                         if (auth == null)
169                                                 continue;
170
171                                         auth.ModuleAuthenticationType = mod.AuthenticationType;
172                                         return auth;
173                                 }
174                         }
175
176                         return null;
177                 }
178
179                 public static void Register (IAuthenticationModule authenticationModule)
180                 {
181                         if (authenticationModule == null)
182                                 throw new ArgumentNullException ("authenticationModule");
183
184                         DoUnregister (authenticationModule.AuthenticationType, false);
185                         lock (modules)
186                                 modules.Add (authenticationModule);
187                 }
188
189                 public static void Unregister (IAuthenticationModule authenticationModule)
190                 {
191                         if (authenticationModule == null)
192                                 throw new ArgumentNullException ("authenticationModule");
193
194                         DoUnregister (authenticationModule.AuthenticationType, true);
195                 }
196
197                 public static void Unregister (string authenticationScheme)
198                 {
199                         if (authenticationScheme == null)
200                                 throw new ArgumentNullException ("authenticationScheme");
201                         
202                         DoUnregister (authenticationScheme, true);
203                 }
204
205                 static void DoUnregister (string authenticationScheme, bool throwEx)
206                 {
207                         EnsureModules ();
208                         lock (modules) {
209                                 IAuthenticationModule module = null;
210                                 foreach (IAuthenticationModule mod in modules) {
211                                         string modtype = mod.AuthenticationType;
212                                         if (String.Compare (modtype, authenticationScheme, true) == 0) {
213                                                 module = mod;
214                                                 break;
215                                         }
216                                 }
217
218                                 if (module == null) {
219                                         if (throwEx)
220                                                 throw new InvalidOperationException ("Scheme not registered.");
221                                 } else {
222                                         modules.Remove (module);
223                                 }
224                         }
225                 }
226         }
227 }
228