[couchdb-glib] Use the new credentials object for authentication



commit 7db8f26a232239f1c957feaafd976a784518e6ba
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Sat Jan 16 11:11:23 2010 +0100

    Use the new credentials object for authentication

 couchdb-glib/Makefile.am                 |    8 +-
 couchdb-glib/couchdb-auth.c              |   43 ----------
 couchdb-glib/couchdb-auth.h              |   50 ------------
 couchdb-glib/couchdb-glib.h              |    4 +-
 couchdb-glib/couchdb-session.c           |  127 ++++++++++++++----------------
 couchdb-glib/couchdb-session.h           |   12 +--
 desktopcouch-glib/desktopcouch-session.c |   15 ++--
 tests/test-desktopcouch-glib.c           |   24 ++++++-
 tests/test-oauth.c                       |    5 +-
 9 files changed, 105 insertions(+), 183 deletions(-)
---
diff --git a/couchdb-glib/Makefile.am b/couchdb-glib/Makefile.am
index 1e639e1..53d2806 100644
--- a/couchdb-glib/Makefile.am
+++ b/couchdb-glib/Makefile.am
@@ -25,7 +25,7 @@ couchdb-marshal.c: couchdb-marshal.list $(GLIB_GENMARSHAL)
 	$(GLIB_GENMARSHAL) $< --body --prefix=_couchdb_marshal > $@
 
 libcouchdb_glib_1_0_la_headers =	\
-	couchdb-auth.h			\
+	couchdb-credentials.h		\
 	couchdb-database-info.h		\
 	couchdb-document.h		\
 	couchdb-document-contact.h	\
@@ -38,7 +38,7 @@ libcouchdb_glib_1_0_la_headers =	\
 	utils.h
 
 libcouchdb_glib_1_0_la_sources =	\
-	couchdb-auth.c			\
+	couchdb-credentials.c		\
 	couchdb-database-info.c		\
 	couchdb-document.c		\
 	couchdb-document-contact.c	\
@@ -66,12 +66,12 @@ libcouchdb_glib_1_0_la_LDFLAGS =	\
 
 hdir = $(includedir)/couchdb-glib-1.0
 h_DATA = 				\
-	couchdb-glib.h			\
-	couchdb-auth.h			\
+	couchdb-credentials.h		\
 	couchdb-database-info.h		\
 	couchdb-document-contact.h	\
 	couchdb-document.h		\
 	couchdb-document-info.h		\
+	couchdb-glib.h			\
 	couchdb-session.h		\
 	couchdb-struct-field.h		\
 	couchdb-types.h
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index de227fc..a60745b 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -25,12 +25,12 @@
 #define __COUCHDB_GLIB_H__
 
 #include <couchdb-types.h>
-#include <couchdb-session.h>
-#include <couchdb-auth.h>
+#include <couchdb-credentials.h>
 #include <couchdb-database-info.h>
 #include <couchdb-document.h>
 #include <couchdb-document-contact.h>
 #include <couchdb-document-info.h>
+#include <couchdb-session.h>
 #include <couchdb-struct-field.h>
 
 #endif /* __COUCHDB_GLIB_H__ */
diff --git a/couchdb-glib/couchdb-session.c b/couchdb-glib/couchdb-session.c
index fb3e383..51be181 100644
--- a/couchdb-glib/couchdb-session.c
+++ b/couchdb-glib/couchdb-session.c
@@ -36,18 +36,10 @@
 #endif
 
 struct _CouchdbSessionPrivate {
-	GObject parent;
-
 	char *uri;
 	SoupSession *http_session;
-
 	GHashTable *db_watchlist;
-
-	gboolean oauth_enabled;
-	char *oauth_consumer_key;
-	char *oauth_consumer_secret;
-	char *oauth_token_key;
-	char *oauth_token_secret;	
+	CouchdbCredentials *credentials;
 };
 
 G_DEFINE_TYPE(CouchdbSession, couchdb_session, G_TYPE_OBJECT)
@@ -77,14 +69,8 @@ couchdb_session_finalize (GObject *object)
 	g_free (couchdb->priv->uri);
 	g_object_unref (couchdb->priv->http_session);
 
-	if (couchdb->priv->oauth_consumer_key)
-		g_free (couchdb->priv->oauth_consumer_key);
-	if (couchdb->priv->oauth_consumer_secret)
-		g_free (couchdb->priv->oauth_consumer_secret);
-	if (couchdb->priv->oauth_token_key)
-		g_free (couchdb->priv->oauth_token_key);
-	if (couchdb->priv->oauth_token_secret)
-		g_free (couchdb->priv->oauth_token_secret);
+	if (couchdb->priv->credentials)
+		g_object_unref (G_OBJECT (couchdb->priv->credentials));
 
 	g_free (couchdb->priv);
 
