• 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,
        }
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

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

    存储密码

    以下是在正在运行的特勤服务中存储密码的方法, 比如侏儒密钥环或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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

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

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    查找密码

    以下是在正在运行的特勤服务中查找密码的方法, 比如侏儒密钥环或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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

    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
    
    • 1
    • 2
    • 3
    • 4

    删除密码

    以下是从正在运行的特勤服务中删除密码的方法, 比如侏儒密钥环或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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

    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
    
    • 1
    • 2
    • 3
    • 4

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

  • 相关阅读:
    Hugging News #0904: 登陆 AWS Marketplace
    C++ 装饰器模式
    算法基础-中国剩余定理
    英语不好怎么学python?这份python英语常用单词给我收好
    Java成品网站推荐 毕设从这起步就够了
    vue3中setup的两个参数 props,context (attrs,emit,slots),vue3中的双向数据绑定自定义事件emit和v-model
    卷积神经网络中的自适应加权池化
    ffmpeg将一个视频中的音频合并到另一个视频
    MAX/MSP SDK学习05:A_GIMME方法
    Outlook无需API开发连接钉钉群机器人,实现新增会议日程自动发送群消息通知
  • 原文地址:https://blog.csdn.net/leopardsaga/article/details/133623557