~ubuntu-branches/ubuntu/saucy/python-keyring/saucy

« back to all changes in this revision

Viewing changes to keyring/backends/Gnome.py

  • Committer: Package Import Robot
  • Author(s): Dmitry Shachnev, Dmitry Shachnev, Sebastian Ramacher
  • Date: 2013-07-14 22:42:56 UTC
  • mfrom: (1.1.7) (4.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20130714224256-mxehposo7iq23drm
Tags: 1.6-1
* Team upload.

[ Dmitry Shachnev ]
* New upstream release.
  - Fixes incompatibility between GNOME Keyring and SecretService
    backends (closes: #714440).
  - Removes warning when gnome-keyring is not available (LP: #1197988).
* Drop fix-importkiller.patch, applied upstream.

[ Sebastian Ramacher ]
* debian/control: Update Homepage to point to the Bitbucket repository. The
  old homepage does no longer exist.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
    def supported(self):
14
14
        try:
 
15
            from gi import Repository
 
16
            if not Repository.get_default().enumerate_versions('GnomeKeyring'):
 
17
                return -1
15
18
            from gi.repository import GnomeKeyring
16
19
        except ImportError:
17
20
            return -1
23
26
            else:
24
27
                return 0
25
28
 
 
29
    def _find_passwords(self, service, username, deleting=False):
 
30
        from gi.repository import GnomeKeyring
 
31
        passwords = []
 
32
 
 
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:
 
42
                passwords += items
 
43
            elif deleting:
 
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)
 
48
        return passwords
 
49
 
26
50
    def get_password(self, service, username):
27
51
        """Get password of the username for the service
28
52
        """
29
 
        from gi.repository import GnomeKeyring
30
 
 
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:
39
 
            return None
40
 
        if result == GnomeKeyring.Result.NO_MATCH:
41
 
            return None
42
 
        if result == GnomeKeyring.Result.CANCELLED:
43
 
            # The user pressed "Cancel" when prompted to unlock their keyring.
44
 
            return None
45
 
 
46
 
        assert len(items) == 1, 'no more than one entry should ever match'
 
53
        items = self._find_passwords(service, username)
 
54
        if not items:
 
55
            return None
 
56
 
47
57
        secret = items[0].secret
48
58
        return secret if isinstance(secret, unicode) else secret.decode('utf-8')
49
59
 
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)
68
81
 
69
82
    def delete_password(self, service, username):
70
83
        """Delete the password for the username of the service.
71
84
        """
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)
 
87
        if not items:
79
88
            raise PasswordDeleteError("Password not found")
80
89
        for current in items:
81
90
            result = GnomeKeyring.item_delete_sync(current.keyring,
82
91
                                                   current.item_id)
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)
85
96
 
86
97
    def _safe_string(self, source, encoding='utf-8'):
87
98
        """Convert unicode to string as gnomekeyring barfs on unicode"""