## Javascript 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
    17
    18
    19
    const Secret = imports.gi.Secret;

    function on_password_stored(source, result) {
    	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.
     */
    var attributes = {
    	"number": "8",
    	"string": "eight",
    	"even": "true"
    };

    Secret.password_store(EXAMPLE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
                          "The label", "the password", null, 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
    13
    14
    const Secret = imports.gi.Secret;

    /*
     * The attributes used to later lookup the password. These
     * attributes should conform to the schema.
     */
    var attributes = {
    	"number": "9",
    	"string": "nine",
    	"even": "false"
    };

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