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 basic auth credentials in --extra-index-urls when using multiple credentials for same domain #10033

Merged
merged 8 commits into from
Jun 11, 2021
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/3931.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prefer credentials from the URL over the previously-obtained credentials from URLs of the same domain, so it is possible to use different credentials on the same index server for different ``--extra-index-url`` options.
9 changes: 4 additions & 5 deletions src/pip/_internal/network/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@ def _get_url_and_credentials(self, original_url):
"""
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))
# 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
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)
username, password = self.passwords.get(netloc, (None, None))
uranusjr marked this conversation as resolved.
Show resolved Hide resolved

if username is not None or password is not None:
# Convert the username and password if they're None, so that
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/test_network_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,29 @@ def test_get_credentials_parses_correctly(input_url, url, username, password):
)


def test_get_credentials_uses_cached_credentials():
def test_get_credentials_not_to_uses_cached_credentials():
uranusjr marked this conversation as resolved.
Show resolved Hide resolved
auth = MultiDomainBasicAuth()
auth.passwords['example.com'] = ('user', 'pass')

got = auth._get_url_and_credentials("http://foo:bar@example.com/path")
expected = ('http://example.com/path', 'foo', 'bar')
assert got == expected


def test_get_credentials_not_to_uses_cached_credentials_only_username():
auth = MultiDomainBasicAuth()
auth.passwords['example.com'] = ('user', 'pass')

got = auth._get_url_and_credentials("http://foo@example.com/path")
expected = ('http://example.com/path', 'foo', '')
assert got == expected


def test_get_credentials_uses_cached_credentials():
auth = MultiDomainBasicAuth()
auth.passwords['example.com'] = ('user', 'pass')

got = auth._get_url_and_credentials("http://example.com/path")
expected = ('http://example.com/path', 'user', 'pass')
assert got == expected

Expand Down