@@ -203,8 +189,8 @@ couchdb_session_init (CouchdbSession *couchdb)
 	couchdb->priv = g_new0 (CouchdbSessionPrivate, 1);
 
 	couchdb->priv->db_watchlist = g_hash_table_new_full (g_str_hash, g_str_equal,
-						       (GDestroyNotify) g_free,
-						       (GDestroyNotify) dbwatch_free);
+							     (GDestroyNotify) g_free,
+							     (GDestroyNotify) dbwatch_free);
 	if (couchdb->priv->uri == NULL)
 		couchdb->priv->uri = g_strdup("http://127.0.0.1:5984";);
 
@@ -212,11 +198,7 @@ couchdb_session_init (CouchdbSession *couchdb)
 		SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_GNOME_FEATURES_2_26,
                 NULL);
 
-	couchdb->priv->oauth_consumer_key = NULL;
-	couchdb->priv->oauth_consumer_secret = NULL;
-	couchdb->priv->oauth_token_key = NULL;
-	couchdb->priv->oauth_token_secret = NULL;
-	couchdb->priv->oauth_enabled = FALSE;
+	couchdb->priv->credentials = NULL;
 
 	soup_session_add_feature_by_type (couchdb->priv->http_session, SOUP_TYPE_LOGGER);
 }
@@ -472,67 +454,70 @@ couchdb_session_listen_for_changes (CouchdbSession *couchdb, const char *dbname)
 }
 
 /**
- * couchdb_session_enable_oauth:
+ * couchdb_session_enable_authentication:
  * @couchdb: A #CouchdbSession object
- * @consumer_key: Consumer key to use
- * @consumer_secret: Consumer secret to use
- * @token_key: Token key to use
- * @token_secret: Token secret to use
- *
- * Enables oAuth signing of all requests for the given #CouchdbSession object.
+ * @credentials: A #CouchdbCredentials object
  *
- * Return value: TRUE if oAuth is enabled in the library or FALSE if not.
+ * Enables authentication for the given #CouchdbSession object.
  */
-gboolean
-couchdb_session_enable_oauth (CouchdbSession *couchdb,
-			      const char *consumer_key,
-			      const char *consumer_secret,
-			      const char *token_key,
-			      const char *token_secret)
+void
+couchdb_session_enable_authentication (CouchdbSession *couchdb,
+				       CouchdbCredentials *credentials)
 {
-	g_return_val_if_fail (COUCHDB_IS_SESSION (couchdb), FALSE);
+	g_return_if_fail (COUCHDB_IS_SESSION (couchdb));
 
-#ifdef HAVE_OAUTH
-	if (couchdb->priv->oauth_enabled) {
-		g_free (couchdb->priv->oauth_consumer_key);
-		g_free (couchdb->priv->oauth_consumer_secret);
-		g_free (couchdb->priv->oauth_token_key);
-		g_free (couchdb->priv->oauth_token_secret);
-	}
+	if (couchdb->priv->credentials)
+		g_object_unref (G_OBJECT (couchdb->priv->credentials));
 
-	couchdb->priv->oauth_consumer_key = g_strdup (consumer_key);
-	couchdb->priv->oauth_consumer_secret = g_strdup (consumer_secret);
-	couchdb->priv->oauth_token_key = g_strdup (token_key);
-	couchdb->priv->oauth_token_secret = g_strdup (token_secret);
-	couchdb->priv->oauth_enabled = TRUE;
+	couchdb->priv->credentials = COUCHDB_CREDENTIALS (g_object_ref (G_OBJECT (credentials)));
+}
 
-	return TRUE;
-#else
-	return FALSE;
-#endif
+/**
+ * couchdb_session_enable_authentication:
+ * @couchdb: A #CouchdbSession object
+ *
+ * Disables authentication for the given #CouchdbSession object.
+ */
+void
+couchdb_session_disable_authentication (CouchdbSession *couchdb)
+{
+	g_return_if_fail (COUCHDB_IS_SESSION (couchdb));
+
+	if (couchdb->priv->credentials) {
+		g_object_unref (G_OBJECT (couchdb->priv->credentials));
+		couchdb->priv->credentials = NULL;
+	}
 }
 
