/
keyring.py
69 lines (63 loc) · 2.15 KB
/
keyring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# This file is part of gmail-notifier.
#
# gmail-notifier is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# gmail-notifier is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with gmail-notifier. If not, see <http://www.gnu.org/licenses/>.
#
#
# Copyright 2009 by Pat Pannuto <pat.pannuto@gmail.com>
import gnomekeyring as gkey
class Keyring(object):
def __init__(self, name, server, protocol, setName=False):
if setName:
import gobject
gobject.set_application_name (name)
self._name = name
self._server = server
self._protocol = protocol
self._keyring = gkey.get_default_keyring_sync()
def has_credentials(self):
try:
attrs = {"server": self._server, "protocol": self._protocol}
items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
return len(items) > 0
except gkey.DeniedError:
return False
except gkey.NoMatchError:
return False
def get_credentials(self, user):
attrs = {"server": self._server, "protocol": self._protocol}
try:
items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
for item in items:
if item.attributes["user"] == user:
return item.secret
except gkey.NoMatchError:
pass
return None
def set_credentials(self, user, pw):
attrs = {
"user": user,
"server": self._server,
"protocol": self._protocol,
}
gkey.item_create_sync(gkey.get_default_keyring_sync(),
gkey.ITEM_NETWORK_PASSWORD, self._name, attrs, pw, True)
def delete_credentials(self, user, pw=None):
attrs = {
"user":user,
"server":self._server,
"protocol":self._protocol,
}
items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
for item in items:
gkey.item_delete_sync(None, item.item_id)