[gnome-keyring/dbus-api] [dbus] Refactor collection creation.



commit b49c6468d130df949ae01172111bd88d2c1c7d27
Author: Stef Walter <stef memberwebs com>
Date:   Sat Dec 12 23:46:35 2009 +0000

    [dbus] Refactor collection creation.
    
    Reorganize things with more code reuse.

 daemon/dbus/gkd-secret-create.c  |   62 ++++++++++++++++++++++++--------------
 daemon/dbus/gkd-secret-create.h  |    5 +++
 daemon/dbus/gkd-secret-service.c |   16 +++-------
 3 files changed, 49 insertions(+), 34 deletions(-)
---
diff --git a/daemon/dbus/gkd-secret-create.c b/daemon/dbus/gkd-secret-create.c
index 1757307..894e4a0 100644
--- a/daemon/dbus/gkd-secret-create.c
+++ b/daemon/dbus/gkd-secret-create.c
@@ -105,18 +105,13 @@ create_collection_with_credential (GkdSecretCreate *self, GP11Object *cred)
 	session =  gkd_secret_prompt_get_pkcs11_session (GKD_SECRET_PROMPT (self));
 	g_return_val_if_fail (session, FALSE);
 
-	/* Setup remainder of attributes on collection */
-	gp11_attributes_add_ulong (self->pkcs11_attrs, CKA_G_CREDENTIAL,
-	                           gp11_object_get_handle (cred));
-
-	collection = gp11_session_create_object_full (session, self->pkcs11_attrs, NULL, &error);
+	collection = gkd_secret_create_with_credential (session, self->pkcs11_attrs, cred, &error);
 	if (!collection) {
 		g_warning ("couldn't create collection: %s", error->message);
 		g_clear_error (&error);
 		return FALSE;
 	}
 
-	gp11_object_set_session (collection, session);
 	identifier = gp11_object_get_data (collection, CKA_ID, &n_identifier, &error);
 	g_object_unref (collection);
 
@@ -144,7 +139,7 @@ create_collection_with_password (GkdSecretCreate *self, const gchar *password)
 
 	g_assert (GKD_SECRET_IS_CREATE (self));
 
-	if (gp11_attributes_find_boolean (self->pkcs11_attrs, CKA_TOKEN, &token))
+	if (!gp11_attributes_find_boolean (self->pkcs11_attrs, CKA_TOKEN, &token))
 		token = FALSE;
 	n_password = password ? strlen (password) : 0;
 
@@ -239,7 +234,6 @@ gkd_secret_create_set_property (GObject *obj, guint prop_id, const GValue *value
 	case PROP_PKCS11_ATTRIBUTES:
 		g_return_if_fail (!self->pkcs11_attrs);
 		self->pkcs11_attrs = g_value_dup_boxed (value);
-		gp11_attributes_add_ulong (self->pkcs11_attrs, CKA_CLASS, CKO_G_COLLECTION);
 		g_return_if_fail (self->pkcs11_attrs);
 		break;
 	default:
@@ -297,6 +291,35 @@ gkd_secret_create_new (GkdSecretService *service, const gchar *caller,
 	                     NULL);
 }
 
+GP11Object*
+gkd_secret_create_with_credential (GP11Session *session, GP11Attributes *attrs,
+                                   GP11Object *cred, GError **error)
+{
+	GP11Attributes *atts;
+	GP11Attribute *attr;
+	GP11Object *collection;
+	gboolean token;
+
+	atts = gp11_attributes_newv (CKA_G_CREDENTIAL, GP11_ULONG, gp11_object_get_handle (cred),
+	                             CKA_CLASS, GP11_ULONG, CKO_G_COLLECTION,
+	                             GP11_INVALID);
+
+	attr = gp11_attributes_find (attrs, CKA_LABEL);
+	if (attr != NULL)
+		gp11_attributes_add (atts, attr);
+	if (!gp11_attributes_find_boolean (attrs, CKA_TOKEN, &token))
+		token = FALSE;
+	gp11_attributes_add_boolean (atts, CKA_TOKEN, token);
+
+	collection = gp11_session_create_object_full (session, atts, NULL, error);
+	gp11_attributes_unref (atts);
+
+	if (collection != NULL)
+		gp11_object_set_session (collection, session);
+
+	return collection;
+}
+
 DBusMessage*
 gkd_secret_create_without_prompting (GkdSecretService *service, DBusMessage *message,
                                      GP11Attributes *attrs, GkdSecretSecret *master)