+/**
+ * couchdb_session_is_authentication_enabled:
+ * @couchdb: A #CouchdbSession object
+ *
+ * Gets whether the given #CouchdbSession object has authentication enabled.
+ *
+ * Return value: TRUE if authentication is enabled, FALSE otherwise.
+ */
 gboolean
-couchdb_session_is_oauth_enabled (CouchdbSession *couchdb)
+couchdb_session_is_authentication_enabled (CouchdbSession *couchdb)
 {
 	g_return_val_if_fail (COUCHDB_IS_SESSION (couchdb), FALSE);
 	
-	return couchdb->priv->oauth_enabled;
+	return couchdb->priv->credentials != NULL;
 }
 
-
 static void
-couchdb_session_add_oauth_signature (CouchdbSession *couchdb, SoupMessage *http_message, const char *method, const char *url)
+add_oauth_signature (CouchdbSession *couchdb, SoupMessage *http_message, const char *method, const char *url)
 {
-	/* This method is a no-op if we are configured without OAUTH */
 #ifdef HAVE_OAUTH
+	/* This method is a no-op if we are configured without OAUTH */
 	char *signed_url;
 
-	signed_url = oauth_sign_url2 (url, NULL, OA_HMAC, method,
-				      couchdb->priv->oauth_consumer_key,
-				      couchdb->priv->oauth_consumer_secret,
-				      couchdb->priv->oauth_token_key,
-				      couchdb->priv->oauth_token_secret);
+	signed_url = oauth_sign_url2 (
+		url, NULL, OA_HMAC, method,
+		couchdb_credentials_get_item (couchdb->priv->credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_CONSUMER_KEY),
+		couchdb_credentials_get_item (couchdb->priv->credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_CONSUMER_SECRET),
+		couchdb_credentials_get_item (couchdb->priv->credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_TOKEN_KEY),
+		couchdb_credentials_get_item (couchdb->priv->credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_TOKEN_SECRET));
 	if (signed_url != NULL) {
 		char **parsed_url;
 		GString *header = NULL;
@@ -644,9 +629,15 @@ couchdb_session_send_message (CouchdbSession *couchdb, const char *method, const
 					  body, strlen (body));
 	}
 
-	if (couchdb_session_is_oauth_enabled (couchdb))
-		couchdb_session_add_oauth_signature (couchdb, http_message, method, url);
-	
+	if (couchdb_session_is_authentication_enabled (couchdb)) {
+		switch (couchdb_credentials_get_auth_type (couchdb->priv->credentials)) {
+		case COUCHDB_CREDENTIALS_TYPE_OAUTH:
+			add_oauth_signature (couchdb, http_message, method, url);
+			break;
+		default:
+			g_warning ("Got unknown credentials object, not authenticating message");
+		}
+	}
 
 	g_debug ("Sending %s to %s... with headers\n: ", method, url);
 	soup_message_headers_foreach (http_message->request_headers,
diff --git a/couchdb-glib/couchdb-session.h b/couchdb-glib/couchdb-session.h
index 8fe982b..97a1657 100644
--- a/couchdb-glib/couchdb-session.h
+++ b/couchdb-glib/couchdb-session.h
@@ -28,6 +28,7 @@
 #include <glib-object.h>
 #include <json-glib/json-glib.h>
 #include "couchdb-types.h"
+#include "couchdb-credentials.h"
 #include "couchdb-database-info.h"
 
 G_BEGIN_DECLS
@@ -73,14 +74,9 @@ gboolean             couchdb_session_delete_database (CouchdbSession *couchdb, c
 
 void                 couchdb_session_listen_for_changes (CouchdbSession *couchdb, const char *dbname);
 
-gboolean             couchdb_session_enable_oauth (CouchdbSession *couchdb,
-						   const char *consumer_key,
-						   const char *consumer_secret,
-						   const char *token_key,
-						   const char *token_secret);
-void                 couchdb_session_disable_oauth (CouchdbSession *couchdb);
-
-gboolean             couchdb_session_is_oauth_enabled (CouchdbSession *couchdb);
+void                 couchdb_session_enable_authentication (CouchdbSession *couchdb, CouchdbCredentials *credentials);
+void                 couchdb_session_disable_authentication (CouchdbSession *couchdb);
+gboolean             couchdb_session_is_authentication_enabled (CouchdbSession *couchdb);
 
 gboolean             couchdb_session_send_message (CouchdbSession *couchdb, const char *method, const char *url, const char *body, JsonParser *parser, GError **error);
 
diff --git a/desktopcouch-glib/desktopcouch-session.c b/desktopcouch-glib/desktopcouch-session.c
index af3a641..9aad5f7 100644
--- a/desktopcouch-glib/desktopcouch-session.c
+++ b/desktopcouch-glib/desktopcouch-session.c
@@ -101,6 +101,7 @@ desktopcouch_session_new (void)
 			gchar **items;
 			char *oauth_c_key = NULL, *oauth_c_secret = NULL, *oauth_t_key = NULL, *oauth_t_secret = NULL;
 			DesktopcouchSession *dc;
+			CouchdbCredentials *credentials;
 			GnomeKeyringFound *first_item = (GnomeKeyringFound *) items_found->data;
 
 			items = g_strsplit (first_item->secret, ":", 4);
@@ -116,18 +117,20 @@ desktopcouch_session_new (void)
 
 			/* Enable OAuth on this connection */
 			dc = DESKTOPCOUCH_SESSION (g_object_new (DESKTOPCOUCH_TYPE_SESSION, "uri", uri, NULL));
-			couchdb_session_enable_oauth (COUCHDB_SESSION (dc),
-						      oauth_c_key,
-						      oauth_c_secret,
-						      oauth_t_key,
-						      oauth_t_secret);
 
+			credentials = couchdb_credentials_new_with_oauth (oauth_c_key,
+									  oauth_c_secret,
+									  oauth_t_key,
+									  oauth_t_secret);
+			couchdb_session_enable_authentication (COUCHDB_SESSION (dc), credentials);
+
+			/* Free memory */
 			g_free (oauth_c_key);
 			g_free (oauth_c_secret);
 			g_free (oauth_t_key);
 			g_free (oauth_t_secret);
-
 			g_free (uri);
+			g_object_unref (G_OBJECT (credentials));
 
 			return dc;
 		} else {
diff --git a/tests/test-desktopcouch-glib.c b/tests/test-desktopcouch-glib.c
index d8bb659..a99c328 100644
--- a/tests/test-desktopcouch-glib.c
+++ b/tests/test-desktopcouch-glib.c
@@ -26,11 +26,32 @@ static DesktopcouchSession *dc = NULL;
 static void
 test_connect_desktopcouch (void)
 {
+	/* Create desktopcouch session */
 	dc = desktopcouch_session_new ();
-
 	g_assert (DESKTOPCOUCH_IS_SESSION (dc));
 }
 
+static void
+test_list_databases (void)
+{
+	GSList *dblist, *sl;
+	GError *error = NULL;
+
+	/* List databases */
+	dblist = couchdb_session_list_databases (COUCHDB_SESSION (dc), &error);
+	if (error != NULL) {
+		g_warning ("Error listing databases: %s", error->message);
+		g_error_free (error);
+		g_assert (error == NULL);
+	}
+
+	for (sl = dblist; sl != NULL; sl = sl->next)
+		g_print ("Found database %s\n", (const char *) sl->data);
+
+	/* Free memory */
+	couchdb_session_free_database_list (dblist);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -39,6 +60,7 @@ main (int argc, char *argv[])
 	g_test_init (&argc, &argv, NULL);
 
 	g_test_add_func ("/testdesktopcouchglib/Connect", test_connect_desktopcouch);
+	g_test_add_func ("/testdesktopcouchglib/ListDatabases", test_list_databases);
 
 	return g_test_run ();
 }
diff --git a/tests/test-oauth.c b/tests/test-oauth.c
index 4d82f7e..34c3073 100644
--- a/tests/test-oauth.c
+++ b/tests/test-oauth.c
@@ -67,6 +67,7 @@ main (int argc, char *argv[])
 	char *liboauth_signed;
 	char *command_line, *command_line_output;
 	CouchdbSession *couchdb;
+	CouchdbCredentials *credentials;
 	GSList *db_list;
 	GError *error = NULL;
 
@@ -112,7 +113,9 @@ main (int argc, char *argv[])
 	g_thread_init (NULL);
 
 	couchdb = couchdb_session_new (url);
-	couchdb_session_enable_oauth (couchdb, c_key, c_secret, t_key, t_secret);
+	credentials = couchdb_credentials_new_with_oauth (c_key, c_secret, t_key, t_secret);
+	couchdb_session_enable_authentication (couchdb, credentials);
+	g_object_unref (G_OBJECT (credentials));
 
 	db_list = couchdb_session_list_databases (couchdb, &error);
 	if (db_list != NULL) {



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