Issue #2334 - HTTPDigestAuth - All state now in thread local storage
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Following feedback from tardyp and @vincentxb.
authored by exvito · committed by exvito · parent e65360d · Browse files · 1 file·+38 added−27 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+38 added−27 removed
- other:+38 added−27 removed
Provenance
- Commit
e8d9bc5- Tree
64b8108- Author
exvito <ex.vitorino@gmail.com>- Committer
exvito <ex.vitorino@gmail.com>- Parent
e65360d
1 file changed+38 added−27 removed
modified:requests/auth.py+38 added-27 removed
62 unmodified lines63 """Attaches HTTP Digest Authentication to the given Request object."""64 def __init__(self, username, password):65 self.username = username66 self.password = password67- self.last_nonce = ''68- self.nonce_count = 069- self.chal = {}70- self.pos = None71- self.num_401_calls = threading.local()67+ # Keep state in per-thread local storage68+ self.tl = threading.local()69+ self.init_per_thread_state()70 71+ def init_per_thread_state(self):72+ self.tl.last_nonce = ''73+ self.tl.nonce_count = 074+ self.tl.chal = {}75+ self.tl.pos = None76+ self.tl.num_401_calls = None77+78 def build_digest_header(self, method, url):79 75- realm = self.chal['realm']76- nonce = self.chal['nonce']77- qop = self.chal.get('qop')78- algorithm = self.chal.get('algorithm')79- opaque = self.chal.get('opaque')80+ realm = self.tl.chal['realm']81+ nonce = self.tl.chal['nonce']82+ qop = self.tl.chal.get('qop')83+ algorithm = self.tl.chal.get('algorithm')84+ opaque = self.tl.chal.get('opaque')85 86 if algorithm is None:87 _algorithm = 'MD5'88 else:29 unmodified lines118 119 HA1 = hash_utf8(A1)120 HA2 = hash_utf8(A2)121 117- if nonce == self.last_nonce:118- self.nonce_count += 1122+ if nonce == self.tl.last_nonce:123+ self.tl.nonce_count += 1124 else:120- self.nonce_count = 1121- ncvalue = '%08x' % self.nonce_count122- s = str(self.nonce_count).encode('utf-8')125+ self.tl.nonce_count = 1126+ ncvalue = '%08x' % self.tl.nonce_count127+ s = str(self.tl.nonce_count).encode('utf-8')128 s += nonce.encode('utf-8')129 s += time.ctime().encode('utf-8')130 s += os.urandom(8)131 11 unmodified lines143 else:144 # XXX handle auth-int.145 return None146 142- self.last_nonce = nonce147+ self.tl.last_nonce = nonce148 149 # XXX should the partial digests be encoded too?150 base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \151 'response="%s"' % (self.username, realm, nonce, path, respdig)10 unmodified lines162 163 def handle_redirect(self, r, **kwargs):164 """Reset num_401_calls counter on redirects."""165 if r.is_redirect:161- self.num_401_calls.value = 1166+ self.tl.num_401_calls = 1167 168 def handle_401(self, r, **kwargs):169 """Takes the given response and tries digest-auth, if needed."""170 166- if self.pos is not None:171+ if self.tl.pos is not None:172 # Rewind the file position indicator of the body to where173 # it was to resend the request.169- r.request.body.seek(self.pos)170- num_401_calls = self.num_401_calls.value174+ r.request.body.seek(self.tl.pos)175+ num_401_calls = self.tl.num_401_calls176 s_auth = r.headers.get('www-authenticate', '')177 178 if 'digest' in s_auth.lower() and num_401_calls < 2:179 175- self.num_401_calls.value += 1180+ self.tl.num_401_calls += 1181 pat = re.compile(r'digest ', flags=re.IGNORECASE)177- self.chal = parse_dict_header(pat.sub('', s_auth, count=1))182+ self.tl.chal = parse_dict_header(pat.sub('', s_auth, count=1))183 184 # Consume content and release the original connection185 # to allow our new request to reuse the same one.186 r.content9 unmodified lines196 _r.request = prep197 198 return _r199 195- self.num_401_calls.value = 1200+ self.tl.num_401_calls = 1201 return r202 203 def __call__(self, r):204+ # When called from a thread other than the one that __init__'ed us205+ # per-thread state may be missing: initialize it if that's the case.206+ try:207+ self.tl.last_nonce208+ except AttributeError:209+ self.init_per_thread_state()210 # If we have a saved nonce, skip the 401200- if self.last_nonce:211+ if self.tl.last_nonce:212 r.headers['Authorization'] = self.build_digest_header(r.method, r.url)213 try:203- self.pos = r.body.tell()214+ self.tl.pos = r.body.tell()215 except AttributeError:216 # In the case of HTTPDigestAuth being reused and the body of217 # the previous request was a file-like object, pos has the218 # file position of the previous body. Ensure it's set to219 # None.209- self.pos = None220+ self.tl.pos = None221 r.register_hook('response', self.handle_401)222 r.register_hook('response', self.handle_redirect)212- self.num_401_calls.value = 1223+ self.tl.num_401_calls = 1224 225 return r