2009-08-26 Gonzalo Paniagua Javier <gonzalo@novell.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Wed, 26 Aug 2009 06:53:55 +0000 (06:53 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Wed, 26 Aug 2009 06:53:55 +0000 (06:53 -0000)
* System.dll.sources:
* monotouch_System.dll.sources:
* System20.csproj: add Mono.Http/NtmlClient.cs

* Mono.Http/ChangeLog: New file.
* Mono.Http/NtlmClient.cs: copied from Mono.Http.
* System.Net/NtlmClient.cs: use Mono.Http.NtmlClient from System
instead of dynamically loading the one in Mono.Http.

svn path=/trunk/mcs/; revision=140690

mcs/class/System/ChangeLog
mcs/class/System/Mono.Http/ChangeLog [new file with mode: 0644]
mcs/class/System/Mono.Http/NtlmClient.cs [new file with mode: 0644]
mcs/class/System/System.Net/NtlmClient.cs
mcs/class/System/System.dll.sources
mcs/class/System/System20.csproj
mcs/class/System/monotouch_System.dll.sources

index b0efa6bd38495db995b42b66d1b144bd085f9f0a..6cf3cb89a82c865be8134523ac5b2d98c3b085b1 100644 (file)
@@ -1,3 +1,14 @@
+2009-08-26 Gonzalo Paniagua Javier <gonzalo@novell.com>
+
+       * System.dll.sources:
+       * monotouch_System.dll.sources:
+       * System20.csproj: add Mono.Http/NtmlClient.cs
+
+       * Mono.Http/ChangeLog: New file.
+       * Mono.Http/NtlmClient.cs: copied from Mono.Http.
+       * System.Net/NtlmClient.cs: use Mono.Http.NtmlClient from System
+       instead of dynamically loading the one in Mono.Http.
+
 2009-08-11  Jérémie Laval  <jeremie.laval@gmail.com>
 
        * System.dll.sources: Adding ParallelFx files.
diff --git a/mcs/class/System/Mono.Http/ChangeLog b/mcs/class/System/Mono.Http/ChangeLog
new file mode 100644 (file)
index 0000000..70f113d
--- /dev/null
@@ -0,0 +1,4 @@
+2009-08-26 Gonzalo Paniagua Javier <gonzalo@novell.com>
+
+       * NtlmClient.cs: copied from Mono.Http.
+
diff --git a/mcs/class/System/Mono.Http/NtlmClient.cs b/mcs/class/System/Mono.Http/NtlmClient.cs
new file mode 100644 (file)
index 0000000..a2c1ec1
--- /dev/null
@@ -0,0 +1,161 @@
+//
+// Mono.Http.NtlmClient
+//
+// Authors:
+//     Gonzalo Paniagua Javier (gonzalo@ximian.com)
+//
+// (c) 2003 Novell, Inc. (http://www.novell.com)
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#if SECURITY_DEP
+using System;
+using System.Collections;
+using System.Net;
+using Mono.Security.Protocol.Ntlm;
+
+namespace Mono.Http
+{
+       class NtlmSession
+       {
+               MessageBase message;
+
+               public NtlmSession () 
+               {
+               }
+
+               public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
+               {
+                       HttpWebRequest request = webRequest as HttpWebRequest;
+                       if (request == null)
+                               return null;
+       
+                       NetworkCredential cred = credentials.GetCredential (request.RequestUri, "NTLM");
+                       if (cred == null)
+                               return null;
+
+                       string userName = cred.UserName;
+                       string domain = cred.Domain;
+                       string password = cred.Password;
+                       if (userName == null || userName == "")
+                               return null;
+                       domain = domain != null && domain.Length > 0 ? domain : request.Headers ["Host"];
+
+                       bool completed = false;
+                       if (message == null) {
+                               Type1Message type1 = new Type1Message ();
+                               type1.Domain = domain;
+                               message = type1;
+                       } else if (message.Type == 1) {
+                               // Should I check the credentials?
+                               if (challenge == null) {
+                                       message = null;
+                                       return null;
+                               }
+
+                               Type2Message type2 = new Type2Message (Convert.FromBase64String (challenge));
+                               if (password == null)
+                                       password = "";
+
+                               Type3Message type3 = new Type3Message ();
+                               type3.Domain = domain;
+                               type3.Username = userName;
+                               type3.Challenge = type2.Nonce;
+                               type3.Password = password;
+                               message = type3;
+                               completed = true;
+                       } else {
+                               // Should I check the credentials?
+                               // type must be 3 here
+                               if (challenge == null || challenge == String.Empty) {
+                                       Type1Message type1 = new Type1Message ();
+                                       type1.Domain = domain;
+                                       message = type1;
+                               } else {
+                                       completed = true;
+                               }
+                       }
+                       
+                       string token = "NTLM " + Convert.ToBase64String (message.GetBytes ());
+                       return new Authorization (token, completed);
+               }
+       }
+
+       public class NtlmClient : IAuthenticationModule
+       {
+               static Hashtable cache;
+
+               static NtlmClient () 
+               {
+                       cache = new Hashtable ();
+               }
+       
+               public NtlmClient () {}
+       
+               public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
+               {
+                       if (credentials == null || challenge == null)
+                               return null;
+       
+                       string header = challenge.Trim ();
+                       int idx = header.ToLower ().IndexOf ("ntlm");
+                       if (idx == -1)
+                               return null;
+
+                       idx = header.IndexOfAny (new char [] {' ', '\t'});
+                       if (idx != -1) {
+                               header = header.Substring (idx).Trim ();
+                       } else {
+                               header = null;
+                       }
+
+                       HttpWebRequest request = webRequest as HttpWebRequest;
+                       if (request == null)
+                               return null;
+
+                       lock (cache) {
+                               NtlmSession ds = (NtlmSession) cache [request.RequestUri];
+                               if (ds == null) {
+                                       ds = new NtlmSession ();
+                                       cache.Add (request.RequestUri, ds);
+                               }
+
+                               return ds.Authenticate (header, webRequest, credentials);
+                       }
+               }
+
+               public Authorization PreAuthenticate (WebRequest webRequest, ICredentials credentials) 
+               {
+                       return null;
+               }
+       
+               public string AuthenticationType { 
+                       get { return "NTLM"; }
+               }
+       
+               public bool CanPreAuthenticate { 
+                       get { return false; }
+               }
+       }
+}
+#endif
index 7f0979716fc03ea576a276d725f734bf7c968416..f50d774ca0e78e23fbd908c96036bbe7a8abe5d7 100644 (file)
@@ -37,22 +37,13 @@ namespace System.Net
 {
        class NtlmClient : IAuthenticationModule
        {
-               static Type ntlmAuthType;
                IAuthenticationModule authObject;
 
-               static NtlmClient ()
-               {
-#if !MONOTOUCH
-                       Assembly ass = Assembly.Load (Consts.AssemblyMono_Http);
-                       if (ass != null)
-                               ntlmAuthType = ass.GetType ("Mono.Http.NtlmClient", false);
-#endif
-               }
-               
                public NtlmClient ()
                {
-                       if (ntlmAuthType != null)
-                               authObject = (IAuthenticationModule) Activator.CreateInstance (ntlmAuthType);
+#if SECURITY_DEP
+                       authObject = new Mono.Http.NtlmClient ();
+#endif
                }
        
                public Authorization Authenticate (string challenge, WebRequest webRequest, ICredentials credentials) 
index a67d8b4944c962f4092a6f97905e59ed6ce56324..2c06ce55b89adb4ab256bbfd902840372183dccb 100644 (file)
@@ -28,6 +28,7 @@ Microsoft.Win32/UserPreferenceChangedEventArgs.cs
 Microsoft.Win32/UserPreferenceChangedEventHandler.cs
 Microsoft.Win32/UserPreferenceChangingEventArgs.cs
 Microsoft.Win32/UserPreferenceChangingEventHandler.cs
+Mono.Http/NtlmClient.cs
 System.CodeDom/CodeArgumentReferenceExpression.cs
 System.CodeDom/CodeArrayCreateExpression.cs
 System.CodeDom/CodeArrayIndexerExpression.cs
index a1f0581a8d3c79db87f35ac92cf13ceb0f327891..704a83496a5481de8885a47a557b92ea1fbb9d35 100644 (file)
     <Compile Include="..\..\build\common\MonoTODOAttribute.cs">\r
       <Link>MonoTODOAttribute.cs</Link>\r
     </Compile>\r
+    <Compile Include="Mono.Http\NtlmClient.cs" />\r
     <Compile Include="System.CodeDom.Compiler\GeneratedCodeAttribute.cs" />\r
     <Compile Include="System.CodeDom\CodeArgumentReferenceExpression.cs" />\r
     <Compile Include="System.CodeDom\CodeArrayCreateExpression.cs" />\r
index 5c34d675856e4b72a167edfe296a0072297fe8c6..ba8e7d2ae090b67bea4c62c23fcd2983185e74f6 100644 (file)
@@ -1,4 +1,6 @@
 #include net_2_1_raw_System.dll.sources
+Mono.Http/NtlmClient.cs
+
 System.Collections.Specialized/NameObjectCollectionBase.cs
 System.Collections.Specialized/NameValueCollection.cs
 System.Collections.Specialized/ProcessStringDictionary.cs