# Examples

## C examples

### C example: Define a password schema

Each stored password has a set of attributes which are later used to lookup the password. The names and types of the attributes are defined in a schema. The schema is usually defined once globally. Here's how to define a schema:

    			/* in a header: */

    			const SecretSchema * example_get_schema (void) G_GNUC_CONST;

    			#define EXAMPLE_SCHEMA  example_get_schema ()

    			/* in a .c file: */

    			const SecretSchema *
    			example_get_schema (void)
    			{
    				static const SecretSchema the_schema = {
    					"org.example.Password", SECRET_SCHEMA_NONE,
    					{
    						{  "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
    						{  "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
    						{  "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
    						{  "NULL", 0 },
    					}
    				};
    				return &the_schema;
    			}

See the other examples for how to use the schema.

### C 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.

    			static void
    			on_password_stored (GObject *source,
    			                    GAsyncResult *result,
    			                    gpointer unused)
    			{
    				GError *error = NULL;

    				secret_password_store_finish (result, &error);
    				if (error != NULL) {
    					/* ... handle the failure here */
    					g_error_free (error);
    				} else {
    					/* ... do something now that the password has been stored */
    				}
    			}

    			/*
    			 * The variable argument list is the attributes used to later
    			 * lookup the password. These attributes must conform to the schema.
    			 */
    			secret_password_store (EXAMPLE_SCHEMA, SECRET_COLLECTION_DEFAULT, "The label",
    			                       "the password", NULL, on_password_stored, NULL,
    			                       "number", 8,
    			                       "string", "eight",
    			                       "even", TRUE,
    			                       NULL);

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.

    			GError *error = NULL;

    			/*
    			 * The variable argument list is the attributes used to later
    			 * lookup the password. These attributes must conform to the schema.
    			 */
    			secret_password_store_sync (EXAMPLE_SCHEMA, SECRET_COLLECTION_DEFAULT,
    			                            "The label", "the password", NULL, &error,
    			                            "number", 9,
    			                            "string", "nine",
    			                            "even", FALSE,
    			                            NULL);

    			if (error != NULL) {
    				/* ... handle the failure here */
    				g_error_free (error);
    			} else {
    				/* ... do something now that the password has been stored */
    			}

### C example: Lookup a password

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

Each stored password has a set of attributes which are used to lookup the password. If multiple passwords match the lookup attributes, then the one stored most recently is returned.

These examples use the example schema.

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

    			static void
    			on_password_lookup (GObject *source,
    			                    GAsyncResult *result,
    			                    gpointer unused)
    			 {
    				GError *error = NULL;

    				gchar *password = secret_password_lookup_finish (result, &error);

    				if (error != NULL) {
    					/* ... handle the failure here */
    					g_error_free (error);

    				} else if (password == NULL) {
    					/* password will be null, if no matching password found */

    				} else {
    					/* ... do something with the password */
    					secret_password_free (password);
    				}

    			}

    			/*
    			 * The variable argument list is the attributes used to later
    			 * lookup the password. These attributes must conform to the schema.
    			 */
    			secret_password_lookup (EXAMPLE_SCHEMA, NULL, on_password_lookup, NULL,
    			                        "string", "nine",
    			                        "even", FALSE,
    			                        NULL);

This next example looks up a password synchronously. The function call will block until the lookup completes. So this is appropriate for non GUI applications.

    			GError *error = NULL;

    			/* The attributes used to lookup the password should conform to the schema. */
    			gchar *password = secret_password_lookup_sync (EXAMPLE_SCHEMA, NULL, &error,
    			                                               "string", "nine",
    			                                               "even", FALSE,
    			                                               NULL);

    			if (error != NULL) {
    				/* ... handle the failure here */
    				g_error_free (error);

    			} else if (password == NULL) {
    				/* password will be null, if no matching password found */

    			} else {
    				/* ... do something with the password */
    				secret_password_free (password);
    			}

### C example: Remove a password

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

Each stored password has a set of attributes which are used to find which password to remove. If multiple passwords match the attributes, then the one stored most recently is removed.

These examples use the example schema.

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

    			static void
    			on_password_cleared (GObject *source,
    			                     GAsyncResult *result,
    			                     gpointer unused)
    			 {
    				GError *error = NULL;

    				gboolean removed = secret_password_clear_finish (result, &error);

    				if (error != NULL) {
    					/* ... handle the failure here */
    					g_error_free (error);

    				} else {
    					/* removed will be TRUE if a password was removed */
    				}
    			}

    			/*
    			 * The variable argument list is the attributes used to later
    			 * lookup the password. These attributes must conform to the schema.
    			 */
    			secret_password_clear (EXAMPLE_SCHEMA, NULL, on_password_cleard, NULL,
    			                       "string", "nine",
    			                       "even", FALSE,
    			                       NULL);

This next example looks up a password synchronously. The function call will block until the lookup completes. So this is appropriate for non GUI applications.

    			GError *error = NULL;

    			/*
    			 * The variable argument list is the attributes used to later
    			 * lookup the password. These attributes must conform to the schema.
    			 */
    			gboolean removed = secret_password_clear_sync (EXAMPLE_SCHEMA, NULL, &error,
    			                                               "string", "nine",
    			                                               "even", FALSE,
    			                                               NULL);

    			if (error != NULL) {
    				/* ... handle the failure here */
    				g_error_free (error);

    			} else {
    				/* removed will be TRUE if a password was removed */
    			}

## Javascript examples

### Javascript example: Define a password schema

Each stored password has a set of attributes which are later used to lookup the password. The names and types of the attributes are defined in a schema. The schema is usually defined once globally. Here's how to define a schema:

    			const Secret = imports.gi.Secret;

    			/* This schema is usually defined once globally */
    			const EXAMPLE_SCHEMA = new Secret.Schema.new("org.example.Password",
    				Secret.SchemaFlags.NONE,
    				{
    					"number": Secret.SchemaAttributeType.INTEGER,
    					"string": Secret.SchemaAttributeType.STRING,
    					"even": Secret.SchemaAttributeType.BOOLEAN,
    				}
    			);

See the other examples for how to use the schema.

### 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.

    			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.

    			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);

### Javascript example: Lookup a password

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

Each stored password has a set of attributes which are used to lookup the password. If multiple passwords match the lookup attributes, then the one stored most recently is returned.

These examples use the example schema.

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

    			const Secret = imports.gi.Secret;

    			function on_password_lookup(source, result) {
    				var password = Secret.password_lookup_finish(result);
    				/* password will be null if no matching password found */
    			}

    			/* The attributes used to lookup the password should conform to the schema. */
    			Secret.password_lookup(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
    			                       null, on_password_lookup);

This next example looks up a password synchronously. The function call will block until the lookup completes. So this is appropriate for non GUI applications.

    			const Secret = imports.gi.Secret;

    			/* The attributes used to lookup the password should conform to the schema. */
    			var password = Secret.password_lookup_sync(EXAMPLE_SCHEMA,
    			                                           { "number": "8", "even": "true" },
    			                                           null);

    			/* password will be null, if no matching password found */

### Javascript example: Remove a password

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

Each stored password has a set of attributes which are used to find which password to remove. If multiple passwords match the attributes, then the one stored most recently is removed.

These examples use the example schema.

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

    			const Secret = imports.gi.Secret;

    			function on_password_clear(source, result) {
    				var removed = Secret.password_clear_finish(result);
    				/* removed will be true if the password was removed */
    			}

    			/* The attributes used to lookup which password to remove should conform to the schema. */
    			Secret.password_clear(EXAMPLE_SCHEMA, { "number": "8", "even": "true" },
    			                      null, on_password_clear);

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

    			const Secret = imports.gi.Secret;

    			/* The attributes used to lookup which password to remove should conform to the schema. */
    			var removed = Secret.password_clear_sync(EXAMPLE_SCHEMA,
    			                                         { "number": "8", "even": "true" },
    			                                         null);

    			/* removed will be true if the password was removed */

## Python examples

### Python example: Define a password schema

Each stored password has a set of attributes which are later used to lookup the password. The names and types of the attributes are defined in a schema. The schema is usually defined once globally. Here's how to define a schema:

    			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,
    				}
    			)

