1 ''' factotum support '''
8 class factotumbasic(urllib2.BaseHandler):
9 def __init__(self, passmgr=None):
10 self.f = factotum.Factotum()
13 def http_error_401(self, req, fp, code, msg, headers):
14 host = urllib2.urlparse.urlparse(req.get_full_url())[1]
15 authreq = headers.get('www-authenticate', None)
16 if authreq == None: return None
17 authreq = authreq.split(' ', 1)
18 if authreq[0].lower() != 'basic': return None
19 chal = urllib2.parse_keqv_list(urllib2.parse_http_list(authreq[1]))
21 self.auth = (host, realm)
24 self.f.delkey(proto="pass", host=host, realm=realm, role="client")
25 self.f.start(proto="pass", host=host, realm=realm, role="client")
26 pw = self.f.read().replace(' ', ':', 1)
27 val = 'Basic %s' % base64.b64encode(pw).strip()
28 if req.headers.get('Authorization', None) == val: return None
29 req.add_header('Authorization', val)
30 result = self.parent.open(req)
33 def http_error_403(self, req, fp, code, msg, headers):
35 self.f.delkey(proto="pass", host=self.auth[0], realm=self.auth[1], role="client")
38 class factotumdigest(urllib2.BaseHandler):
39 auth_header = 'Authorization'
42 def __init__(self, passmgr=None):
43 self.f = factotum.Factotum()
45 def http_error_401(self, req, fp, code, msg, headers):
47 host = urllib2.urlparse.urlparse(req.get_full_url())[1]
48 authreq = headers.get('www-authenticate', None)
49 if authreq == None: return None
50 authreq = authreq.split(' ', 1)
51 if authreq[0].lower() != 'digest': return None
52 chal = urllib2.parse_keqv_list(urllib2.parse_http_list(authreq[1]))
56 self.f.delkey(proto="httpdigest", realm=realm, host=host)
57 self.f.start(proto="httpdigest", role="client", realm=realm, host=host)
58 self.f.write(nonce + ' ' + req.get_method() + ' ' + req.get_selector())
60 user = self.f.attr()["user"]
62 val = 'Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s", algorithm=MD5' % (user, realm, nonce, req.get_selector(), resp)
63 if req.headers.get('Authorization', None) == val: return None
64 req.add_unredirected_header('Authorization', val)
65 result = self.parent.open(req)
69 urllib2.HTTPBasicAuthHandler = factotumbasic
70 mercurial.url.httpdigestauthhandler = factotumdigest