我需要从Ruby编程语言访问GNOME密钥环。但是,我找不到任何宝石的红宝石库。具体来说,我正在尝试获取桌面触控的Oauth键。
如何实现这一目标?
答案 0 :(得分:1)
尝试keyring
https://rubygems.org/gems/keyring/versions/0.3.1
它的工作方式与
一样简单require 'keyring'
keyring = Keyring.new
my_password = keyring.get_password('service', 'username')
答案 1 :(得分:0)
这在使用Gnome 3.28.2 / gnome-keyring 3.28.0 / Seahorse 3.20的Ubuntu 18.04上有效:
$ 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')
不幸的是,keyring
gem不允许您使用默认密钥环以外的任何其他内容。要使用其他钥匙圈,或者如果您只想剪裁中间人,则:
$ 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.
如果密钥环是未锁定的(可能是这样,因为默认情况下会使用您登录时会自动解除锁定的登录密钥环),因此读取和写入将正常进行。如果密钥环被锁定,那么在您进行任何读取或写入操作之前,系统都会提示您提供密码以解锁密钥环。
如果看到以下错误消息:
Typelib file for namespace 'GnomeKeyring' (any version) not found
然后,您可能需要安装gnome-keyring标头(apt install libgnome-keyring-dev
)