Fixed #1623. Added 'MD5-sess' algorithm to HTTPDigestAuth
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by daftshady · committed by daftshady · parent 9968a10 · Browse files · 1 file·+22 added−17 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+22 added−17 removed
- other:+22 added−17 removed
Provenance
- Commit
3501ea4- Tree
6deb01f- Author
daftshady <daftonshady@gmail.com>- Committer
daftshady <daftonshady@gmail.com>- Parent
9968a10
1 file changed+22 added−17 removed
modified:requests/auth.py+22 added-17 removed
75 unmodified lines76 _algorithm = 'MD5'77 else:78 _algorithm = algorithm.upper()79 # lambdas assume digest modules are imported at the top level80- if _algorithm == 'MD5':80+ if _algorithm == 'MD5' or _algorithm == 'MD5-SESS':81 def md5_utf8(x):82 if isinstance(x, str):83 x = x.encode('utf-8')84 return hashlib.md5(x).hexdigest()3 unmodified lines88 if isinstance(x, str):89 x = x.encode('utf-8')90 return hashlib.sha1(x).hexdigest()91 hash_utf8 = sha_utf892- # XXX MD5-sess92+93 KD = lambda s, d: hash_utf8("%s:%s" % (s, d))94 95 if hash_utf8 is None:96 return None6 unmodified lines103 path += '?' + p_parsed.query104 105 A1 = '%s:%s:%s' % (self.username, realm, self.password)106 A2 = '%s:%s' % (method, path)107+ 108+ HA1 = hash_utf8(A1)109+ HA2 = hash_utf8(A2)110 108- if qop is None:109- respdig = KD(hash_utf8(A1), "%s:%s" % (nonce, hash_utf8(A2)))110- elif qop == 'auth' or 'auth' in qop.split(','):111- if nonce == self.last_nonce:112- self.nonce_count += 1113- else:114- self.nonce_count = 1111+ if nonce == self.last_nonce:112+ self.nonce_count += 1113+ else:114+ self.nonce_count = 1115+ ncvalue = '%08x' % self.nonce_count116+ s = str(self.nonce_count).encode('utf-8')117+ s += nonce.encode('utf-8')118+ s += time.ctime().encode('utf-8')119+ s += os.urandom(8)120 116- ncvalue = '%08x' % self.nonce_count117- s = str(self.nonce_count).encode('utf-8')118- s += nonce.encode('utf-8')119- s += time.ctime().encode('utf-8')120- s += os.urandom(8)121+ cnonce = (hashlib.sha1(s).hexdigest()[:16])122+ noncebit = "%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, qop, HA2)123+ if _algorithm == 'MD5-SESS':124+ HA1 = hash_utf8('%s:%s:%s' % (HA1, nonce, cnonce))125 122- cnonce = (hashlib.sha1(s).hexdigest()[:16])123- noncebit = "%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, qop, hash_utf8(A2))124- respdig = KD(hash_utf8(A1), noncebit)126+ if qop is None:127+ respdig = KD(HA1, "%s:%s" % (nonce, HA2))128+ elif qop == 'auth' or 'auth' in qop.split(','):129+ respdig = KD(HA1, noncebit)130 else:131 # XXX handle auth-int.132 return None133 52 unmodified lines