Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Credentials error when working with 2 indices on the same host #2334

Merged
merged 1 commit into from
Oct 25, 2023
Merged
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
1 change: 1 addition & 0 deletions news/2333.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a credentials error when working with two indices on the same host
13 changes: 6 additions & 7 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"virtualenv>=20",
"pyproject-hooks",
"requests-toolbelt",
"unearth>=0.10.0",
"unearth>=0.12.1",
"findpython>=0.4.0,<1.0.0a0",
"tomlkit>=0.11.1,<1",
"shellingham>=1.3.2",
Expand Down
28 changes: 19 additions & 9 deletions src/pdm/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import urllib.parse

from unearth.auth import MaybeAuth, MultiDomainBasicAuth, get_keyring_provider
from unearth.utils import split_auth_from_netloc
from unearth.utils import commonprefix, split_auth_from_url

from pdm._types import RepositoryConfig
from pdm.exceptions import PdmException
Expand All @@ -22,18 +22,28 @@
self.sources = sources
self.ui = ui

def _get_auth_from_index_url(self, netloc: str) -> tuple[MaybeAuth, str | None]:
def _get_auth_from_index_url(self, url: str) -> tuple[MaybeAuth, str | None]:
if not self.sources:
return None, None

target = urllib.parse.urlsplit(url.rstrip("/") + "/")
candidates: list[tuple[MaybeAuth, str, urllib.parse.SplitResult]] = []
for source in self.sources:
assert source.url
parsed = urllib.parse.urlparse(source.url)
auth, index_netloc = split_auth_from_netloc(parsed.netloc)
if index_netloc == netloc:
if source.username:
auth = (source.username, source.password)
return auth, source.url
return None, None
index = source.url.rstrip("/") + "/"
auth, url_no_auth = split_auth_from_url(index)
parsed = urllib.parse.urlparse(url_no_auth)
if source.username:
auth = (source.username, source.password)
if parsed == target:
return auth, index

Check warning on line 39 in src/pdm/models/auth.py

View check run for this annotation

Codecov / codecov/patch

src/pdm/models/auth.py#L39

Added line #L39 was not covered by tests
if parsed.netloc == target.netloc:
candidates.append((auth, index, parsed))

if not candidates:
return None, None
auth, index, _ = max(candidates, key=lambda x: commonprefix(x[2].path, target.path).rfind("/"))
return auth, index

def _prompt_for_password(self, netloc: str) -> tuple[str | None, str | None, bool]:
if self.ui.verbosity < Verbosity.DETAIL:
Expand Down
Loading