Forced Basic Auth for 404'd resources
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 7d91d3f · Browse files · 1 file·+59 added−2 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+59 added−2 removed
- other:+59 added−2 removed
Provenance
- Commit
30d699b- Tree
19ff670- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
7d91d3f
1 file changed+59 added−2 removed
modified:requests/monkeys.py+59 added-2 removed
7 unmodified lines8 9 """10 11 import urllib212+import re13 13-14 class Request(urllib2.Request):15 """Hidden wrapper around the urllib2.Request object. Allows for manual16 setting of HTTP methods.17 """7 unmodified lines25 return self.method26 27 return urllib2.Request.get_method(self)28 29-class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):29 30+class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):31+ """HTTP Redirect handler."""32 def http_error_301(self, req, fp, code, msg, headers):33 pass34 35 http_error_302 = http_error_303 = http_error_307 = http_error_30136 37 38 39 class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):40+ """HTTP Basic Auth Handler with authentication loop fixes."""41 42 def __init__(self, *args, **kwargs):43 urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)44 self.retried_req = None45 46+47 def reset_retry_count(self):48 # Python 2.6.5 will call this on 401 or 407 errors and thus loop49 # forever. We disable reset_retry_count completely and reset in50 # http_error_auth_reqed instead.51 pass52 53+54 def http_error_auth_reqed(self, auth_header, host, req, headers):55 # Reset the retry counter once for each request.56 if req is not self.retried_req:57 self.retried_req = req4 unmodified lines62 )63 64 65 66+class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):67+ """HTTP Basic Auth Handler with forced Authentication."""68+69+ auth_header = 'Authorization'70+ rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'71+ 'realm=(["\'])(.*?)\\2', re.I)72+73+ def __init__(self, *args, **kwargs):74+ HTTPBasicAuthHandler.__init__(self, *args, **kwargs)75+76+77+ def http_error_401(self, req, fp, code, msg, headers):78+ url = req.get_full_url()79+ response = self._http_error_auth_reqed('www-authenticate', url, req, headers)80+ self.reset_retry_count()81+ return response82+83+ http_error_404 = http_error_40184+85+86+ def _http_error_auth_reqed(self, authreq, host, req, headers):87+88+ authreq = headers.get(authreq, None)89+90+ if self.retried > 5:91+ # retry sending the username:password 5 times before failing.92+ raise urllib2.HTTPError(req.get_full_url(), 401, "basic auth failed",93+ headers, None)94+ else:95+ self.retried += 196+97+ if authreq:98+99+ mo = self.rx.search(authreq)100+101+ if mo:102+ scheme, quote, realm = mo.groups()103+104+ if scheme.lower() == 'basic':105+ response = self.retry_http_basic_auth(host, req, realm)106+107+ if response and response.code not in (401, 404):108+ self.retried = 0109+ return response110+ else:111+ response = self.retry_http_basic_auth(host, req, 'Realm')112+113+ if response and response.code not in (401, 404):114+ self.retried = 0115+ return response116+117+118+119 class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):120 121 def __init__(self, *args, **kwargs):122 urllib2.HTTPDigestAuthHandler.__init__(self, *args, **kwargs)25 unmodified lines