[gnome-keyring/wip/dueno/ssh-agent-2: 3/8] login: Add non-varargs version of gkd_login_*_password() functions



commit 809d5425649397936409078f9ee0608f336dbf0b
Author: Daiki Ueno <dueno src gnome org>
Date:   Fri Feb 16 12:58:15 2018 +0100

    login: Add non-varargs version of gkd_login_*_password() functions
    
    Those functions could be used in GkdLoginInteraction where the fields
    are stored in a table.

 daemon/login/gkd-login.c |  123 ++++++++++++++++++++++++++++++++++------------
 daemon/login/gkd-login.h |   13 +++++
 2 files changed, 104 insertions(+), 32 deletions(-)
---
diff --git a/daemon/login/gkd-login.c b/daemon/login/gkd-login.c
index 4d8aee1..030c5b0 100644
--- a/daemon/login/gkd-login.c
+++ b/daemon/login/gkd-login.c
@@ -510,33 +510,27 @@ find_saved_items (GckSession *session,
 
 static gboolean
 fields_to_attribute (GckBuilder *builder,
-                     const gchar *field,
-                     va_list va)
+                     GHashTable *fields)
 {
-       GString *fields = g_string_sized_new (128);
-       const gchar *last = NULL;
+       GString *concat = g_string_sized_new (128);
+       const gchar *field;
        const gchar *value;
+       GList *keys, *l;
 
-       while (field) {
-               if (g_strcmp0 (last, field) >= 0) {
-                       g_critical ("lookup fields must be sorted '%s' >= '%s'", last, field);
-                       return FALSE;
-               }
-
-               last = field;
-               value = va_arg (va, const gchar *);
+       keys = g_hash_table_get_keys (fields);
+       for (l = g_list_sort (keys, (GCompareFunc) g_strcmp0); l; l = l->next) {
+               field = l->data;
+               value = g_hash_table_lookup (fields, field);
                g_return_val_if_fail (value != NULL, FALSE);
 
-               g_string_append (fields, field);
-               g_string_append_c (fields, '\0');
-               g_string_append (fields, value);
-               g_string_append_c (fields, '\0');
-
-               field = va_arg (va, const gchar *);
+               g_string_append (concat, field);
+               g_string_append_c (concat, '\0');
+               g_string_append (concat, value);
+               g_string_append_c (concat, '\0');
        }
 
-       gck_builder_add_data (builder, CKA_G_FIELDS, (const guchar *)fields->str, fields->len);
-       g_string_free (fields, TRUE);
+       gck_builder_add_data (builder, CKA_G_FIELDS, (const guchar *)concat->str, concat->len);
+       g_string_free (concat, TRUE);
        return TRUE;
 }
 
@@ -545,13 +539,36 @@ gkd_login_lookup_password (GckSession *session,
                            const gchar *field,
                            ...)
 {
+       GHashTable *fields;
+       const gchar *value;
+       gchar *result;
+       va_list va;
+
+       fields = g_hash_table_new (g_str_hash, g_str_equal);
+
+       va_start (va, field);
+       while (field) {
+               value = va_arg (va, const gchar *);
+               g_hash_table_insert (fields, (gpointer)field, (gpointer)value);
+               field = va_arg (va, const gchar *);
+       }
+       va_end (va);
+
+       result = gkd_login_lookup_passwordv (session, fields);
+       g_hash_table_unref (fields);
+       return result;
+}
+
+gchar *
+gkd_login_lookup_passwordv (GckSession *session,
+                           GHashTable *fields)
+{
        GckBuilder builder = GCK_BUILDER_INIT;
        GckAttributes *attrs;
        GList *objects, *l;
        GError *error = NULL;
        gpointer data = NULL;
        gsize length;
-       va_list va;
 
        if (!session)
                session = lookup_login_session (NULL);
@@ -562,10 +579,8 @@ gkd_login_lookup_password (GckSession *session,
 
        gck_builder_add_ulong (&builder, CKA_CLASS, CKO_SECRET_KEY);
 
-       va_start (va, field);
-       if (!fields_to_attribute (&builder, field, va))
+       if (!fields_to_attribute (&builder, fields))
                g_return_val_if_reached (FALSE);
-       va_end (va);
 
        attrs = gck_attributes_ref_sink (gck_builder_end (&builder));
        objects = find_saved_items (session, attrs);
@@ -597,11 +612,32 @@ gkd_login_clear_password (GckSession *session,
                           const gchar *field,
                           ...)
 {
+       GHashTable *fields;
+       const gchar *value;
+       va_list va;
+
+       fields = g_hash_table_new (g_str_hash, g_str_equal);
+
+       va_start (va, field);
+       while (field) {
+               value = va_arg (va, const gchar *);
+               g_hash_table_insert (fields, (gpointer)field, (gpointer)value);
+               field = va_arg (va, const gchar *);
+       }
+       va_end (va);
+
+       gkd_login_clear_passwordv (session, fields);
+       g_hash_table_unref (fields);
+}
+
+void
+gkd_login_clear_passwordv (GckSession *session,
+                          GHashTable *fields)
+{
        GckBuilder builder = GCK_BUILDER_INIT;
        GckAttributes *attrs;
        GList *objects, *l;
        GError *error = NULL;
-       va_list va;
 
        if (!session)
                session = lookup_login_session (NULL);
@@ -612,10 +648,8 @@ gkd_login_clear_password (GckSession *session,
 
        gck_builder_add_ulong (&builder, CKA_CLASS, CKO_SECRET_KEY);
 
-       va_start (va, field);
-       if (!fields_to_attribute (&builder, field, va))
+       if (!fields_to_attribute (&builder, fields))
                g_return_if_reached ();
-       va_end (va);
 
        attrs = gck_attributes_ref_sink (gck_builder_end (&builder));
        objects = find_saved_items (session, attrs);
@@ -642,6 +676,34 @@ gkd_login_store_password (GckSession *session,
                           const gchar *field,
                           ...)
 {
+       GHashTable *fields;
+       const gchar *value;
+       gboolean result;
+       va_list va;
+
+       fields = g_hash_table_new (g_str_hash, g_str_equal);
+
+       va_start (va, field);
+       while (field) {
+               value = va_arg (va, const gchar *);
+               g_hash_table_insert (fields, (gpointer)field, (gpointer)value);
+               field = va_arg (va, const gchar *);
+       }
+       va_end (va);
+
+       result = gkd_login_store_passwordv (session, password, label, method, lifetime, fields);
+       g_hash_table_unref (fields);
+       return result;
+}
+
+gboolean
+gkd_login_store_passwordv (GckSession *session,
+                          const gchar *password,
+                          const gchar *label,
+                          const gchar *method,
+                          gint lifetime,
+                          GHashTable *fields)
+{
        GckBuilder builder = GCK_BUILDER_INIT;
        GckAttributes *attrs;
        guchar *identifier;
@@ -650,7 +712,6 @@ gkd_login_store_password (GckSession *session,
        GckObject *item;
        GError *error = NULL;
        gsize length;
-       va_list va;
 
        if (!method)
                method = GCR_UNLOCK_OPTION_SESSION;
@@ -662,10 +723,8 @@ gkd_login_store_password (GckSession *session,
        if (!session)
                return FALSE;
 
-       va_start (va, field);
-       if (!fields_to_attribute (&builder, field, va))
+       if (!fields_to_attribute (&builder, fields))
                g_return_val_if_reached (FALSE);
-       va_end (va);
 
        if (g_str_equal (method, GCR_UNLOCK_OPTION_ALWAYS)) {
                gck_builder_add_string (&builder, CKA_G_COLLECTION, "login");
diff --git a/daemon/login/gkd-login.h b/daemon/login/gkd-login.h
index 5e21982..1ad4629 100644
--- a/daemon/login/gkd-login.h
+++ b/daemon/login/gkd-login.h
@@ -48,4 +48,17 @@ gboolean          gkd_login_store_password           (GckSession *session,
                                                      const gchar *field,
                                                      ...) G_GNUC_NULL_TERMINATED;
 
+gchar *           gkd_login_lookup_passwordv         (GckSession *session,
+                                                     GHashTable *fields);
+
+void              gkd_login_clear_passwordv          (GckSession *session,
+                                                     GHashTable *fields);
+
+gboolean          gkd_login_store_passwordv          (GckSession *session,
+                                                     const gchar *password,
+                                                     const gchar *label,
+                                                     const gchar *method,
+                                                     gint lifetime,
+                                                     GHashTable *fields);
+
 #endif /* __GKD_LOGIN_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]