Fix bug in renegotiating a nonce with the server
authorIan Cordasco <ian.cordasco@rackspace.com>
Tue, 23 Dec 2014 16:40:31 +0000 (10:40 -0600)
committerIan Cordasco <ian.cordasco@rackspace.com>
Tue, 23 Dec 2014 16:40:31 +0000 (10:40 -0600)
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.

requests/auth.py

index 618a902a29d32fc4af8e99ba0ba0bb688b1c6c60..b950181d9e4839a7c1ace822db7f401438317e66 100644 (file)
@@ -67,6 +67,7 @@ class HTTPDigestAuth(AuthBase):
         self.nonce_count = 0
         self.chal = {}
         self.pos = None
+        self.num_401_calls = 1
 
     def build_digest_header(self, method, url):
 
@@ -154,7 +155,7 @@ class HTTPDigestAuth(AuthBase):
     def handle_redirect(self, r, **kwargs):
         """Reset num_401_calls counter on redirects."""
         if r.is_redirect:
-            setattr(self, 'num_401_calls', 1)
+            self.num_401_calls = 1
 
     def handle_401(self, r, **kwargs):
         """Takes the given response and tries digest-auth, if needed."""
@@ -168,7 +169,7 @@ class HTTPDigestAuth(AuthBase):
 
         if 'digest' in s_auth.lower() and num_401_calls < 2:
 
-            setattr(self, 'num_401_calls', num_401_calls + 1)
+            self.num_401_calls += 1
             pat = re.compile(r'digest ', flags=re.IGNORECASE)
             self.chal = parse_dict_header(pat.sub('', s_auth, count=1))
 
@@ -188,7 +189,7 @@ class HTTPDigestAuth(AuthBase):
 
             return _r
 
-        setattr(self, 'num_401_calls', num_401_calls + 1)
+        self.num_401_calls = 1
         return r
 
     def __call__(self, r):