2007-07-12 Geoff Norton <gnorton@customerdna.com>
authorGeoff Norton <grompf@sublimeintervention.com>
Thu, 12 Jul 2007 18:04:35 +0000 (18:04 -0000)
committerGeoff Norton <grompf@sublimeintervention.com>
Thu, 12 Jul 2007 18:04:35 +0000 (18:04 -0000)
    * HttpListenerContext.cs:
    * HttpListener.cs: Handle HttpListener AuthenticationSchemes
    Only Basic authentication is included for now.

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

mcs/class/System/System.Net/ChangeLog
mcs/class/System/System.Net/HttpListener.cs
mcs/class/System/System.Net/HttpListenerContext.cs

index 74c1cb873bfa2906f748a31d10e073ae9b47e058..4026318f1eaf7fa58507f51ac7df8671515771ff 100644 (file)
@@ -1,3 +1,9 @@
+2007-07-12  Geoff Norton  <gnorton@customerdna.com>
+
+       * HttpListenerContext.cs: 
+       * HttpListener.cs: Handle HttpListener AuthenticationSchemes
+       Only Basic authentication is included for now.
+
 2007-06-30  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * NetworkAccess.cs: Only mark as flags enum on 2.0.
index 662f8291165d14f39fb830ad9140a7dc723599d1..241cf114f528085e30b6e8e7cf458729b0bf7c35 100644 (file)
@@ -217,7 +217,11 @@ namespace System.Net {
                                        wait_queue.RemoveAt (idx);
                        }
 
-                       return ares.GetContext (); // This will throw on error.
+                       HttpListenerContext context = ares.GetContext ();
+                       if (auth_schemes != AuthenticationSchemes.Anonymous) {
+                               context.ParseAuthentication ();
+                       }
+                       return context; // This will throw on error.
                }
 
                public HttpListenerContext GetContext ()
index 43c08a2b92beae03d31704d9ce2b7489bd91aaa3..6ec4ae106a739fc391f774f253df97801c5d9a12 100644 (file)
@@ -78,6 +78,59 @@ namespace System.Net {
                public IPrincipal User {
                        get { return user; }
                }
+
+               internal void ParseAuthentication () {
+                       // TODO: Handle NTLM/Digest modes
+                       string header = request.Headers ["Authorization"];
+
+                       if (header == null || header.Length < 2)
+                               return;
+
+                       string [] authenticationData = header.Substring (header.IndexOf (":")+1).Split (new char [] {' '});
+
+                       if (string.Compare (authenticationData [0], "basic", true) == 0) {
+                               user = ParseBasicAuthentication (authenticationData [1]);
+                       }
+               }
+       
+               internal IPrincipal ParseBasicAuthentication (string authData) {
+                       try {
+                               // Basic AUTH Data is a formatted Base64 String
+                               string domain = null;
+                               string user = null;
+                               string password = null;
+                               int pos = -1;
+                               string authString = System.Text.Encoding.Default.GetString (Convert.FromBase64String (authData));
+       
+                               // The format is DOMAIN\username:password
+                               // Domain is optional
+
+                               pos = authString.IndexOf (":");
+       
+                               // parse the password off the end
+                               password = authString.Substring (pos+1);
+                               
+                               // discard the password
+                               authString = authString.Substring (0, pos);
+       
+                               // check if there is a domain
+                               pos = authString.IndexOf ("\\");
+       
+                               if (pos > 0) {
+                                       domain = authString.Substring (0, pos);
+                                       user = authString.Substring (pos);
+                               } else {
+                                       user = authString;
+                               }
+       
+                               HttpListenerBasicIdentity identity = new HttpListenerBasicIdentity (user, password);
+                               // TODO: What are the roles MS sets
+                               return new GenericPrincipal (identity, new string [0]);
+                       } catch (Exception) {
+                               // Invalid auth data is swallowed silently
+                               return null;
+                       } 
+               }
        }
 }
 #endif