added HTTP Basic to hgfactotum

This commit is contained in:
aiju 2011-05-09 18:53:34 +00:00
parent ea0fe9a39b
commit d232167dc3
2 changed files with 74 additions and 7 deletions

View file

@ -3,6 +3,26 @@
import mercurial.url
import urllib2
import factotum
import base64
class factotumbasic(urllib2.BaseHandler):
def __init__(self, passmgr=None):
self.f = factotum.Factotum()
def http_error_401(self, req, fp, code, msg, headers):
host = urllib2.urlparse.urlparse(req.get_full_url())[1]
authreq = headers.get('www-authenticate', None)
if authreq == None: return None
authreq = authreq.split(' ', 1)
if authreq[0].lower() != 'basic': return None
chal = urllib2.parse_keqv_list(urllib2.parse_http_list(authreq[1]))
realm = chal['realm']
self.f.start(proto="pass", host=host, realm=realm, role="client")
pw = self.f.read()
user = self.f.attr()["user"]
val = 'Basic %s' % base64.b64encode(user + ':' + pw).strip()
if req.headers.get('Authorization', None) == val: return None
req.add_header('Authorization', val)
return self.parent.open(req)
class factotumdigest(urllib2.BaseHandler):
auth_header = 'Authorization'
@ -22,16 +42,18 @@ class factotumdigest(urllib2.BaseHandler):
realm = chal['realm']
nonce = chal['nonce']
if self.retried >= 6:
self.f.delkey(proto="httpdigest", realm=realm)
self.f.start(proto="httpdigest", role="client", realm=realm)
self.f.delkey(proto="httpdigest", realm=realm, host=host)
self.f.start(proto="httpdigest", role="client", realm=realm, host=host)
self.f.write(nonce + ' ' + req.get_method() + ' ' + req.get_selector())
resp = self.f.read()
user = self.f.attr()["user"]
self.f.close()
val = 'Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s", algorithm=MD5' % ("aiju", realm, nonce, req.get_selector(), resp)
val = 'Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s", algorithm=MD5' % (user, realm, nonce, req.get_selector(), resp)
if req.headers.get('Authorization', None) == val: return None
req.add_unredirected_header('Authorization', val)
result = self.parent.open(req)
self.retried = 0
return result
urllib2.HTTPBasicAuthHandler = factotumbasic
mercurial.url.httpdigestauthhandler = factotumdigest