Digest authentication support!
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Ripped from urllib2
authored by Kenneth Reitz · committed by Kenneth Reitz · parent b7c788c · Browse files · 1 file·+93 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+93 added−1 removed
- other:+93 added−1 removed
Provenance
- Commit
60b37e5- Tree
9bfa4d7- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
b7c788c
1 file changed+93 added−1 removed
modified:requests/auth.py+93 added-1 removed
5 unmodified lines6 7 This module contains the authentication handlers for Requests.8 """9 10+import time11+import hashlib12+13 from base64 import b64encode14+from urlparse import urlparse15 16+from .utils import randombytes, parse_dict_header17+18+19 def http_basic(r, username, password):20 """Attaches HTTP Basic Authentication to the given Request object.21 Arguments should be considered non-positional.22 11 unmodified lines34 """Attaches HTTP Digest Authentication to the given Request object.35 Arguments should be considered non-positional.36 """37 31- r.headers38+ def handle_401(r):39+ """Takes the given response and tries digest-auth, if needed."""40 41+ s_auth = r.headers.get('www-authenticate', '')42 43+ if 'digest' in s_auth.lower():44+45+ last_nonce = ''46+ nonce_count = 047+48+ chal = parse_dict_header(s_auth.replace('Digest ', ''))49+50+ realm = chal['realm']51+ nonce = chal['nonce']52+ qop = chal.get('qop')53+ algorithm = chal.get('algorithm', 'MD5')54+ opaque = chal.get('opaque', None)55+56+ algorithm = algorithm.upper()57+ # lambdas assume digest modules are imported at the top level58+ if algorithm == 'MD5':59+ H = lambda x: hashlib.md5(x).hexdigest()60+ elif algorithm == 'SHA':61+ H = lambda x: hashlib.sha1(x).hexdigest()62+ # XXX MD5-sess63+ KD = lambda s, d: H("%s:%s" % (s, d))64+65+ if H is None:66+ return None67+68+ # XXX not implemented yet69+ entdig = None70+ path = urlparse(r.request.url).path71+72+ A1 = "%s:%s:%s" % (username, realm, password)73+ A2 = "%s:%s" % (r.request.method, path)74+75+ if qop == 'auth':76+ if nonce == last_nonce:77+ nonce_count += 178+ else:79+ nonce_count = 180+ last_nonce = nonce81+82+ ncvalue = '%08x' % nonce_count83+ cnonce = (hashlib.sha1("%s:%s:%s:%s" % (84+ nonce_count, nonce, time.ctime(), randombytes(8)))85+ .hexdigest()[:16]86+ )87+ noncebit = "%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, qop, H(A2))88+ respdig = KD(H(A1), noncebit)89+ elif qop is None:90+ respdig = KD(H(A1), "%s:%s" % (nonce, H(A2)))91+ else:92+ # XXX handle auth-int.93+ return None94+95+ # XXX should the partial digests be encoded too?96+ base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \97+ 'response="%s"' % (username, realm, nonce, path, respdig)98+ if opaque:99+ base += ', opaque="%s"' % opaque100+ if entdig:101+ base += ', digest="%s"' % entdig102+ base += ', algorithm="%s"' % algorithm103+ if qop:104+ base += ', qop=auth, nc=%s, cnonce="%s"' % (ncvalue, cnonce)105+106+107+ r.request.headers['Authorization'] = 'Digest %s' % (base)108+ r.request.send(anyway=True)109+ _r = r.request.response110+ _r.history.append(r)111+ print _r.status_code112+113+ # r.request.response114+115+ print locals()116+117+ print _r.headers118+ return _r119+120+ return r121+122+ r.hooks['response'] = handle_401123+ return r124+125+126 def dispatch(t):127 """Given an auth tuple, return an expanded version."""128 129 if not t:22 unmodified lines