From 870b5f3a9c74c6e528f37b242201a335e2e8ef50 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 2 Aug 2022 18:18:00 -0400 Subject: [PATCH] Add support for a scheme property in SecretService backend. Set to 'KeypassXC' to use the username/system convention from KeypassXC, either with KEYRING_PROPERTY_SCHEME or keyring.with_properties(scheme='KeypassXC'). Fixes #448. --- keyring/backend.py | 6 +++++ keyring/backends/SecretService.py | 41 +++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/keyring/backend.py b/keyring/backend.py index be393a7e..5dfe46a4 100644 --- a/keyring/backend.py +++ b/keyring/backend.py @@ -6,6 +6,7 @@ import abc import logging import operator +import copy from typing import Optional @@ -151,6 +152,11 @@ def parse(item): for name, value in props: setattr(self, name, value) + def with_properties(self, **kwargs): + alt = copy.copy(self) + vars(alt).update(kwargs) + return alt + class Crypter: """Base class providing encryption and decryption""" diff --git a/keyring/backends/SecretService.py b/keyring/backends/SecretService.py index cf1a3d4d..fa32d0e0 100644 --- a/keyring/backends/SecretService.py +++ b/keyring/backends/SecretService.py @@ -27,6 +27,12 @@ class Keyring(KeyringBackend): """Secret Service Keyring""" appid = 'Python keyring library' + scheme = 'default' + + schemes = dict( + default=dict(username='username', service='service'), + KeypassXC=dict(username='UserName', service='Title'), + ) @properties.ClassProperty @classmethod @@ -73,11 +79,24 @@ def unlock(self, item): if item.is_locked(): # User dismissed the prompt raise KeyringLocked('Failed to unlock the item!') + def _query(self, service, username): + scheme = self.schemes[self.scheme] + return ( + { + scheme['username']: username, + scheme['service']: service, + } + if username + else { + scheme['service']: service, + } + ) + def get_password(self, service, username): """Get password of the username for the service""" collection = self.get_preferred_collection() with closing(collection.connection): - items = collection.search_items({"username": username, "service": service}) + items = collection.search_items(self._query(service, username)) for item in items: self.unlock(item) return item.get_secret().decode('utf-8') @@ -85,11 +104,10 @@ def get_password(self, service, username): def set_password(self, service, username, password): """Set password for the username of the service""" collection = self.get_preferred_collection() - attributes = { - "application": self.appid, - "service": service, - "username": username, - } + attributes = dict( + self._query(service, username), + application=self.appid, + ) label = "Password for '{}' on '{}'".format(username, service) with closing(collection.connection): collection.create_item(label, attributes, password, replace=True) @@ -98,7 +116,7 @@ def delete_password(self, service, username): """Delete the stored password (only the first one)""" collection = self.get_preferred_collection() with closing(collection.connection): - items = collection.search_items({"username": username, "service": service}) + items = collection.search_items(self._query(service, username)) for item in items: return item.delete() raise PasswordDeleteError("No such password!") @@ -111,16 +129,13 @@ def get_credential(self, service, username): and return a SimpleCredential containing the username and password Otherwise, it will return the first username and password combo that it finds. """ - - query = {"service": service} - if username: - query["username"] = username - + scheme = self.schemes[self.scheme] + query = self._query(service, username) collection = self.get_preferred_collection() with closing(collection.connection): items = collection.search_items(query) for item in items: self.unlock(item) - username = item.get_attributes().get("username") + username = item.get_attributes().get(scheme['username']) return SimpleCredential(username, item.get_secret().decode('utf-8'))