See the other examples for how to use the schema.

### 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.

    			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.

    			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)

### Python example: Lookup a password

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

Each stored password has a set of attributes which are used to lookup the password. If multiple passwords match the lookup attributes, then the one stored most recently is returned.

These examples use the example schema.

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

    			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)

This next example looks up a password synchronously. The function call will block until the lookup completes. So this is appropriate for non GUI applications.

    			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

### Python example: Remove a password

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

Each stored password has a set of attributes which are used to find which password to remove. If multiple passwords match the attributes, then the one stored most recently is removed.

These examples use the example schema.

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

    			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)

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

    			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 removed

## Vala examples

### Vala example: Define a password schema

Each stored password has a set of attributes which are later used to lookup the password. The names and types of the attributes are defined in a schema. The schema is usually defined once globally. Here's how to define a schema:

    			var example = new Secret.Schema ("org.example.Password", Secret.SchemaFlags.NONE,
    			                                 "number", Secret.SchemaAttributeType.INTEGER,
    			                                 "string", Secret.SchemaAttributeType.STRING,
    			                                 "even", Secret.SchemaAttributeType.BOOLEAN);

See the other examples for how to use the schema.

### Vala 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.

    			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:

    			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:

    			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.

    			Secret.password_store_sync (example_schema, attributes, Secret.COLLECTION_DEFAULT,
    			                            "The label", "the password", null,
    			                            "number", 9, "string", "nine", "even", false);

### Vala example: Lookup a password

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

Each stored password has a set of attributes which are used to lookup the password. If multiple passwords match the lookup attributes, then the one stored most recently is returned.

These examples use the example schema.

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

    			var attributes = new GLib.HashTable<string,string> ();
    			attributes["number"] = "8";
    			attributes["string"] = "eight";
    			attributes["even"] = "true";

    			Secret.password_lookupv.begin (example_schema, attributes, null, (obj, async_res) => {
    				string password = Secret.password_lookup.end (async_res);
    			});

This next example looks up a password synchronously. The function call will block until the lookup completes. So this is appropriate for non GUI applications.

    			string password = Secret.password_lookup_sync (example_schema, attributes, null,
    			                                               "number", 9, "string", "nine", "even", false);
    			/* password will be null, if no matching password found */

### Vala example: Remove a password

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

Each stored password has a set of attributes which are used to find which password to remove. If multiple passwords match the attributes, then the one stored most recently is removed.

These examples use the example schema.

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

    			var attributes = new GLib.HashTable<string,string> ();
    			attributes["number"] = "8";
    			attributes["string"] = "eight";
    			attributes["even"] = "true";

    			Secret.password_clearv.begin (example_schema, attributes, null, (obj, async_res) => {
    				bool removed = Secret.password_clearv.end (async_res);
    			});

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

    			var attributes = new GLib.HashTable<string,string> ();
    			attributes["number"] = "8";
    			attributes["string"] = "eight";
    			attributes["even"] = "true";

    			bool removed = Secret.password_clear_sync (example_schema, null,
    			                                           "number", 8, "string", "eight", "even", true);
    			/* removed will be true if the password was removed */