~jconti/gm-notify/messaging-menu

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
from __future__ import print_function

__version__ = "$Revision: 14294 $"

from gi.repository import GnomeKeyring, Gtk

def attributes(d):
  '''Converts a dictionary to a GnomeKeyring.Attribute array'''
  attrs = GnomeKeyring.Attribute.list_new()
  for key in d:
    GnomeKeyring.Attribute.list_append_string(attrs, key, str(d[key]))
  return attrs

def dict_from_attributes(attrs):
  '''Converts item results back into a dictionary'''
  result = {}
  for attr in GnomeKeyring.Attribute.list_to_glist(attrs):
    result[attr.name] = attr.get_string()
  return result

class KeyringException(Exception):
  pass

class Keyring(object):
    def __init__(self, name, server, protocol):
        self._name = name
        self._server = server
        self._protocol = protocol
        result, self._keyring = GnomeKeyring.get_default_keyring_sync()
    
    def has_credentials(self):
        attrs = attributes({"server": self._server, "protocol": self._protocol})
        result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs)
        if result in (GnomeKeyring.Result.NO_MATCH, GnomeKeyring.Result.DENIED):
            return False
        return len(items) > 0

    def get_credentials(self):
        attrs = attributes({"server": self._server, "protocol": self._protocol})
        result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs)
        if len(items) == 0:
            raise KeyringException("Credentials not found")
        d = dict_from_attributes(items[0].attributes)
        return (d["user"], items[0].secret)
    
    def delete_credentials(self):
        attrs = attributes({"server": self._server, "protocol": self._protocol})
        result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.NETWORK_PASSWORD, attrs)
        for item in items:
            GnomeKeyring.item_delete_sync(self._keyring, item.item_id)
    
    def set_credentials(self, user, pw):
        attrs = attributes({
                "user": user,
                "server": self._server,
                "protocol": self._protocol,
            })
        GnomeKeyring.item_create_sync(self._keyring,
                GnomeKeyring.ItemType.NETWORK_PASSWORD, self._name, attrs, pw, True)