def _get_new_credentials( self, original_url: str, *, allow_netrc: bool = False, allow_keyring: bool = False, ) -> AuthInfo: """Find and return credentials for the specified URL.""" # Split the credentials and netloc from the url. url, netloc, url_user_password = split_auth_netloc_from_url( original_url, ) # Start with the credentials embedded in the url username, password = url_user_password if username is not None and password is not None: logger.debug("Found credentials in url for %s", netloc) return url_user_password # Find a matching index url for this request index_url = self._get_index_url(url) if index_url: # Split the credentials from the url. index_info = split_auth_netloc_from_url(index_url) if index_info: index_url, _, index_url_user_password = index_info logger.debug("Found index url %s", index_url) # If an index URL was found, try its embedded credentials if index_url and index_url_user_password[0] is not None: username, password = index_url_user_password if username is not None and password is not None: logger.debug("Found credentials in index url for %s", netloc) return index_url_user_password # Get creds from netrc if we still don't have them if allow_netrc: netrc_auth = get_netrc_auth(original_url) if netrc_auth: logger.debug("Found credentials in netrc for %s", netloc) return netrc_auth # If we don't have a password and keyring is available, use it. if allow_keyring: # The index url is more specific than the netloc, so try it first # fmt: off kr_auth = (get_keyring_auth(index_url, username) or get_keyring_auth(netloc, username)) # fmt: on if kr_auth: logger.debug("Found credentials in keyring for %s", netloc) return kr_auth return username, password
def _get_url_and_credentials(self, original_url): """Return the credentials to use for the provided URL. If allowed, netrc and keyring may be used to obtain the correct credentials. Returns (url_without_credentials, username, password). Note that even if the original URL contains credentials, this function may return a different username and password. """ url, netloc, _ = split_auth_netloc_from_url(original_url) # Use any stored credentials that we have for this netloc username, password = self.passwords.get(netloc, (None, None)) # If nothing cached, acquire new credentials without prompting # the user (e.g. from netrc, keyring, or similar). if username is None or password is None: username, password = self._get_new_credentials(original_url) if username is not None and password is not None: # Store the username and password self.passwords[netloc] = (username, password) return url, username, password
def _get_url_and_credentials( self, original_url: str) -> Tuple[str, Optional[str], Optional[str]]: """Return the credentials to use for the provided URL. If allowed, netrc and keyring may be used to obtain the correct credentials. Returns (url_without_credentials, username, password). Note that even if the original URL contains credentials, this function may return a different username and password. """ url, netloc, _ = split_auth_netloc_from_url(original_url) # Try to get credentials from original url username, password = self._get_new_credentials(original_url) # If credentials not found, use any stored credentials for this netloc. # Do this if either the username or the password is missing. # This accounts for the situation in which the user has specified # the username in the index url, but the password comes from keyring. if (username is None or password is None) and netloc in self.passwords: un, pw = self.passwords[netloc] # It is possible that the cached credentials are for a different username, # in which case the cache should be ignored. if username is None or username == un: username, password = un, pw if username is not None or password is not None: # Convert the username and password if they're None, so that # this netloc will show up as "cached" in the conditional above. # Further, HTTPBasicAuth doesn't accept None, so it makes sense to # cache the value that is going to be used. username = username or "" password = password or "" # Store any acquired credentials. self.passwords[netloc] = (username, password) assert ( # Credentials were found (username is not None and password is not None) # Credentials were not found or (username is None and password is None) ), f"Could not load credentials from url: {original_url}" return url, username, password
def _get_url_and_credentials(self, original_url): # type: (str) -> Tuple[str, Optional[str], Optional[str]] """Return the credentials to use for the provided URL. If allowed, netrc and keyring may be used to obtain the correct credentials. Returns (url_without_credentials, username, password). Note that even if the original URL contains credentials, this function may return a different username and password. """ url, netloc, _ = split_auth_netloc_from_url(original_url) # Use any stored credentials that we have for this netloc username, password = self.passwords.get(netloc, (None, None)) if username is None and password is None: # No stored credentials. Acquire new credentials without prompting # the user. (e.g. from netrc, keyring, or the URL itself) username, password = self._get_new_credentials(original_url) if username is not None or password is not None: # Convert the username and password if they're None, so that # this netloc will show up as "cached" in the conditional above. # Further, HTTPBasicAuth doesn't accept None, so it makes sense to # cache the value that is going to be used. username = username or "" password = password or "" # Store any acquired credentials. self.passwords[netloc] = (username, password) assert ( # Credentials were found (username is not None and password is not None) or # Credentials were not found (username is None and password is None) ), f"Could not load credentials from url: {original_url}" return url, username, password
def test_split_auth_netloc_from_url(url, expected): actual = split_auth_netloc_from_url(url) assert actual == expected
return u <<<<<<< HEAD return None def _get_new_credentials(self, original_url, allow_netrc=True, allow_keyring=True): # type: (str, bool, bool) -> AuthInfo ======= def _get_new_credentials(self, original_url, allow_netrc=True, allow_keyring=True): >>>>>>> b66a76afa15ab74019740676a52a071b85ed8f71 """Find and return credentials for the specified URL.""" # Split the credentials and netloc from the url. url, netloc, url_user_password = split_auth_netloc_from_url( original_url, ) # Start with the credentials embedded in the url username, password = url_user_password if username is not None and password is not None: logger.debug("Found credentials in url for %s", netloc) return url_user_password # Find a matching index url for this request index_url = self._get_index_url(url) if index_url: # Split the credentials from the url. index_info = split_auth_netloc_from_url(index_url) if index_info: index_url, _, index_url_user_password = index_info
def test_split_auth_netloc_from_url(url, expected): actual = split_auth_netloc_from_url(url) assert actual == expected
def _get_new_credentials(self, original_url, allow_netrc=True, allow_keyring=True): """Find and return credentials for the specified URL.""" # Split the credentials and netloc from the url. url, netloc, url_user_password = split_auth_netloc_from_url( original_url) # Start with the credentials embedded in the url username, password = url_user_password if username is not None and password is not None: logger.debug("Found credentials in url for %s", netloc) return url_user_password # Find a matching index url for this request index_url = self._get_index_url(url) if index_url: # Split the credentials from the url. index_info = split_auth_netloc_from_url(index_url) if index_info: index_url, _, index_url_user_password = index_info logger.debug("Found index url %s", index_url) # If an index URL was found, try its embedded credentials if index_url and index_url_user_password[0] is not None: username, password = index_url_user_password if username is not None and password is not None: logger.debug("Found credentials in index url for %s", netloc) return index_url_user_password # Check for creds in environment user_var = 'PIP_USERNAME' passwd_var = 'PIP_PASSWORD' msg = "Found '{fv}' env variable, but not '{nv}'. Ignoring '{fv}'." env_username = os.environ.get(user_var) env_password = os.environ.get(passwd_var) if env_username: if env_password: logger.debug("Found credentials in environment.") return env_username, env_password logger.warning(msg.format(fv=user_var, nv=passwd_var)) if env_password: logger.warning(msg.format(fv=passwd_var, nv=user_var)) # Get creds from netrc if we still don't have them if allow_netrc: netrc_auth = get_netrc_auth(original_url) if netrc_auth: logger.debug("Found credentials in netrc for %s", netloc) return netrc_auth # If we don't have a password and keyring is available, use it. if allow_keyring: # The index url is more specific than the netloc, so try it first kr_auth = (_get_keyring_auth(index_url, username) or _get_keyring_auth(netloc, username)) if kr_auth: logger.debug("Found credentials in keyring for %s", netloc) return kr_auth return None, None
def test_split_auth_netloc_from_url( url: str, expected: Tuple[str, str, Tuple[Optional[str], Optional[str]]]) -> None: actual = split_auth_netloc_from_url(url) assert actual == expected