本文主要是介绍Gnome-keyring如何进行密码的CRUD,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
TLDR: 通过其API Example可知,存储Password时可以指定Keyring,查找Password时是在所有keyrings中检索。
Password在Keyring中的CRUD操作如下:
定义密码架构
每个存储的密码都有一组属性,这些属性稍后会 用于查找密码。属性的名称和类型 在架构中定义。架构通常全局定义一次。 下面介绍如何定义架构:
from gi.repository import SecretEXAMPLE_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 Secretdef 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 Secretdef on_password_lookup(source, result, unused):password = Secret.password_lookup_finish(result)# password will be null, if no matching password foundSecret.password_lookup(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },None, on_password_lookup)
下一个示例同步查找密码。函数 调用将阻止,直到查找完成。所以这适用于 非图形用户界面应用程序。
from gi.repository import Secretpassword = 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 Secretdef on_password_clear(source, result, unused):removed = Secret.password_clear_finish(result)# removed will be true if the password was removedSecret.password_clear(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },None, on_password_clear)
下一个示例同步删除密码。函数 调用将阻止,直到删除完成。所以这适用于 非图形用户界面应用程序。
from gi.repository import Secretremoved = 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
这篇关于Gnome-keyring如何进行密码的CRUD的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!