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 |
var attributes = new GLib.HashTable<string,string> (); attributes["number"] = "8"; attributes["string"] = "eight"; attributes["even"] = "true"; Secret.password_storev.begin (example_schema, attributes, Secret.COLLECTION_DEFAULT, "The label", "the password", null, (obj, async_res) => { bool res = Secret.password_store.end (async_res); /* ... do something now that the password has been stored */ }); |
If you are already inside of an async function, you can also use the yield keyword:
1 2 3 4 5 6 7 8 |
var attributes = new GLib.HashTable<string,string> (); attributes["number"] = "8"; attributes["string"] = "eight"; attributes["even"] = "true"; bool res = yield Secret.password_storev (example_schema, attributes, Secret.COLLECTION_DEFAULT, "The label", "the password", null); |
If you would like to avoid creating a hash table for the attributes you can just use the variadic version:
1 2 3 |
bool res = yield Secret.password_store (example_schema, Secret.COLLECTION_DEFAULT, "The label", "the password", null, "number", 8, "string", "eight", "even", true); |
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 |
Secret.password_store_sync (example_schema, attributes, Secret.COLLECTION_DEFAULT, "The label", "the password", null, "number", 9, "string", "nine", "even", false); |