Skip to content

Commit a90dd11

Browse files
authored
Merge pull request #10033 from snook92/multi_cred_index_url
2 parents 6ad334b + c22e15e commit a90dd11

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

news/3931.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

src/pip/_internal/network/auth.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,12 @@ def _get_url_and_credentials(self, original_url):
170170
"""
171171
url, netloc, _ = split_auth_netloc_from_url(original_url)
172172

173-
# Use any stored credentials that we have for this netloc
174-
username, password = self.passwords.get(netloc, (None, None))
173+
# Try to get credentials from original url
174+
username, password = self._get_new_credentials(original_url)
175175

176+
# If credentials not found, use any stored credentials for this netloc
176177
if username is None and password is None:
177-
# No stored credentials. Acquire new credentials without prompting
178-
# the user. (e.g. from netrc, keyring, or the URL itself)
179-
username, password = self._get_new_credentials(original_url)
178+
username, password = self.passwords.get(netloc, (None, None))
180179

181180
if username is not None or password is not None:
182181
# Convert the username and password if they're None, so that

tests/unit/test_network_auth.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,29 @@ def test_get_credentials_parses_correctly(input_url, url, username, password):
4747
)
4848

4949

50-
def test_get_credentials_uses_cached_credentials():
50+
def test_get_credentials_not_to_uses_cached_credentials():
5151
auth = MultiDomainBasicAuth()
5252
auth.passwords['example.com'] = ('user', 'pass')
5353

5454
got = auth._get_url_and_credentials("http://foo:[email protected]/path")
55+
expected = ('http://example.com/path', 'foo', 'bar')
56+
assert got == expected
57+
58+
59+
def test_get_credentials_not_to_uses_cached_credentials_only_username():
60+
auth = MultiDomainBasicAuth()
61+
auth.passwords['example.com'] = ('user', 'pass')
62+
63+
got = auth._get_url_and_credentials("http://[email protected]/path")
64+
expected = ('http://example.com/path', 'foo', '')
65+
assert got == expected
66+
67+
68+
def test_get_credentials_uses_cached_credentials():
69+
auth = MultiDomainBasicAuth()
70+
auth.passwords['example.com'] = ('user', 'pass')
71+
72+
got = auth._get_url_and_credentials("http://example.com/path")
5573
expected = ('http://example.com/path', 'user', 'pass')
5674
assert got == expected
5775

0 commit comments

Comments
 (0)