Skip to content

Commit

Permalink
feat: support discovering a credential's username on KWallet too
Browse files Browse the repository at this point in the history
Implement the get_credential method for the KWallet backend. This now
allows lookup of a credential by 'service' name only, not requiring the
'username'.

Implements #431
  • Loading branch information
muggenhor committed Aug 24, 2020
1 parent a6980e4 commit 0b7b41f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions keyring/backends/kwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import contextlib

from ..backend import KeyringBackend
from ..credentials import SimpleCredential
from ..errors import PasswordDeleteError
from ..errors import PasswordSetError, InitError, KeyringLocked
from ..util import properties
Expand Down Expand Up @@ -114,6 +115,26 @@ def get_password(self, service, username):
password = self.iface.readPassword(self.handle, service, username, self.appid)
return str(password)

def get_credential(self, service, username):
"""Gets the first username and password for a service.
Returns a Credential instance
The username can be omitted, but if there is one, it will forward to
get_password.
Otherwise, it will return the first username and password combo that it finds.
"""
if username is not None:
return self.get_password(service, username)
if not self.connected(service):
# the user pressed "cancel" when prompted to unlock their keyring.
raise KeyringLocked("Failed to unlock the keyring!")

for username in self.iface.entryList(self.handle, service, self.appid):
password = self.iface.readPassword(
self.handle, service, username, self.appid
)
return SimpleCredential(str(username), str(password))

def set_password(self, service, username, password):
"""Set password for the username of the service
"""
Expand Down

0 comments on commit 0b7b41f

Please sign in to comment.