1
1
import logging
2
2
3
+ from .. import backend
3
4
from ..util import properties
4
5
from ..backend import KeyringBackend
5
6
from ..credentials import SimpleCredential
26
27
log = logging .getLogger (__name__ )
27
28
28
29
29
- class Keyring (KeyringBackend ):
30
+ class Keyring (backend . SchemeSelectable , KeyringBackend ):
30
31
"""libsecret Keyring"""
31
32
32
33
appid = 'Python keyring library'
33
- if available :
34
- schema = Secret .Schema .new (
34
+
35
+ @property
36
+ def schema (self ):
37
+ return Secret .Schema .new (
35
38
"org.freedesktop.Secret.Generic" ,
36
39
Secret .SchemaFlags .NONE ,
37
- {
38
- "application" : Secret .SchemaAttributeType .STRING ,
39
- "service" : Secret .SchemaAttributeType .STRING ,
40
- "username" : Secret .SchemaAttributeType .STRING ,
41
- } ,
40
+ self . _query (
41
+ Secret .SchemaAttributeType .STRING ,
42
+ Secret .SchemaAttributeType .STRING ,
43
+ application = Secret .SchemaAttributeType .STRING ,
44
+ ) ,
42
45
)
43
- collection = Secret .COLLECTION_DEFAULT
46
+
47
+ @property
48
+ def collection (self ):
49
+ return Secret .COLLECTION_DEFAULT
44
50
45
51
@properties .ClassProperty
46
52
@classmethod
@@ -53,11 +59,7 @@ def priority(cls):
53
59
54
60
def get_password (self , service , username ):
55
61
"""Get password of the username for the service"""
56
- attributes = {
57
- "application" : self .appid ,
58
- "service" : service ,
59
- "username" : username ,
60
- }
62
+ attributes = self ._query (service , username , application = self .appid )
61
63
try :
62
64
items = Secret .password_search_sync (
63
65
self .schema , attributes , Secret .SearchFlags .UNLOCK , None
@@ -78,11 +80,7 @@ def get_password(self, service, username):
78
80
79
81
def set_password (self , service , username , password ):
80
82
"""Set password for the username of the service"""
81
- attributes = {
82
- "application" : self .appid ,
83
- "service" : service ,
84
- "username" : username ,
85
- }
83
+ attributes = self ._query (service , username , application = self .appid )
86
84
label = "Password for '{}' on '{}'" .format (username , service )
87
85
try :
88
86
stored = Secret .password_store_sync (
@@ -101,11 +99,7 @@ def set_password(self, service, username, password):
101
99
102
100
def delete_password (self , service , username ):
103
101
"""Delete the stored password (only the first one)"""
104
- attributes = {
105
- "application" : self .appid ,
106
- "service" : service ,
107
- "username" : username ,
108
- }
102
+ attributes = self ._query (service , username , application = self .appid )
109
103
try :
110
104
items = Secret .password_search_sync (
111
105
self .schema , attributes , Secret .SearchFlags .UNLOCK , None
@@ -136,9 +130,7 @@ def get_credential(self, service, username):
136
130
and return a SimpleCredential containing the username and password
137
131
Otherwise, it will return the first username and password combo that it finds.
138
132
"""
139
- query = {"service" : service }
140
- if username :
141
- query ["username" ] = username
133
+ query = self ._query (service , username )
142
134
try :
143
135
items = Secret .password_search_sync (
144
136
self .schema , query , Secret .SearchFlags .UNLOCK , None
0 commit comments