**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Http / Mono.Http / NtlmClient.cs
blobf81488968f0e4abfd6071332726aad0759b726fc
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 //
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.
31 using System;
32 using System.Collections;
33 using System.Net;
34 using Mono.Security.Protocol.Ntlm;
36 namespace Mono.Http
38 class NtlmSession
40 MessageBase message;
42 public NtlmSession ()
46 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
48 HttpWebRequest request = webRequest as HttpWebRequest;
49 if (request == null)
50 return null;
52 NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
53 string userName = cred.UserName;
54 string domain = cred.Domain;
55 string password = cred.Password;
56 if (userName == null || userName == "" || domain == null || domain == "")
57 return null;
59 bool completed = false;
60 if (message == null) {
61 Type1Message type1 = new Type1Message ();
62 type1.Domain = domain;
63 message = type1;
64 } else if (message.Type == 1) {
65 // Should I check the credentials?
66 if (challenge == null) {
67 message = null;
68 return null;
71 Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
72 if (password == null)
73 password = "";
75 Type3Message type3 = new Type3Message ();
76 type3.Domain = domain;
77 type3.Username = userName;
78 type3.Challenge = type2.Nonce;
79 type3.Password = password;
80 message = type3;
81 completed = true;
82 } else {
83 // Should I check the credentials?
84 // type must be 3 here
85 completed = true;
88 string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
89 return new Authorization (token, completed);
93 public class NtlmClient : IAuthenticationModule
95 static Hashtable cache;
97 static NtlmClient ()
99 cache = new Hashtable ();
102 public NtlmClient () {}
104 public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials)
106 if (credentials == null || challenge == null)
107 return null;
109 string header = challenge.Trim ();
110 int idx = header.ToLower ().IndexOf ("ntlm");
111 if (idx == -1)
112 return null;
114 idx = header.IndexOfAny (new char [] {' ', '\t'});
115 if (idx != -1) {
116 header = header.Substring (idx).Trim ();
117 } else {
118 header = null;
121 HttpWebRequest request = webRequest as HttpWebRequest;
122 if (request == null)
123 return null;
125 lock (cache) {
126 NtlmSession ds = (NtlmSession) cache [request.RequestUri];
127 if (ds == null) {
128 ds = new NtlmSession ();
129 cache.Add (request.RequestUri, ds);
132 return ds.Authenticate (header, webRequest, credentials);
136 public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials)
138 return null;
141 public string AuthenticationType {
142 get { return "NTLM"; }
145 public bool CanPreAuthenticate {
146 get { return false; }