Python example: Store a password: Libsecret Library Reference Manual

## Python example: Store a password

Here's how to store a password in the running secret service, like gnome-keyring or ksecretservice.

Each stored password has a set of attributes which are later used to lookup the password. The attributes should not contain secrets, as they are not stored in an encrypted fashion.

These examples use the example schema.

This first example stores a password asynchronously, and is appropriate for GUI applications so that the UI does not block.

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

This next example stores a password synchronously. The function call will block until the password is stored. So this is appropriate for non GUI applications.

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