Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/poetry/utils/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ def authenticated_url(self, url: str) -> str:
if credential.username is not None and credential.password is not None:
username = urllib.parse.quote(credential.username, safe="")
password = urllib.parse.quote(credential.password, safe="")
# The netloc of a url that already contains credentials includes
# them, so strip any userinfo before adding the credentials back.
netloc = parsed.netloc.rsplit("@", 1)[-1]

return (
f"{parsed.scheme}://{username}:{password}@{parsed.netloc}{parsed.path}"
)
return f"{parsed.scheme}://{username}:{password}@{netloc}{parsed.path}"

return url

Expand Down
22 changes: 22 additions & 0 deletions tests/utils/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ def test_authenticator_uses_url_provided_credentials(
assert request.headers["Authorization"] == f"Basic {basic_auth}"


def test_authenticated_url_does_not_duplicate_credentials_from_url(
mock_config: Config,
) -> None:
authenticator = Authenticator(mock_config, NullIO())

# The netloc of a url that carries credentials includes them, so they must
# not be repeated in front of it.
assert (
authenticator.authenticated_url("https://foo001:bar002@foo.bar/simple/")
== "https://foo001:bar002@foo.bar/simple/"
)


def test_authenticated_url_adds_configured_credentials(mock_config: Config) -> None:
authenticator = Authenticator(mock_config, NullIO())

assert (
authenticator.authenticated_url("https://foo.bar/simple/")
== "https://bar:baz@foo.bar/simple/"
)


def test_authenticator_uses_credentials_from_config_if_not_provided(
mock_config: Config, mock_remote: None, http: responses.RequestsMock
) -> None:
Expand Down
Loading