Skip to content

Commit

Permalink
Merge pull request #459 from muggenhor/feat/kwallet-cred-without-user…
Browse files Browse the repository at this point in the history
…name

feat: support discovering a credential's username on KWallet too
  • Loading branch information
jaraco committed Aug 30, 2020
2 parents 8712287 + 3883de2 commit c0ec24c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v21.4.0
-------

* #431: KWallet backend now supports ``get_credential``.

v21.3.1
-------

Expand Down
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 @@ -113,6 +114,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"""
if not self.connected(service):
Expand Down

0 comments on commit c0ec24c

Please sign in to comment.