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

feat: support discovering a credential's username on KWallet too #459

Merged
merged 2 commits into from
Aug 30, 2020
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
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 @@ -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