changeset 6:f7f1b66283c6

Factored out the auth handlers from LocalAuth.py.sample and moved them to AuthHandlers.py, added comments to them.
author Atul Varma <varmaa@toolness.com>
date Sun, 17 Feb 2008 19:45:36 -0600
parents 56bd30b89166
children c461e818f4ca
files AuthHandlers.py LocalAuth.py.sample
diffstat 2 files changed, 43 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AuthHandlers.py	Sun Feb 17 19:45:36 2008 -0600
@@ -0,0 +1,39 @@
+import urllib2
+import re
+
+# Auth handlers that can be modified to provide logging or
+# hacks/bug-fixes.
+
+class BasicAuthHandler( urllib2.HTTPBasicAuthHandler ):
+    def http_error_401(self, req, fp, code, msg, headers):
+        result = urllib2.HTTPBasicAuthHandler.http_error_401(
+            self, req, fp, code, msg, headers
+            )
+        return result
+
+    def http_error_auth_reqed(self, authreq, host, req, headers):
+        # This auth has been modified to fix the bug outlined in
+        # Issue 2136: http://bugs.python.org/issue2136
+        rx_sq = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+realm=\'([^\']*)\'',
+                           re.I)
+
+        authreq = headers.get(authreq, None)
+        if authreq:
+            mo = urllib2.AbstractBasicAuthHandler.rx.search(authreq)
+            if not mo:
+                mo = rx_sq.search(authreq)
+            if mo:
+                scheme, realm = mo.groups()
+                if scheme.lower() == 'basic':
+                    return self.retry_http_basic_auth(host, req, realm)
+
+class DigestAuthHandler( urllib2.HTTPDigestAuthHandler ):
+    def get_authorization(self, req, chal):
+        return urllib2.HTTPDigestAuthHandler.get_authorization(
+            self, req, chal
+            )
+
+    def http_error_401(self, req, fp, code, msg, headers):
+        return urllib2.HTTPDigestAuthHandler.http_error_401(
+            self, req, fp, code, msg, headers
+            )
--- a/LocalAuth.py.sample	Sat Feb 16 22:43:35 2008 -0600
+++ b/LocalAuth.py.sample	Sun Feb 17 19:45:36 2008 -0600
@@ -1,45 +1,9 @@
-import urllib2
-
-class MyBasicAuthHandler( urllib2.HTTPBasicAuthHandler ):
-    def http_error_401(self, req, fp, code, msg, headers):
-        result = urllib2.HTTPBasicAuthHandler.http_error_401(
-            self, req, fp, code, msg, headers
-            )
-        return result
-
-    def http_error_auth_reqed(self, authreq, host, req, headers):
-        # MODIFIED VERSION OF THE ORIGINAL METHOD TO FIX A BUG.
-
-        # host may be an authority (without userinfo) or a URL with an
-        # authority
-        # XXX could be multiple headers
-        authreq = headers.get(authreq, None)
-        if authreq:
-            mo = urllib2.AbstractBasicAuthHandler.rx.search(authreq)
-            if not mo:
-                # HACK TO GET THINGS TO WORK IF THE AUTHREQ USES
-                # SINGLE QUOTES INSTEAD OF DOUBLE QUOTES - A.V.
-                authreq = authreq.replace( "'", "\"" )
-                mo = urllib2.AbstractBasicAuthHandler.rx.search(authreq)
-            if mo:
-                scheme, realm = mo.groups()
-                if scheme.lower() == 'basic':
-                    return self.retry_http_basic_auth(host, req, realm)
-
-class MyDigestAuthHandler( urllib2.HTTPDigestAuthHandler ):
-    def get_authorization(self, req, chal):
-        return urllib2.HTTPDigestAuthHandler.get_authorization(
-            self, req, chal
-            )
-
-    def http_error_401(self, req, fp, code, msg, headers):
-        return urllib2.HTTPDigestAuthHandler.http_error_401(
-            self, req, fp, code, msg, headers
-            )
+from AuthHandlers import BasicAuthHandler
+from AuthHandlers import DigestAuthHandler
 
 def getAuthHandlers():
-    basicAuth = MyBasicAuthHandler()
-    digestAuth = MyDigestAuthHandler()
+    basicAuth = BasicAuthHandler()
+    digestAuth = DigestAuthHandler()
     basicAuth.add_password( "Private",
                             "intranet.mozilla.org",
                             "my_mozilla_username",