In corlib/System.Runtime.InteropServices:
[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.Configuration;
34 #if NET_2_0
35 using System.Net.Configuration;
36 #endif
37
38 namespace System.Net
39 {
40         public class AuthenticationManager
41         {
42                 static ArrayList modules;
43                 static object locker = new object ();
44
45                 private AuthenticationManager ()
46                 {
47                 }
48
49                 static void EnsureModules ()
50                 {
51                         lock (locker) {
52                                 if (modules != null)
53                                         return;
54                                 
55                                 modules = new ArrayList ();
56 #if NET_2_0 && CONFIGURATION_DEP
57                                 object cfg = ConfigurationManager.GetSection ("system.net/authenticationModules");
58                                 AuthenticationModulesSection s = cfg as AuthenticationModulesSection;
59                                 if (s != null) {
60                                         foreach (AuthenticationModuleElement element in s.AuthenticationModules) {
61                                                 IAuthenticationModule module = null;
62                                                 try {
63                                                         Type type = Type.GetType (element.Type, true);
64                                                         module = (IAuthenticationModule) Activator.CreateInstance (type);
65                                                 } catch {}
66                                                 modules.Add (module);
67                                         }
68                                 }
69 #else
70                                 ConfigurationSettings.GetConfig ("system.net/authenticationModules");
71 #endif
72                         }
73                 }
74                 
75                 public static IEnumerator RegisteredModules {
76                         get {
77                                 EnsureModules ();
78                                 return modules.GetEnumerator ();
79                         }
80                 }
81
82                 internal static void Clear ()
83                 {
84                         EnsureModules ();
85                         lock (modules)
86                                 modules.Clear ();
87                 }
88                 
89                 public static Authorization Authenticate (string challenge, WebRequest request, ICredentials credentials)
90                 {
91                         if (request == null)
92                                 throw new ArgumentNullException ("request");
93
94                         if (credentials == null)
95                                 throw new ArgumentNullException ("credentials");
96
97                         if (challenge == null)
98                                 throw new ArgumentNullException ("challenge");
99
100                         return DoAuthenticate (challenge, request, credentials);
101                 }
102
103                 static Authorization DoAuthenticate (string challenge, WebRequest request, ICredentials credentials)
104                 {
105                         EnsureModules ();
106                         lock (modules) {
107                                 foreach (IAuthenticationModule mod in modules) {
108                                         Authorization auth = mod.Authenticate (challenge, request, credentials);
109                                         if (auth == null)
110                                                 continue;
111
112                                         auth.Module = mod;
113                                         return auth;
114                                 }
115                         }
116
117                         return null;
118                 }
119
120                 public static Authorization PreAuthenticate (WebRequest request, ICredentials credentials)
121                 {
122                         if (request == null)
123                                 throw new ArgumentNullException ("request");
124
125                         if (credentials == null)
126                                 return null;
127
128                         EnsureModules ();
129                         lock (modules) {
130                                 foreach (IAuthenticationModule mod in modules) {
131                                         Authorization auth = mod.PreAuthenticate (request, credentials);
132                                         if (auth == null)
133                                                 continue;
134
135                                         auth.Module = mod;
136                                         return auth;
137                                 }
138                         }
139
140                         return null;
141                 }
142
143                 public static void Register (IAuthenticationModule authenticationModule)
144                 {
145                         if (authenticationModule == null)
146                                 throw new ArgumentNullException ("authenticationModule");
147
148                         DoUnregister (authenticationModule.AuthenticationType, false);
149                         lock (modules)
150                                 modules.Add (authenticationModule);
151                 }
152
153                 public static void Unregister (IAuthenticationModule authenticationModule)
154                 {
155                         if (authenticationModule == null)
156                                 throw new ArgumentNullException ("authenticationModule");
157
158                         DoUnregister (authenticationModule.AuthenticationType, true);
159                 }
160
161                 public static void Unregister (string authenticationScheme)
162                 {
163                         if (authenticationScheme == null)
164                                 throw new ArgumentNullException ("authenticationScheme");
165                         
166                         DoUnregister (authenticationScheme, true);
167                 }
168
169                 static void DoUnregister (string authenticationScheme, bool throwEx)
170                 {
171                         EnsureModules ();
172                         lock (modules) {
173                                 IAuthenticationModule module = null;
174                                 foreach (IAuthenticationModule mod in modules) {
175                                         string modtype = mod.AuthenticationType;
176                                         if (String.Compare (modtype, authenticationScheme, true) == 0) {
177                                                 module = mod;
178                                                 break;
179                                         }
180                                 }
181
182                                 if (module == null) {
183                                         if (throwEx)
184                                                 throw new InvalidOperationException ("Scheme not registered.");
185                                 } else {
186                                         modules.Remove (module);
187                                 }
188                         }
189                 }
190         }
191 }
192