Search code examples
introspectiongobjectgnome-shell-extensionsgjsgobject-introspection

Asynchronuous call to GnomeKeyring using GObjectIntrospection


I am writing a gnome-shell extension that shows the current balance of prepaid-cards like phones (or electricity). As this needs credentials for the given services, I do not want to store the password in gsettings, but as entry in gnome keyring.

Currently, I use the synchronous way asking the keyring for login and password using

const GnomeKeyring = imports.gi.GnomeKeyring;

GnomeKeyring.unlock_sync(null, null)
// the variable 'id' is a concat of login '@'webservice url
var attrs = GnomeKeyring.Attribute.list_new()
GnomeKeyring.Attribute.list_append_string(attrs, 'id', id)
var result = GnomeKeyring.find_items_sync(
                GnomeKeyring.ItemType.GENERIC_SECRET, 
                attrs
             )
if (result[0] != GnomeKeyring.Result.OK) return
log('  => password '+result[1][0].secret)
log('     keyring id  = '+result[1][0].item_id)
log('     keyring  = '+result[1][0].keyring)

This sync. approachs weak point is, that the keyring needs to already be open or a password dialog is prompted. When starting gnome-shell with auto-login, this synchronous call blocks actually starting the shell at all - so no possibility to enter the keyring password.

The Gnome Developer Wiki names the asynchronous methods

  • GnomeKeyring.unlock
  • GnomeKeyring.find_items

but both are not found in the javascript environment.

Where can I find the GnomeKeyring-Gir file under fedora23 to confirm the lack of the asynch functions missing? How can I achieve an asynchronous keyring-opening and retrieve of the passwords? Does anybody see a completely different, possible approach? Every little helps...


Solution

  • Please consider using libsecret instead of libgnome-keyring. It says "libsecret replaces libgnome-keyring" on the project website of libsecret. So for new projects you should probably use libsecret instead.

    Furthermore, libsecret has an asynchronous unlock() method. Although, at the time of writing, the docs say that the asynchronous method "may block indefinitely". But that could be a copy&paste error. So I would just try it!

    Also note that libsecret uses GnomeKeyring as a backend, so you would actually use GnomeKeyring, although in conjunction with a more versatile library.