29
def _find_passwords(self, service, username, deleting=False):
30
from gi.repository import GnomeKeyring
33
service = self._safe_string(service)
34
username = self._safe_string(username)
35
for attrs_tuple in (('username', 'service'), ('user', 'domain')):
36
attrs = GnomeKeyring.Attribute.list_new()
37
GnomeKeyring.Attribute.list_append_string(attrs, attrs_tuple[0], username)
38
GnomeKeyring.Attribute.list_append_string(attrs, attrs_tuple[1], service)
39
result, items = GnomeKeyring.find_items_sync(
40
GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs)
41
if result == GnomeKeyring.Result.OK:
44
if result == GnomeKeyring.Result.CANCELLED:
45
raise PasswordDeleteError("Cancelled by user")
46
elif result != GnomeKeyring.Result.NO_MATCH:
47
raise PasswordDeleteError(result.value_name)
26
50
def get_password(self, service, username):
27
51
"""Get password of the username for the service
29
from gi.repository import GnomeKeyring
31
service = self._safe_string(service)
32
username = self._safe_string(username)
33
attrs = GnomeKeyring.Attribute.list_new()
34
GnomeKeyring.Attribute.list_append_string(attrs, 'user', username)
35
GnomeKeyring.Attribute.list_append_string(attrs, 'domain', service)
36
result, items = GnomeKeyring.find_items_sync(
37
GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs)
38
if result == GnomeKeyring.Result.IO_ERROR:
40
if result == GnomeKeyring.Result.NO_MATCH:
42
if result == GnomeKeyring.Result.CANCELLED:
43
# The user pressed "Cancel" when prompted to unlock their keyring.
46
assert len(items) == 1, 'no more than one entry should ever match'
53
items = self._find_passwords(service, username)
47
57
secret = items[0].secret
48
58
return secret if isinstance(secret, unicode) else secret.decode('utf-8')
56
66
username = self._safe_string(username)
57
67
password = self._safe_string(password)
58
68
attrs = GnomeKeyring.Attribute.list_new()
59
GnomeKeyring.Attribute.list_append_string(attrs, 'user', username)
60
GnomeKeyring.Attribute.list_append_string(attrs, 'domain', service)
69
GnomeKeyring.Attribute.list_append_string(attrs, 'username', username)
70
GnomeKeyring.Attribute.list_append_string(attrs, 'service', service)
71
GnomeKeyring.Attribute.list_append_string(attrs, 'application', 'python-keyring')
61
72
result = GnomeKeyring.item_create_sync(
62
73
self.KEYRING_NAME, GnomeKeyring.ItemType.NETWORK_PASSWORD,
63
74
"Password for '%s' on '%s'" % (username, service),
65
76
if result == GnomeKeyring.Result.CANCELLED:
66
77
# The user pressed "Cancel" when prompted to unlock their keyring.
67
78
raise PasswordSetError("Cancelled by user")
79
elif result != GnomeKeyring.Result.OK:
80
raise PasswordSetError(result.value_name)
69
82
def delete_password(self, service, username):
70
83
"""Delete the password for the username of the service.
72
85
from gi.repository import GnomeKeyring
73
attrs = GnomeKeyring.Attribute.list_new()
74
GnomeKeyring.Attribute.list_append_string(attrs, 'user', username)
75
GnomeKeyring.Attribute.list_append_string(attrs, 'domain', service)
76
result, items = GnomeKeyring.find_items_sync(
77
GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs)
78
if result == GnomeKeyring.Result.NO_MATCH:
86
items = self._find_passwords(service, username, deleting=True)
79
88
raise PasswordDeleteError("Password not found")
80
89
for current in items:
81
90
result = GnomeKeyring.item_delete_sync(current.keyring,
83
92
if result == GnomeKeyring.Result.CANCELLED:
84
93
raise PasswordDeleteError("Cancelled by user")
94
elif result != GnomeKeyring.Result.OK:
95
raise PasswordDeleteError(result.value_name)
86
97
def _safe_string(self, source, encoding='utf-8'):
87
98
"""Convert unicode to string as gnomekeyring barfs on unicode"""