Skip to content

Commit

Permalink
fix: passing a username not associated to a credential to get_credent…
Browse files Browse the repository at this point in the history
…ial for WinVaultKeyring returns None
  • Loading branch information
Jamie Beverley committed Oct 3, 2024
1 parent 24ec25d commit 27d0e0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions keyring/backends/Windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def get_credential(self, service, username):
res = self._get_password(self._compound_name(username, service))
# get any first password under the service name
if not res:
res = self._get_password(service)
if not res:
return None
cred = self._get_password(service)
res = cred if username is None or username == cred["UserName"] else None
if not res:
return None
return SimpleCredential(res['UserName'], res.value)
18 changes: 18 additions & 0 deletions keyring/testing/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,21 @@ def test_new_with_properties(self):
assert alt.foo == 'bar'
with pytest.raises(AttributeError):
self.keyring.foo # noqa: B018

def test_wrong_username_returns_none(self):
keyring = self.keyring
service = 'test_wrong_username_returns_none'
cred = keyring.get_credential(service, None)
assert cred is None

password_1 = 'password1'
password_2 = 'password2'
self.set_password(service, 'user1', password_1)
self.set_password(service, 'user2', password_2)

assert keyring.get_credential(service, "user1").password == password_1
assert keyring.get_credential(service, "user2").password == password_2
# Most recent entered
assert keyring.get_credential(service, None).password == password_2
# Missing/wrong username should not return a cred
assert keyring.get_credential(service, "nobody!") is None

0 comments on commit 27d0e0e

Please sign in to comment.