• 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

  • 相关阅读:
    Spring Cloud项目(六)——使用sentinel作为流控管理
    We’re sorry but XXX doesn’t work properly without JavaScript enabled(解决方案汇总)
    [数据结构]-二叉搜索树
    element-plus 表格-定位到指定行
    ASM字节码插桩
    Redis持久化
    JS Array 操作方法合集
    搭建git私人仓库
    (Matlab)基于蝙蝠算法实现电力系统经济调度
    视觉设计规范
  • 原文地址:https://blog.csdn.net/leopardsaga/article/details/133623557