How to find a key by label from secretstorage collection

只愿长相守 提交于 2019-12-11 08:03:45

问题


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


回答1:


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".



来源:https://stackoverflow.com/questions/44677371/how-to-find-a-key-by-label-from-secretstorage-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!