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,31 +134,43 @@ class Authenticator: ...@@ -134,31 +134,43 @@ class Authenticator:
return credentials[0], credentials[1] return credentials[0], credentials[1]
def _get_credentials_for_netloc( def get_pypi_token(self, name: str) -> str:
self, netloc: str return self._password_manager.get_pypi_token(name)
) -> Tuple[Optional[str], Optional[str]]:
credentials = (None, None)
for repository_name in self._config.get("repositories", []): def get_http_auth(self, name: str) -> Optional[Dict[str, str]]:
repository_config = self._config.get(f"repositories.{repository_name}") return self._get_http_auth(name, None)
if not repository_config:
continue
url = repository_config.get("url") 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: if not url:
continue return
parsed_url = urllib.parse.urlsplit(url) parsed_url = urllib.parse.urlsplit(url)
if netloc == parsed_url.netloc: if netloc is None or netloc == parsed_url.netloc:
auth = self._password_manager.get_http_auth(repository_name) auth = self._password_manager.get_http_auth(name)
if auth is None or auth["password"] is None: if auth is None or auth["password"] is None:
username = auth["username"] if auth else None username = auth["username"] if auth else None
auth = self._get_credentials_for_netloc_from_keyring( auth = self._get_credentials_for_netloc_from_keyring(
url, netloc, username url, parsed_url.netloc, username
) )
return auth
def _get_credentials_for_netloc(
self, netloc: str
) -> Tuple[Optional[str], Optional[str]]:
credentials = (None, None)
for repository_name in self._config.get("repositories", []):
auth = self._get_http_auth(repository_name, netloc)
if auth is None: if auth is None:
continue continue
......
...@@ -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