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