I need to access GNOME keyrings from the Ruby programming language. However, I can't find any ruby libraries of gems. Specifically, I'm t开发者_StackOverflow中文版rying to get the Oauth keys for desktopcouch.
How can this be acheived?
Try keyring
https://rubygems.org/gems/keyring/versions/0.3.1
It works as simple as
require 'keyring'
keyring = Keyring.new
my_password = keyring.get_password('service', 'username')
This works on Ubuntu 18.04 with Gnome 3.28.2 / gnome-keyring 3.28.0 / Seahorse 3.20:
$ apt install libgnome-keyring-dev
$ gem install keyring
require 'keyring'
keyring = Keyring.new
keyring.set_password('service', 'username', 'password')
password = keyring.get_password('service', 'username')
# => "password"
keyring.delete_password('service', 'username')
Unfortunately the keyring
gem does not allow you to use anything other than the default keyring. To use other keyrings, or if you just want to cut out the middle-man:
$ gem install gir_ffi-gnome_keyring
require 'gir_ffi-gnome_keyring'
service_name = 'MyApplication'
username = 'Blah'
attrs = GnomeKeyring::AttributeList.new
attrs.append_string 'service', service_name
attrs.append_string 'username', username
attrs.append_string 'arbitrary_values', "whatever"
status, item_id = GnomeKeyring.item_create_sync(
"My Other Keyring",
:generic_secret,
"#{service_name} (#{username})",
attrs,
'my secret password',
true
)
# Using default keyring:
# status, item_id = GnomeKeyring.item_create_sync(
# nil,
# :generic_secret,
# "#{service_name} (#{username})",
# attrs,
# 'my secret password',
# true
# )
# Method signature:
# .item_create_sync(keyring, type, display_name, attributes, secret, update_if_exists)
status, keys = GnomeKeyring.find_items_sync :generic_secret, attrs
keys.first.secret
# => "my secret password"
# NOTE: find_items_sync() will search all unlocked keyrings of the right type and will
# return an array of all matches. There doesn't seem to be a way of narrowing the search.
If the keyring is unlocked (which it probably will be, because by default it uses your login keyring which is automatically unlocked when you log in) then reads and writes will work without any issues. If the keyring is locked, then you will be prompted to supply a password to unlock the keyring, before any reading or writing can happen.
If you see the following error message:
Typelib file for namespace 'GnomeKeyring' (any version) not found
Then you probably need to install the gnome-keyring headers (apt install libgnome-keyring-dev
)
精彩评论