Merge pull request #4433 from kumpera/android-fixes
[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                                 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                 [MonoTODO]
110                 internal static bool OSSupportsExtendedProtection {
111                         get {
112                                 return false;
113                         }
114                 }
115
116                 internal static void Clear ()
117                 {
118                         EnsureModules ();
119                         lock (modules)
120                                 modules.Clear ();
121                 }
122                 
123                 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
124                 {
125                         if (request == null)
126                                 throw new ArgumentNullException ("request");
127
128                         if (credentials == null)
129                                 throw new ArgumentNullException ("credentials");
130
131                         if (challenge == null)
132                                 throw new ArgumentNullException ("challenge");
133
134                         return DoAuthenticate (challenge, request, credentials);
135                 }
136
137                 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
138                 {
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;
145
146                                         auth.ModuleAuthenticationType = mod.AuthenticationType;
147                                         return auth;
148                                 }
149                         }
150
151                         return null;
152                 }
153
154                 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
155                 {
156                         if (request == null)
157                                 throw new ArgumentNullException ("request");
158
159                         if (credentials == null)
160                                 return null;
161
162                         EnsureModules ();
163                         lock (modules) {
164                                 foreach (IAuthenticationModule mod in modules) {
165                                         Authorization auth = mod.PreAuthenticate (request, credentials);
166                                         if (auth == null)
167                                                 continue;
168
169                                         auth.ModuleAuthenticationType = mod.AuthenticationType;
170                                         return auth;
171                                 }
172                         }
173
174                         return null;
175                 }
176
177                 public static void Register (IAuthenticationModule authenticationModule)
178                 {
179                         if (authenticationModule == null)
180                                 throw new ArgumentNullException ("authenticationModule");
181
182                         DoUnregister (authenticationModule.AuthenticationType, false);
183                         lock (modules)
184                                 modules.Add (authenticationModule);
185                 }
186
187                 public static void Unregister (IAuthenticationModule authenticationModule)
188                 {
189                         if (authenticationModule == null)
190                                 throw new ArgumentNullException ("authenticationModule");
191
192                         DoUnregister (authenticationModule.AuthenticationType, true);
193                 }
194
195                 public static void Unregister (string authenticationScheme)
196                 {
197                         if (authenticationScheme == null)
198                                 throw new ArgumentNullException ("authenticationScheme");
199                         
200                         DoUnregister (authenticationScheme, true);
201                 }
202
203                 static void DoUnregister (string authenticationScheme, bool throwEx)
204                 {
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;
213                                         }
214                                 }
215
216                                 if (module == null) {
217                                         if (throwEx)
218                                                 throw new InvalidOperationException ("Scheme not registered.");
219                                 } else {
220                                         modules.Remove (module);
221                                 }
222                         }
223                 }
224         }
225 }
226