@@ -304,7 +327,6 @@ gkd_secret_create_without_prompting (GkdSecretService *service, DBusMessage *mes
 	DBusError derr = DBUS_ERROR_INIT;
 	GkdSecretSession *session;
 	GP11Attributes *atts;
-	GP11Attribute *label;
 	DBusMessage *reply;
 	GP11Object *cred;
 	GP11Object *collection;
@@ -312,6 +334,7 @@ gkd_secret_create_without_prompting (GkdSecretService *service, DBusMessage *mes
 	GError *error = NULL;
 	gpointer identifier;
 	gsize n_identifier;
+	gboolean token;
 	gchar *path;
 
 	/* Figure out the session */
@@ -321,9 +344,12 @@ gkd_secret_create_without_prompting (GkdSecretService *service, DBusMessage *mes
 		return dbus_message_new_error (message, SECRET_ERROR_NO_SESSION,
 		                               "No such session exists");
 
+	if (!gp11_attributes_find_boolean (attrs, CKA_TOKEN, &token))
+		token = FALSE;
+
 	atts = gp11_attributes_newv (CKA_CLASS, GP11_ULONG, CKO_G_CREDENTIAL,
 	                             CKA_GNOME_TRANSIENT, GP11_BOOLEAN, TRUE,
-	                             CKA_TOKEN, GP11_BOOLEAN, TRUE,
+	                             CKA_TOKEN, GP11_BOOLEAN, token,
 	                             GP11_INVALID);
 
 	/* Create ourselves some credentials */
@@ -336,23 +362,13 @@ gkd_secret_create_without_prompting (GkdSecretService *service, DBusMessage *mes
 		return reply;
 	}
 
-	/* The only thing we actually use from the properties right now is the label */
-	atts = gp11_attributes_newv (CKA_TOKEN, GP11_BOOLEAN, TRUE,
-	                             CKA_G_CREDENTIAL, GP11_ULONG, gp11_object_get_handle (cred),
-	                             CKA_CLASS, GP11_ULONG, CKO_G_COLLECTION,
-	                             GP11_INVALID);
-
-	label = gp11_attributes_find (attrs, CKA_LABEL);
-	if (label != NULL)
-		gp11_attributes_add (atts, label);
-
-	g_object_unref (cred);
-
 	pkcs11_session = gkd_secret_service_get_pkcs11_session (service, dbus_message_get_sender (message));
 	g_return_val_if_fail (pkcs11_session, NULL);
 
-	collection = gp11_session_create_object_full (pkcs11_session, atts, NULL, &error);
+	collection = gkd_secret_create_with_credential (pkcs11_session, attrs, cred, &error);
+
 	gp11_attributes_unref (atts);
+	g_object_unref (cred);
 
 	if (collection == NULL) {
 		g_warning ("couldn't create collection: %s", error->message);
diff --git a/daemon/dbus/gkd-secret-create.h b/daemon/dbus/gkd-secret-create.h
index 78e9a4b..0d522f6 100644
--- a/daemon/dbus/gkd-secret-create.h
+++ b/daemon/dbus/gkd-secret-create.h
@@ -48,6 +48,11 @@ GkdSecretCreate*    gkd_secret_create_new                     (GkdSecretService
                                                                const gchar *caller,
                                                                GP11Attributes *attrs);
 
+GP11Object*         gkd_secret_create_with_credential         (GP11Session *session,
+                                                               GP11Attributes *attrs,
+                                                               GP11Object *cred,
+                                                               GError **error);
+
 DBusMessage*        gkd_secret_create_without_prompting       (GkdSecretService *service,
                                                                DBusMessage *message,
                                                                GP11Attributes *attrs,
diff --git a/daemon/dbus/gkd-secret-service.c b/daemon/dbus/gkd-secret-service.c
index a75dda7..96e5496 100644
--- a/daemon/dbus/gkd-secret-service.c
+++ b/daemon/dbus/gkd-secret-service.c
@@ -401,8 +401,7 @@ static DBusMessage*
 service_method_create_collection (GkdSecretService *self, DBusMessage *message)
 {
 	DBusMessageIter iter, array;
-	GP11Attributes *attrs, *all;
-	GP11Attribute *label;
+	GP11Attributes *attrs;
 	GkdSecretCreate *create;
 	ServiceClient *client;
 	DBusMessage *reply;
@@ -415,21 +414,15 @@ service_method_create_collection (GkdSecretService *self, DBusMessage *message)
 		return NULL;
 	if (!dbus_message_iter_init (message, &iter))
 		g_return_val_if_reached (NULL);
-	all = gp11_attributes_new ();
+	attrs = gp11_attributes_new ();
 	dbus_message_iter_recurse (&iter, &array);
-	if (!gkd_secret_property_parse_all (&array, all)) {
-		gp11_attributes_unref (all);
+	if (!gkd_secret_property_parse_all (&array, attrs)) {
+		gp11_attributes_unref (attrs);
 		return dbus_message_new_error_printf (message, DBUS_ERROR_INVALID_ARGS,
 		                                      "Invalid properties");
 	}
 
-	/* The only thing we actually use from the properties right now is the label */
-	label = gp11_attributes_find (all, CKA_LABEL);
-	attrs = gp11_attributes_new ();
-	if (label != NULL)
-		gp11_attributes_add (attrs, label);
 	gp11_attributes_add_boolean (attrs, CKA_TOKEN, TRUE);
-	gp11_attributes_unref (all);
 
 	/* Create the prompt object, for the password */
 	caller = dbus_message_get_sender (message);
@@ -483,6 +476,7 @@ service_method_create_with_master_password (GkdSecretService *self, DBusMessage
 		                               "Invalid secret argument");
 	}
 
+	gp11_attributes_add_boolean (attrs, CKA_TOKEN, TRUE);
 	reply = gkd_secret_create_without_prompting (self, message, attrs, secret);
 	gp11_attributes_unref (attrs);
 	gkd_secret_secret_free (secret);



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