| 1 |
import base64 |
| 2 |
import hashlib |
| 3 |
|
| 4 |
from mercurial.hgweb import common |
| 5 |
from mercurial import node |
| 6 |
|
| 7 |
|
| 8 |
def parse_keqv_list(req, l): |
| 9 |
"""Parse list of key=value strings where keys are not duplicated.""" |
| 10 |
parsed = {} |
| 11 |
for elt in l: |
| 12 |
k, v = elt.split(b'=', 1) |
| 13 |
if v[0:1] == b'"' and v[-1:] == b'"': |
| 14 |
v = v[1:-1] |
| 15 |
parsed[k] = v |
| 16 |
return parsed |
| 17 |
|
| 18 |
|
| 19 |
class digestauthserver: |
| 20 |
def __init__(self): |
| 21 |
self._user_hashes = {} |
| 22 |
|
| 23 |
def gethashers(self): |
| 24 |
def _md5sum(x): |
| 25 |
m = hashlib.md5() |
| 26 |
m.update(x) |
| 27 |
return node.hex(m.digest()) |
| 28 |
|
| 29 |
h = _md5sum |
| 30 |
|
| 31 |
kd = lambda s, d, h=h: h(b"%s:%s" % (s, d)) |
| 32 |
return h, kd |
| 33 |
|
| 34 |
def adduser(self, user, password, realm): |
| 35 |
h, kd = self.gethashers() |
| 36 |
a1 = h(b'%s:%s:%s' % (user, realm, password)) |
| 37 |
self._user_hashes[(user, realm)] = a1 |
| 38 |
|
| 39 |
def makechallenge(self, realm): |
| 40 |
# We aren't testing the protocol here, just that the bytes make the |
| 41 |
# proper round trip. So hardcoded seems fine. |
| 42 |
nonce = b'064af982c5b571cea6450d8eda91c20d' |
| 43 |
return b'realm="%s", nonce="%s", algorithm=MD5, qop="auth"' % ( |
| 44 |
realm, |
| 45 |
nonce, |
| 46 |
) |
| 47 |
|
| 48 |
def checkauth(self, req, header): |
| 49 |
log = req.rawenv[b'wsgi.errors'] |
| 50 |
|
| 51 |
h, kd = self.gethashers() |
| 52 |
resp = parse_keqv_list(req, header.split(b', ')) |
| 53 |
|
| 54 |
if resp.get(b'algorithm', b'MD5').upper() != b'MD5': |
| 55 |
log.write(b'Unsupported algorithm: %s' % resp.get(b'algorithm')) |
| 56 |
raise common.ErrorResponse( |
| 57 |
common.HTTP_FORBIDDEN, b"unknown algorithm" |
| 58 |
) |
| 59 |
user = resp[b'username'] |
| 60 |
realm = resp[b'realm'] |
| 61 |
nonce = resp[b'nonce'] |
| 62 |
|
| 63 |
ha1 = self._user_hashes.get((user, realm)) |
| 64 |
if not ha1: |
| 65 |
log.write(b'No hash found for user/realm "%s/%s"' % (user, realm)) |
| 66 |
raise common.ErrorResponse(common.HTTP_FORBIDDEN, b"bad user") |
| 67 |
|
| 68 |
qop = resp.get(b'qop', b'auth') |
| 69 |
if qop != b'auth': |
| 70 |
log.write(b"Unsupported qop: %s" % qop) |
| 71 |
raise common.ErrorResponse(common.HTTP_FORBIDDEN, b"bad qop") |
| 72 |
|
| 73 |
cnonce, ncvalue = resp.get(b'cnonce'), resp.get(b'nc') |
| 74 |
if not cnonce or not ncvalue: |
| 75 |
log.write(b'No cnonce (%s) or ncvalue (%s)' % (cnonce, ncvalue)) |
| 76 |
raise common.ErrorResponse(common.HTTP_FORBIDDEN, b"no cnonce") |
| 77 |
|
| 78 |
a2 = b'%s:%s' % (req.method, resp[b'uri']) |
| 79 |
noncebit = b"%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, qop, h(a2)) |
| 80 |
|
| 81 |
respdig = kd(ha1, noncebit) |
| 82 |
if respdig != resp[b'response']: |
| 83 |
log.write( |
| 84 |
b'User/realm "%s/%s" gave %s, but expected %s' |
| 85 |
% (user, realm, resp[b'response'], respdig) |
| 86 |
) |
| 87 |
return False |
| 88 |
|
| 89 |
return True |
| 90 |
|
| 91 |
|
| 92 |
digest = digestauthserver() |
| 93 |
|
| 94 |
|
| 95 |
def perform_authentication(hgweb, req, op): |
| 96 |
auth = req.headers.get(b'Authorization') |
| 97 |
|
| 98 |
if req.headers.get(b'X-HgTest-AuthType') == b'Digest': |
| 99 |
if not auth: |
| 100 |
challenge = digest.makechallenge(b'mercurial') |
| 101 |
raise common.ErrorResponse( |
| 102 |
common.HTTP_UNAUTHORIZED, |
| 103 |
b'who', |
| 104 |
[(b'WWW-Authenticate', b'Digest %s' % challenge)], |
| 105 |
) |
| 106 |
|
| 107 |
if not digest.checkauth(req, auth[7:]): |
| 108 |
raise common.ErrorResponse(common.HTTP_FORBIDDEN, b'no') |
| 109 |
|
| 110 |
return |
| 111 |
|
| 112 |
if not auth: |
| 113 |
raise common.ErrorResponse( |
| 114 |
common.HTTP_UNAUTHORIZED, |
| 115 |
b'who', |
| 116 |
[(b'WWW-Authenticate', b'Basic Realm="mercurial"')], |
| 117 |
) |
| 118 |
|
| 119 |
if base64.b64decode(auth.split()[1]).split(b':', 1) != [b'user', b'pass']: |
| 120 |
raise common.ErrorResponse(common.HTTP_FORBIDDEN, b'no') |
| 121 |
|
| 122 |
|
| 123 |
def extsetup(ui): |
| 124 |
common.permhooks.insert(0, perform_authentication) |
| 125 |
digest.adduser(b'user', b'pass', b'mercurial') |