Add digest auth support.
authorAdam Sampson <ats@offog.org>
Tue, 9 Jul 2013 20:51:21 +0000 (20:51 +0000)
committerAdam Sampson <ats@offog.org>
Tue, 9 Jul 2013 20:51:21 +0000 (20:51 +0000)
This works with curl and urllib2, at least.

testserver.py

index 70bb0013f5376ad144c196a8eee5c67e6c16aa54..7f3cc895d853fa8f2860b5d71b0d69cca662be4c 100644 (file)
@@ -77,6 +77,27 @@ class HTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
                 return None
             self.path = m.group(3)
 
+        m = re.match(r'^/digest-([^/-]+)-([^/]+)(/.*)$', self.path)
+        if m:
+            # Require digest authentication. (Not a good implementation!)
+            realm = "rawdog test server"
+            nonce = "0123456789abcdef"
+            a1 = m.group(1) + ":" + realm + ":" + m.group(2)
+            a2 = "GET:" + self.path
+            def h(s):
+                return hashlib.md5(s).hexdigest()
+            response = h(h(a1) + ":" + nonce + ":" + h(a2))
+            mr = re.search(r'response="([^"]*)"',
+                         self.headers.get("Authorization", ""))
+            if mr is None or mr.group(1) != response:
+                self.send_response(401)
+                self.send_header("WWW-Authenticate",
+                                 'Digest realm="%s", nonce="%s"'
+                                     % (realm, nonce))
+                self.end_headers()
+                return None
+            self.path = m.group(3)
+
         m = re.match(r'^/(\d\d\d)(/.*)?$', self.path)
         if m:
             # Request for a particular response code.