Skip to content
Myr

Fix bug in renegotiating a nonce with the server

Before adoption: this commit came in with the history the project adopted, so no landing records it.

If a session runs long enough (without constant activity) then the server can expire the nonce the session has negotiated. If that happens the session will get a new 401 response which we were immediately returning to the user. A user would then have to essentially reinitialize session.auth each time they get an unexpected 401. Also, there's no need for setattr calls when we can simply assign the attribute on the instance.

authored by Ian Cordasco · committed by Ian Cordasco · parent bd3cf95 · Browse files · 1 file+4 added−3 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+4 added−3 removed

  • other:+4 added−3 removed

1 file+4 added−3 removed

modified:requests/auth.py+4 added-3 removed
65 unmodified lines
66 self.last_nonce = ''67 self.nonce_count = 068 self.chal = {}69 self.pos = None70 self.num_401_calls = 17172 def build_digest_header(self, method, url):7374 realm = self.chal['realm']
79 unmodified lines
154155 def handle_redirect(self, r, **kwargs):156 """Reset num_401_calls counter on redirects."""157 if r.is_redirect:157 setattr(self, 'num_401_calls', 1)158 self.num_401_calls = 1159160 def handle_401(self, r, **kwargs):161 """Takes the given response and tries digest-auth, if needed."""162
5 unmodified lines
168 s_auth = r.headers.get('www-authenticate', '')169170 if 'digest' in s_auth.lower() and num_401_calls < 2:171171 setattr(self, 'num_401_calls', num_401_calls + 1)172 self.num_401_calls += 1173 pat = re.compile(r'digest ', flags=re.IGNORECASE)174 self.chal = parse_dict_header(pat.sub('', s_auth, count=1))175176 # Consume content and release the original connection
11 unmodified lines
188 _r.request = prep189190 return _r191191 setattr(self, 'num_401_calls', num_401_calls + 1)192 self.num_401_calls = 1193 return r194195 def __call__(self, r):196 # If we have a saved nonce, skip the 401
13 unmodified lines