Skip to content
Myr

KILL THE AUTH HANDLERS

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

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 68b4830 · Browse files · 2 files+6 added−117 removed
This commit

State

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

Diff stats

2 files+6 added−117 removed

  • other:+6 added−117 removed

2 files+6 added−117 removed

modified:requests/models.py+1 added-3 removed
22 unmodified lines
23from .utils import (dict_from_cookiejar, get_unicode_from_response, stream_decode_response_unicode, decode_gzip, stream_decode_gzip)24from .status_codes import codes25from .exceptions import Timeout, URLRequired, TooManyRedirects26from .monkeys import Request as _Request27from .monkeys import (28 HTTPBasicAuthHandler, HTTPForcedBasicAuthHandler,29 HTTPDigestAuthHandler, HTTPRedirectHandler)27from .monkeys import HTTPRedirectHandler2829from .auth import dispatch as auth_dispatch3031REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)
488 unmodified lines
modified:requests/monkeys.py+5 added-114 removed
27 unmodified lines
282930class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):31 """HTTP Redirect handler."""32 def http_error_301(self, req, fp, code, msg, headers):32 def _pass(self, req, fp, code, msg, headers):33 pass3435 http_error_302 = http_error_303 = http_error_307 = http_error_30135 http_error_302 = _pass36 http_error_303 = _pass37 http_error_307 = _pass38 http_error_301 = _pass39373839class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):40 """HTTP Basic Auth Handler with authentication loop fixes."""4142 def __init__(self, *args, **kwargs):43 urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)44 self.retried_req = None45 self.retried = 0464748 def reset_retry_count(self):49 # Python 2.6.5 will call this on 401 or 407 errors and thus loop50 # forever. We disable reset_retry_count completely and reset in51 # http_error_auth_reqed instead.52 pass535455 def http_error_auth_reqed(self, auth_header, host, req, headers):56 # Reset the retry counter once for each request.57 if req is not self.retried_req:58 self.retried_req = req59 self.retried = 06061 return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(62 self, auth_header, host, req, headers63 )64656667class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):68 """HTTP Basic Auth Handler with forced Authentication."""6970 auth_header = 'Authorization'71 rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'72 'realm=(["\'])(.*?)\\2', re.I)7374 def __init__(self, *args, **kwargs):75 HTTPBasicAuthHandler.__init__(self, *args, **kwargs)767778 def http_error_401(self, req, fp, code, msg, headers):79 url = req.get_full_url()80 response = self._http_error_auth_reqed('www-authenticate', url, req, headers)81 self.reset_retry_count()82 return response8384 http_error_404 = http_error_401858687 def _http_error_auth_reqed(self, authreq, host, req, headers):8889 authreq = headers.get(authreq, None)9091 if self.retried > 5:92 # retry sending the username:password 5 times before failing.93 raise urllib2.HTTPError(req.get_full_url(), 401, "basic auth failed",94 headers, None)95 else:96 self.retried += 19798 if authreq:99100 mo = self.rx.search(authreq)101102 if mo:103 scheme, quote, realm = mo.groups()104105 if scheme.lower() == 'basic':106 response = self.retry_http_basic_auth(host, req, realm)107108 if response and response.code not in (401, 404):109 self.retried = 0110 return response111 else:112 response = self.retry_http_basic_auth(host, req, 'Realm')113114 if response and response.code not in (401, 404):115 self.retried = 0116 return response117118119120class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):121122 def __init__(self, *args, **kwargs):123 urllib2.HTTPDigestAuthHandler.__init__(self, *args, **kwargs)124 self.retried_req = None125126 def reset_retry_count(self):127 # Python 2.6.5 will call this on 401 or 407 errors and thus loop128 # forever. We disable reset_retry_count completely and reset in129 # http_error_auth_reqed instead.130 pass131132 def http_error_auth_reqed(self, auth_header, host, req, headers):133 # Reset the retry counter once for each request.134 if req is not self.retried_req:135 self.retried_req = req136 self.retried = 0137 # In python < 2.5 AbstractDigestAuthHandler raises a ValueError if138 # it doesn't know about the auth type requested. This can happen if139 # somebody is using BasicAuth and types a bad password.140141 try:142 return urllib2.HTTPDigestAuthHandler.http_error_auth_reqed(143 self, auth_header, host, req, headers)144 except ValueError, inst:145 arg = inst.args[0]146 if arg.startswith("AbstractDigestAuthHandler doesn't know "):147 return148 raise