Commit 5c7c9da3 by Dos Moonen

Refactored Authenticator and added two methods which delegate to PasswordManager…

Refactored Authenticator and added two methods which delegate to PasswordManager so that Publisher can use Authenticator instead of PasswordManager.

- Authenticator and its tests got moved into utils.
- No longer retrieve "repositories.{name}.url" in two steps ("repositories.{name}", then "url") since that does not allow us to use environment variable POETRY_REPOSITORIES_{name}_URL.
parent bea59682
...@@ -24,8 +24,8 @@ from poetry.utils.env import EnvCommandError ...@@ -24,8 +24,8 @@ from poetry.utils.env import EnvCommandError
from poetry.utils.helpers import safe_rmtree from poetry.utils.helpers import safe_rmtree
from poetry.utils.pip import pip_editable_install from poetry.utils.pip import pip_editable_install
from ..utils.authenticator import Authenticator
from ..utils.pip import pip_install from ..utils.pip import pip_install
from .authenticator import Authenticator
from .chef import Chef from .chef import Chef
from .chooser import Chooser from .chooser import Chooser
from .operations.install import Install from .operations.install import Install
......
...@@ -6,10 +6,9 @@ from typing import List ...@@ -6,10 +6,9 @@ from typing import List
from typing import Optional from typing import Optional
from typing import Union from typing import Union
from poetry.utils.helpers import get_cert from ..utils.authenticator import Authenticator
from poetry.utils.helpers import get_client_cert from ..utils.helpers import get_cert
from poetry.utils.password_manager import PasswordManager from ..utils.helpers import get_client_cert
from .uploader import Uploader from .uploader import Uploader
...@@ -32,7 +31,7 @@ class Publisher: ...@@ -32,7 +31,7 @@ class Publisher:
self._package = poetry.package self._package = poetry.package
self._io = io self._io = io
self._uploader = Uploader(poetry, io) self._uploader = Uploader(poetry, io)
self._password_manager = PasswordManager(poetry.config) self._authenticator = Authenticator(poetry.config, self._io)
@property @property
def files(self) -> List[Path]: def files(self) -> List[Path]:
...@@ -58,13 +57,13 @@ class Publisher: ...@@ -58,13 +57,13 @@ class Publisher:
if not (username and password): if not (username and password):
# Check if we have a token first # Check if we have a token first
token = self._password_manager.get_pypi_token(repository_name) token = self._authenticator.get_pypi_token(repository_name)
if token: if token:
logger.debug(f"Found an API token for {repository_name}.") logger.debug(f"Found an API token for {repository_name}.")
username = "__token__" username = "__token__"
password = token password = token
else: else:
auth = self._password_manager.get_http_auth(repository_name) auth = self._authenticator.get_http_auth(repository_name)
if auth: if auth:
logger.debug( logger.debug(
"Found authentication information for {}.".format( "Found authentication information for {}.".format(
......
...@@ -34,7 +34,7 @@ from poetry.utils.patterns import wheel_file_re ...@@ -34,7 +34,7 @@ from poetry.utils.patterns import wheel_file_re
from ..config.config import Config from ..config.config import Config
from ..inspection.info import PackageInfo from ..inspection.info import PackageInfo
from ..installation.authenticator import Authenticator from ..utils.authenticator import Authenticator
from .exceptions import PackageNotFound from .exceptions import PackageNotFound
from .exceptions import RepositoryError from .exceptions import RepositoryError
from .pypi_repository import PyPiRepository from .pypi_repository import PyPiRepository
......
...@@ -134,35 +134,47 @@ class Authenticator: ...@@ -134,35 +134,47 @@ class Authenticator:
return credentials[0], credentials[1] return credentials[0], credentials[1]
def get_pypi_token(self, name: str) -> str:
return self._password_manager.get_pypi_token(name)
def get_http_auth(self, name: str) -> Optional[Dict[str, str]]:
return self._get_http_auth(name, None)
def _get_http_auth(
self, name: str, netloc: Optional[str]
) -> Optional[Dict[str, str]]:
if name == "pypi":
url = "https://upload.pypi.org/legacy/"
else:
url = self._config.get(f"repositories.{name}.url")
if not url:
return
parsed_url = urllib.parse.urlsplit(url)
if netloc is None or netloc == parsed_url.netloc:
auth = self._password_manager.get_http_auth(name)
if auth is None or auth["password"] is None:
username = auth["username"] if auth else None
auth = self._get_credentials_for_netloc_from_keyring(
url, parsed_url.netloc, username
)
return auth
def _get_credentials_for_netloc( def _get_credentials_for_netloc(
self, netloc: str self, netloc: str
) -> Tuple[Optional[str], Optional[str]]: ) -> Tuple[Optional[str], Optional[str]]:
credentials = (None, None) credentials = (None, None)
for repository_name in self._config.get("repositories", []): for repository_name in self._config.get("repositories", []):
repository_config = self._config.get(f"repositories.{repository_name}") auth = self._get_http_auth(repository_name, netloc)
if not repository_config:
continue
url = repository_config.get("url") if auth is None:
if not url:
continue continue
parsed_url = urllib.parse.urlsplit(url) return auth["username"], auth["password"]
if netloc == parsed_url.netloc:
auth = self._password_manager.get_http_auth(repository_name)
if auth is None or auth["password"] is None:
username = auth["username"] if auth else None
auth = self._get_credentials_for_netloc_from_keyring(
url, netloc, username
)
if auth is None:
continue
return auth["username"], auth["password"]
return credentials return credentials
......
...@@ -7,7 +7,7 @@ import requests ...@@ -7,7 +7,7 @@ import requests
from cleo.io.null_io import NullIO from cleo.io.null_io import NullIO
from poetry.installation.authenticator import Authenticator from poetry.utils.authenticator import Authenticator
class SimpleCredential: class SimpleCredential:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment