Search code examples
pythongtk3

How to find a key by label from secretstorage collection


I used GnomeKeyring from Gtk3 with Python 2.7 but almost all methodes are deprecated [1]. So I tried to use SecretSecret.Collection [2]

import gi
gi.require_version('Secret', '1.0')
from gi.repository import Secret
>> ValueError: Namespace Secret not available

I found the package "python-secretstorage" [3] and can access the keyring now:

import secretstorage
bus = secretstorage.dbus_init()
collection = secretstorage.get_default_collection(bus)  ## login keyring

But how can I find the key I am searching for by label so I dont have to iterate over all item?

items = collection.get_all_items()
for item in items:
    if item.get_label() == "most_wanted_key":
        return item

Here is what I tried, but it don't works with the label, only with attribute(s).

found_items = collection.search_items({"label": "most_wanted_key"})
  1. https://lazka.github.io/pgi-docs/GnomeKeyring-1.0/functions.html
  2. https://lazka.github.io/pgi-docs/Secret-1/classes/Collection.html
  3. https://secretstorage.readthedocs.io/en/latest/

Update

https://specifications.freedesktop.org/secret-service/ch05.html Chapter 5. Lookup Attributes During a lookup, attribute names and values are matched via case-sensitive string equality. ... In order to search for items, use the SearchItems() method of the Service interface. https://specifications.freedesktop.org/secret-service/re01.html#org.freedesktop.Secret.Service.SearchItems


Solution

  • I haven't figured out how to search for the label either, but AFAICT, the label is set from one of the attributes by the GUI. This seems to work for me to find website credentials:

    import secretstorage
    bus = secretstorage.dbus_init()
    collection = secretstorage.get_default_collection(bus)  ## login keyring
    search={u'action_url': u'https://testapp.example.com:8443/login'}
    items = collection.search_items(search)
    for item in items:
      print item.get_label()
      print item.get_secret()
    

    The printed label is, in fact, identical to what I searched for, and I think this is the intended way to use the API. Of course, the twist is in figuring out the exact attribute to search for; for another website the identifying URL is stored under "origin_url".