Gnome-keyring如何进行密码的CRUD

TLDR: 通过其API Example可知,存储Password时可以指定Keyring,查找Password时是在所有keyrings中检索。

Password在Keyring中的CRUD操作如下:

定义密码架构

每个存储的密码都有一组属性,这些属性稍后会 用于查找密码。属性的名称和类型 在架构中定义。架构通常全局定义一次。 下面介绍如何定义架构:

from gi.repository import Secret

EXAMPLE_SCHEMA = Secret.Schema.new("org.mock.type.Store",
    Secret.SchemaFlags.NONE,
    {
        "number": Secret.SchemaAttributeType.INTEGER,
        "string": Secret.SchemaAttributeType.STRING,
        "even": Secret.SchemaAttributeType.BOOLEAN,
    }
)

请参阅其他示例,了解如何 以使用架构。

存储密码

以下是在正在运行的特勤服务中存储密码的方法, 比如侏儒密钥环或ksecretservice。

每个存储的密码都有一组属性,这些属性稍后会 用于查找密码。属性不应包含 机密,因为它们不是以加密方式存储的。

这些示例使用示例架构

第一个示例异步存储密码,并且 适用于 GUI 应用程序,以便 UI 不会阻塞。

from gi.repository import Secret

def on_password_stored(source, result, unused):
    Secret.password_store_finish(result)
    # ... do something now that the password has been stored

# The attributes used to later lookup the password. These
# attributes should conform to the schema.
attributes = {
    "number": "8",
    "string": "eight",
    "even": "true"
}

Secret.password_store(EXAMPLE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
                      "The label", "the password", None, on_password_stored)

下一个示例同步存储密码。函数 调用将阻止,直到存储密码。所以这适用于 非图形用户界面应用程序。

from gi.repository import Secret

# The attributes used to later lookup the password. These
# attributes should conform to the schema.
attributes = {
    "number": "8",
    "string": "eight",
    "even": "true"
}

Secret.password_store_sync(EXAMPLE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
                           "The label", "the password", None)

查找密码

以下是在正在运行的特勤服务中查找密码的方法, 比如侏儒密钥环或ksecretservice。

每个存储的密码都有一组属性,这些属性是 用于查找密码。如果多个密码与 查找属性,然后返回最近存储的属性。

这些示例使用示例架构

第一个示例异步查找密码,并且 适用于 GUI 应用程序,以便 UI 不会阻塞。

from gi.repository import Secret

def on_password_lookup(source, result, unused):
    password = Secret.password_lookup_finish(result)
    # password will be null, if no matching password found

Secret.password_lookup(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
                       None, on_password_lookup)

下一个示例同步查找密码。函数 调用将阻止,直到查找完成。所以这适用于 非图形用户界面应用程序。

from gi.repository import Secret

password = Secret.password_lookup_sync(EXAMPLE_SCHEMA, { "number": "8", "even": "true" }, None)
# password will be null, if no matching password found

删除密码

以下是从正在运行的特勤服务中删除密码的方法, 比如侏儒密钥环或ksecretservice。

每个存储的密码都有一组属性,这些属性是 用于查找要删除的密码。如果多个密码与 属性,然后删除最近存储的属性。

这些示例使用示例架构

第一个示例异步删除密码,并且 适用于 GUI 应用程序,以便 UI 不会阻塞。

from gi.repository import Secret

def on_password_clear(source, result, unused):
    removed = Secret.password_clear_finish(result)
    # removed will be true if the password was removed

Secret.password_clear(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
                      None, on_password_clear)

下一个示例同步删除密码。函数 调用将阻止,直到删除完成。所以这适用于 非图形用户界面应用程序。

from gi.repository import Secret

removed = Secret.password_clear_sync(EXAMPLE_SCHEMA, { "number": "8", "even": "true" }, None)
# removed will be true if the password was remo

Ref: https://gnome.pages.gitlab.gnome.org/libsecret/libsecret-python-examples.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开源技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值