Add TimeZoneInfo Serialization
[mono.git] / mcs / class / System / Mono.Http / NtlmClient.cs
1 //
2 // Mono.Http.NtlmClient
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2003 Novell, Inc. (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if SECURITY_DEP
32
33 extern alias MonoSecurity;
34
35 using System;
36 using System.Collections;
37 using System.Net;
38 using MonoSecurity::Mono.Security.Protocol.Ntlm;
39
40 namespace Mono.Http
41 {
42         class NtlmSession
43         {
44                 MessageBase message;
45
46                 public NtlmSession () 
47                 {
48                 }
49
50                 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
51                 {
52                         HttpWebRequest request = webRequest as HttpWebRequest;
53                         if (request == null)
54                                 return null;
55         
56                         NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
57                         if (cred == null)
58                                 return null;
59
60                         string userName = cred.UserName;
61                         string domain = cred.Domain;
62                         string password = cred.Password;
63                         if (userName == null || userName == "")
64                                 return null;
65
66                         if (String.IsNullOrEmpty (domain)) {
67                                 int idx = userName.IndexOf ('\\');
68                                 if (idx == -1) {
69                                         idx = userName.IndexOf ('/');
70                                 }
71                                 if (idx >= 0) {
72                                         domain = userName.Substring (0, idx);
73                                         userName = userName.Substring (idx + 1);
74                                 }
75                         }
76
77                         bool completed = false;
78                         if (message == null) {
79                                 Type1Message type1 = new Type1Message ();
80                                 type1.Domain = domain;
81                                 type1.Host = ""; // MS does not send it
82                                 type1.Flags |= NtlmFlags.NegotiateNtlm2Key;
83                                 message = type1;
84                         } else if (message.Type == 1) {
85                                 // Should I check the credentials?
86                                 if (challenge == null) {
87                                         message = null;
88                                         return null;
89                                 }
90
91                                 Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
92                                 if (password == null)
93                                         password = "";
94
95                                 Type3Message type3 = new Type3Message (type2);
96                                 type3.Username = userName;
97                                 type3.Password = password;
98                                 type3.Domain = domain;
99                                 message = type3;
100                                 completed = true;
101                         } else {
102                                 // Should I check the credentials?
103                                 // type must be 3 here
104                                 if (challenge == null || challenge == String.Empty) {
105                                         Type1Message type1 = new Type1Message ();
106                                         type1.Domain = domain;
107                                         type1.Host = ""; // MS does not send it
108                                         message = type1;
109                                 } else {
110                                         completed = true;
111                                 }
112                         }
113                         
114                         string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
115                         return new Authorization (token, completed);
116                 }
117         }
118
119         class NtlmClient : IAuthenticationModule
120         {
121                 static Hashtable cache;
122
123                 static NtlmClient () 
124                 {
125                         cache = new Hashtable ();
126                 }
127         
128                 public NtlmClient () {}
129         
130                 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
131                 {
132                         if (credentials == null || challenge == null)
133                                 return null;
134         
135                         string header = challenge.Trim ();
136                         int idx = header.ToLower ().IndexOf ("ntlm");
137                         if (idx == -1)
138                                 return null;
139
140                         idx = header.IndexOfAny (new char [] {' ', '\t'});
141                         if (idx != -1) {
142                                 header = header.Substring (idx).Trim ();
143                         } else {
144                                 header = null;
145                         }
146
147                         HttpWebRequest request = webRequest as HttpWebRequest;
148                         if (request == null)
149                                 return null;
150
151                         lock (cache) {
152                                 NtlmSession ds = (NtlmSession) cache [request];
153                                 if (ds == null) {
154                                         ds = new NtlmSession ();
155                                         cache.Add (request, ds);
156                                 }
157
158                                 return ds.Authenticate (header, webRequest, credentials);
159                         }
160                 }
161
162                 public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials) 
163                 {
164                         return null;
165                 }
166         
167                 public string AuthenticationType { 
168                         get { return "NTLM"; }
169                 }
170         
171                 public bool CanPreAuthenticate { 
172                         get { return false; }
173                 }
174         }
175 }
176 #endif