def _make_opener(self, realm, base_url, username, password): """HTTP Basic Auth and cookie support for token verification.""" auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(realm=realm, uri=base_url, user=username, passwd=password) opener = build_opener(auth_handler) install_opener(opener) cookie_jar = CookieJar() cookie_handler = HTTPCookieProcessor(cookie_jar) handlers = [auth_handler, cookie_handler] opener = build_opener(*handlers) return opener
def srtm_login_or_skip(monkeypatch): import os try: srtm_username = os.environ['SRTM_USERNAME'] except KeyError: pytest.skip('SRTM_USERNAME environment variable is unset.') try: srtm_password = os.environ['SRTM_PASSWORD'] except KeyError: pytest.skip('SRTM_PASSWORD environment variable is unset.') from six.moves.urllib.request import (HTTPBasicAuthHandler, HTTPCookieProcessor, HTTPPasswordMgrWithDefaultRealm, build_opener) from six.moves.http_cookiejar import CookieJar password_manager = HTTPPasswordMgrWithDefaultRealm() password_manager.add_password( None, "https://urs.earthdata.nasa.gov", srtm_username, srtm_password) cookie_jar = CookieJar() opener = build_opener(HTTPBasicAuthHandler(password_manager), HTTPCookieProcessor(cookie_jar)) monkeypatch.setattr(cartopy.io, 'urlopen', opener.open)
def __init__(self, *args, **kwargs): """Initialize the Basic Auth handler. Args: *args (tuple): Positional arguments to pass to the parent class. **kwargs (dict): Keyword arguments to pass to the parent class. """ HTTPBasicAuthHandler.__init__(self, *args, **kwargs) self._tried_login = False self._otp_token_method = None self._otp_token_attempts = 0 self._last_otp_token = None
def _make_opener(self, realm, base_url, username, password): """uTorrent API need HTTP Basic Auth and cookie support for token verify.""" auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(realm=realm, uri=base_url, user=username, passwd=password) opener = build_opener(auth_handler) install_opener(opener) cookie_jar = CookieJar() cookie_handler = HTTPCookieProcessor(cookie_jar) handlers = [auth_handler, cookie_handler] opener = build_opener(*handlers) return opener
def http_error_auth_reqed(self, authreq, host, req, headers): """Handle an HTTP 401 Unauthorized from an API request. This will start by checking whether a two-factor authentication token is required by the server, and which method it will be sent by (SMS or token generator application), before handing back to the parent class, which will then call into our custom :py:meth:`retry_http_basic_auth`. Args: authreq (unicode): The authentication request type. host (unicode): The URL being accessed. req (rbtools.api.request.Request): The API request being made. headers (dict): The headers sent in the Unauthorized error response. Returns: httplib.HTTPResponse: If attempting another request, this will be the HTTP response from that request. This will be ``None`` if not making another request. Raises: urllib2.URLError: The HTTP request resulted in an error. If this is an :http:`401`, it may be handled by this class again. """ otp_header = headers.get(self.OTP_TOKEN_HEADER, '') if otp_header and otp_header.startswith('required'): try: self._otp_token_method = otp_header.split(';')[1].strip() except IndexError: logging.error( 'Invalid %s header value: "%s". This header ' 'is needed for two-factor authentication to ' 'work. Please report this!', self.OTP_TOKEN_HEADER, otp_header) return None return HTTPBasicAuthHandler.http_error_auth_reqed( self, authreq, host, req, headers)
def http_error_auth_reqed(self, authreq, host, req, headers): """Handle an HTTP 401 Unauthorized from an API request. This will start by checking whether a two-factor authentication token is required by the server, and which method it will be sent by (SMS or token generator application), before handing back to the parent class, which will then call into our custom :py:meth:`retry_http_basic_auth`. Args: authreq (unicode): The authentication request type. host (unicode): The URL being accessed. req (rbtools.api.request.Request): The API request being made. headers (dict): The headers sent in the Unauthorized error response. Returns: httplib.HTTPResponse: If attempting another request, this will be the HTTP response from that request. This will be ``None`` if not making another request. Raises: urllib2.URLError: The HTTP request resulted in an error. If this is an :http:`401`, it may be handled by this class again. """ otp_header = headers.get(self.OTP_TOKEN_HEADER, '') if otp_header and otp_header.startswith('required'): try: self._otp_token_method = otp_header.split(';')[1].strip() except IndexError: logging.error('Invalid %s header value: "%s". This header ' 'is needed for two-factor authentication to ' 'work. Please report this!', self.OTP_TOKEN_HEADER, otp_header) return None return HTTPBasicAuthHandler.http_error_auth_reqed( self, authreq, host, req, headers)
def __init__(self, *args, **kwargs): HTTPBasicAuthHandler.__init__(self, *args, **kwargs) self._retried = False self._lasturl = "" self._needs_otp_token = False self._otp_token_attempts = 0