From 38e3c4952f9436aad182b2253b94380f6cdcd1d2 Mon Sep 17 00:00:00 2001 From: mls Date: Tue, 26 Apr 2022 12:36:16 +0200 Subject: [PATCH] Simplify bad auth retry workaround needed for old python versions This changes the code back to retrying up to 5 times for old python version 2.6.6-2.7.9. The complete backport of the basic auth changes clutters up the code way to much for such a little gain. (This basically reverts commit 326abe0c8bf3fd8ba1a0684a84048d8764319352) --- osc/conf.py | 42 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/osc/conf.py b/osc/conf.py index 8cd6ceb3..80db0ace 100644 --- a/osc/conf.py +++ b/osc/conf.py @@ -524,6 +524,13 @@ def _build_opener(apiurl): return super(self.__class__, self).retry_http_basic_auth(host, req, realm) + def http_error_401(self, req, fp, code, msg, headers): + response = super(self.__class__, self).http_error_401(req, fp, code, msg, headers) + # workaround for http://bugs.python.org/issue9639 + if hasattr(self, 'retried'): + self.retried = 0 + return response + if 'last_opener' not in _build_opener.__dict__: _build_opener.last_opener = (None, None) @@ -538,43 +545,10 @@ def _build_opener(apiurl): # read proxies from env proxyhandler = ProxyHandler() - authhandler_class = OscHTTPBasicAuthHandler - # workaround for http://bugs.python.org/issue9639 - if sys.version_info >= (2, 6, 6) and sys.version_info < (2, 7, 9): - class OscHTTPBasicAuthHandlerCompat(OscHTTPBasicAuthHandler): - # The following two functions were backported from upstream 2.7. - def http_error_auth_reqed(self, authreq, host, req, headers): - authreq = headers.get(authreq, None) - - if authreq: - mo = AbstractBasicAuthHandler.rx.search(authreq) - if mo: - scheme, quote, realm = mo.groups() - if quote not in ['"', "'"]: - warnings.warn("Basic Auth Realm was unquoted", - UserWarning, 2) - if scheme.lower() == 'basic': - return self.retry_http_basic_auth(host, req, realm) - - def retry_http_basic_auth(self, host, req, realm): - self._rewind_request(req) - user, pw = self.passwd.find_user_password(realm, host) - if pw is not None: - raw = "%s:%s" % (user, pw) - auth = 'Basic %s' % base64.b64encode(raw).strip() - if req.get_header(self.auth_header, None) == auth: - return None - req.add_unredirected_header(self.auth_header, auth) - return self.parent.open(req, timeout=req.timeout) - else: - return None - - authhandler_class = OscHTTPBasicAuthHandlerCompat - options = config['api_host_options'][apiurl] # with None as first argument, it will always use this username/password # combination for urls for which arg2 (apisrv) is a super-url - authhandler = authhandler_class( \ + authhandler = OscHTTPBasicAuthHandler( \ HTTPPasswordMgrWithDefaultRealm()) authhandler.add_password(None, apiurl, options['user'], options['pass'])