From c28da22e9c42e22b303bb07da434ce65e10c0cb2 Mon Sep 17 00:00:00 2001 From: Yossi Gottlieb Date: Sat, 27 Sep 2014 20:42:58 +0300 Subject: [PATCH 1/3] A fix for #1979 repeat HTTP digest authentication after redirect. --- requests/auth.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/requests/auth.py b/requests/auth.py index 9b6426dc..41be9829 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -150,6 +150,14 @@ class HTTPDigestAuth(AuthBase): return 'Digest %s' % (base) + def handle_302(self, r, **kwargs): + """Reset num_401_calls counter on redirects.""" + try: + delattr(self, 'num_401_calls') + except AttributeError: + pass + return r + def handle_401(self, r, **kwargs): """Takes the given response and tries digest-auth, if needed.""" @@ -194,4 +202,5 @@ class HTTPDigestAuth(AuthBase): except AttributeError: pass r.register_hook('response', self.handle_401) + r.register_hook('response', self.handle_302) return r From 6ff6f756c034ad0a946c711d0e136cfbd077ab62 Mon Sep 17 00:00:00 2001 From: Yossi Gottlieb Date: Wed, 8 Oct 2014 00:06:01 +0300 Subject: [PATCH 2/3] Clean up, support all redirects, fix potential endless 401 loop. --- requests/auth.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/requests/auth.py b/requests/auth.py index 41be9829..b2b341f5 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -17,6 +17,7 @@ from base64 import b64encode from .compat import urlparse, str from .cookies import extract_cookies_to_jar from .utils import parse_dict_header, to_native_string +from .status_codes import codes CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded' CONTENT_TYPE_MULTI_PART = 'multipart/form-data' @@ -150,13 +151,12 @@ class HTTPDigestAuth(AuthBase): return 'Digest %s' % (base) - def handle_302(self, r, **kwargs): + def handle_redirect(self, r, **kwargs): """Reset num_401_calls counter on redirects.""" - try: - delattr(self, 'num_401_calls') - except AttributeError: - pass - return r + if r.status_code in ( + codes.temporary_redirect, + codes.permanent_redirect): + setattr(self, 'num_401_calls', 1) def handle_401(self, r, **kwargs): """Takes the given response and tries digest-auth, if needed.""" @@ -190,7 +190,7 @@ class HTTPDigestAuth(AuthBase): return _r - setattr(self, 'num_401_calls', 1) + setattr(self, 'num_401_calls', num_401_calls + 1) return r def __call__(self, r): @@ -202,5 +202,5 @@ class HTTPDigestAuth(AuthBase): except AttributeError: pass r.register_hook('response', self.handle_401) - r.register_hook('response', self.handle_302) + r.register_hook('response', self.handle_redirect) return r From 3d8823cafc82fd9849eef6650a54a61554079b61 Mon Sep 17 00:00:00 2001 From: Yossi Gottlieb Date: Thu, 23 Oct 2014 13:46:44 +0300 Subject: [PATCH 3/3] Clean up handle_redirect. --- requests/auth.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/requests/auth.py b/requests/auth.py index b2b341f5..010919f3 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -153,9 +153,7 @@ class HTTPDigestAuth(AuthBase): def handle_redirect(self, r, **kwargs): """Reset num_401_calls counter on redirects.""" - if r.status_code in ( - codes.temporary_redirect, - codes.permanent_redirect): + if r.is_redirect: setattr(self, 'num_401_calls', 1) def handle_401(self, r, **kwargs):