Skip to content
Myr

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

1 file+93 added−1 removed

modified:requests/auth.py+93 added-1 removed
5 unmodified lines
67This module contains the authentication handlers for Requests.8"""910import time11import hashlib1213from base64 import b64encode14from urlparse import urlparse1516from .utils import randombytes, parse_dict_header171819def 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 lines
34 """Attaches HTTP Digest Authentication to the given Request object.35 Arguments should be considered non-positional.36 """3731 r.headers38 def handle_401(r):39 """Takes the given response and tries digest-auth, if needed."""4041 s_auth = r.headers.get('www-authenticate', '')4243 if 'digest' in s_auth.lower():4445 last_nonce = ''46 nonce_count = 04748 chal = parse_dict_header(s_auth.replace('Digest ', ''))4950 realm = chal['realm']51 nonce = chal['nonce']52 qop = chal.get('qop')53 algorithm = chal.get('algorithm', 'MD5')54 opaque = chal.get('opaque', None)5556 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))6465 if H is None:66 return None6768 # XXX not implemented yet69 entdig = None70 path = urlparse(r.request.url).path7172 A1 = "%s:%s:%s" % (username, realm, password)73 A2 = "%s:%s" % (r.request.method, path)7475 if qop == 'auth':76 if nonce == last_nonce:77 nonce_count += 178 else:79 nonce_count = 180 last_nonce = nonce8182 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 None9495 # 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)105106107 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_code112113 # r.request.response114115 print locals()116117 print _r.headers118 return _r119120 return r121122 r.hooks['response'] = handle_401123 return r124125126def dispatch(t):127 """Given an auth tuple, return an expanded version."""128129 if not t:
22 unmodified lines