manpagez: man pages & more
html files: libsecret-1
Home | html | info | man

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
}
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.