[couchdb-glib] Added CouchdbCredentials object to store credentials



commit 31721d82e0a41b2199530677d63e651143720772
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Sat Jan 16 11:11:02 2010 +0100

    Added CouchdbCredentials object to store credentials

 couchdb-glib/couchdb-credentials.c |  148 ++++++++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-credentials.h |   71 +++++++++++++++++
 2 files changed, 219 insertions(+), 0 deletions(-)
---
diff --git a/couchdb-glib/couchdb-credentials.c b/couchdb-glib/couchdb-credentials.c
new file mode 100644
index 0000000..04a882f
--- /dev/null
+++ b/couchdb-glib/couchdb-credentials.c
@@ -0,0 +1,148 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "couchdb-credentials.h"
+
+struct _CouchdbCredentialsPrivate {
+	CouchdbCredentialsType type;
+	GHashTable *auth_data;
+};
+
+G_DEFINE_TYPE(CouchdbCredentials, couchdb_credentials, G_TYPE_OBJECT)
+
+static void
+couchdb_credentials_finalize (GObject *object)
+{
+	CouchdbCredentials *credentials = COUCHDB_CREDENTIALS (object);
+
+	if (credentials->priv != NULL) {
+		if (credentials->priv->auth_data != NULL) {
+			g_hash_table_destroy (credentials->priv->auth_data);
+			credentials->priv->auth_data = NULL;
+		}
+
+		g_free (credentials->priv);
+		credentials->priv = NULL;
+	}
+
+	G_OBJECT_CLASS (couchdb_credentials_parent_class)->finalize (object);
+}
+
+static void
+couchdb_credentials_class_init (CouchdbCredentialsClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	object_class->finalize = couchdb_credentials_finalize;
+}
+
+static void
+couchdb_credentials_init (CouchdbCredentials *credentials)
+{
+	credentials->priv = g_new0 (CouchdbCredentialsPrivate, 1);
+
+	credentials->priv->type = COUCHDB_CREDENTIALS_TYPE_UNKNOWN;
+	credentials->priv->auth_data = g_hash_table_new_full (g_str_hash, g_str_equal,
+							      (GDestroyNotify) g_free,
+							      (GDestroyNotify) g_free);
+}
+
+/**
+ * couchdb_credentials_new_with_oauth:
+ * @consumer_key: OAuth consumer key
+ * @consumer_secret: OAuth consumer secret
+ * @token_key: OAuth token key
+ * @token_secret: OAuth token secret
+ *
+ * Create a new #CouchdbCredentials object to be used for OAuth authentication.
+ *
+ * Return value: A #CouchdbCredentials object.
+ */
+CouchdbCredentials *
+couchdb_credentials_new_with_oauth (const gchar *consumer_key,
+				    const gchar *consumer_secret,
+				    const gchar *token_key,
+				    const gchar *token_secret)
+{
+	CouchdbCredentials *credentials;
+
+	credentials = COUCHDB_CREDENTIALS (g_object_new (COUCHDB_TYPE_CREDENTIALS, NULL));
+	credentials->priv->type = COUCHDB_CREDENTIALS_TYPE_OAUTH;
+	couchdb_credentials_set_item (credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_CONSUMER_KEY, consumer_key);
+	couchdb_credentials_set_item (credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_CONSUMER_SECRET, consumer_secret);
+	couchdb_credentials_set_item (credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_TOKEN_KEY, token_key);
+	couchdb_credentials_set_item (credentials, COUCHDB_CREDENTIALS_ITEM_OAUTH_TOKEN_SECRET, token_secret);
+
+	return credentials;
+}
+
+/**
+ * couchdb_credentials_get_auth_type:
+ * @credentials: A #CouchdbCredentials object
+ *
+ * Retrieve the type of authentication defined for the given #CouchdbCredentials object.
+ *
+ * Return value: A #CouchdbCredentialsType specifying the type of authentication.
+ */
+CouchdbCredentialsType
+couchdb_credentials_get_auth_type (CouchdbCredentials *credentials)
+{
+	g_return_val_if_fail (COUCHDB_IS_CREDENTIALS (credentials), COUCHDB_CREDENTIALS_TYPE_UNKNOWN);
+
+	return credentials->priv->type;
+}
+
+/**
+ * couchdb_credentials_get_item:
+ * @credentials: A #CouchdbCredentials object
+ * @item: Name of the item to retrieve
+ *
+ * Get the value associated with one authentication item on the given #CouchdbCredentials object.
+ * The valid values are the COUCHDB_CREDENTIALS_ITEM_* values defined in couchdb-credentials.h.
+ *
+ * Return value: The value associated with the given item, or NULL if not found.
+ */
+const gchar *
+couchdb_credentials_get_item (CouchdbCredentials *credentials, const gchar *item)
+{
+	g_return_val_if_fail (COUCHDB_IS_CREDENTIALS (credentials), NULL);
+
+	return (const gchar *) g_hash_table_lookup (credentials->priv->auth_data, item);
+}
+
+/**
+ * couchdb_credentials_set_item:
+ * @credentials: A #CouchdbCredentials object
+ * @item: Name of the item to set the value of
+ * #value: Value of the item
+ *
+ * Set the value associated with one authentication item on the given #CouchdbCredentials object.
+ * The valid values are the COUCHDB_CREDENTIALS_ITEM_* values defined in couchdb-credentials.h.
+ */
+void
+couchdb_credentials_set_item (CouchdbCredentials *credentials, const gchar *item, const gchar *value)
+{
+	g_return_if_fail (COUCHDB_IS_CREDENTIALS (credentials));
+
+	g_hash_table_insert (credentials->priv->auth_data,
+			     g_strdup (item),
+			     g_strdup (value));
+}
diff --git a/couchdb-glib/couchdb-credentials.h b/couchdb-glib/couchdb-credentials.h
new file mode 100644
index 0000000..a891869
--- /dev/null
+++ b/couchdb-glib/couchdb-credentials.h
@@ -0,0 +1,71 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __COUCHDB_CREDENTIALS_H__
+#define __COUCHDB_CREDENTIALS_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define COUCHDB_TYPE_CREDENTIALS                (couchdb_credentials_get_type ())
+#define COUCHDB_CREDENTIALS(obj)                (G_TYPE_CHECK_INSTANCE_CAST ((obj), COUCHDB_TYPE_CREDENTIALS, CouchdbCredentials))
+#define COUCHDB_IS_CREDENTIALS(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COUCHDB_TYPE_CREDENTIALS))
+#define COUCHDB_CREDENTIALS_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST ((klass), COUCHDB_TYPE_CREDENTIALS, CouchdbCredentialsClass))
+#define COUCHDB_IS_CREDENTIALS_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE ((klass), COUCHDB_TYPE_CREDENTIALS))
+#define COUCHDB_CREDENTIALS_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), COUCHDB_TYPE_CREDENTIALS, CouchdbCredentialsClass))
+
+typedef struct _CouchdbCredentialsPrivate CouchdbCredentialsPrivate;
+
+typedef struct {
+	GObject parent;
+	CouchdbCredentialsPrivate *priv;
+} CouchdbCredentials;
+
+typedef struct {
+	GObjectClass parent_class;
+} CouchdbCredentialsClass;
+
+typedef enum {
+	COUCHDB_CREDENTIALS_TYPE_UNKNOWN = -1,
+	COUCHDB_CREDENTIALS_TYPE_OAUTH
+} CouchdbCredentialsType;
+
+GType                  couchdb_credentials_get_type (void);
+CouchdbCredentials    *couchdb_credentials_new_with_oauth (const gchar *consumer_key,
+							   const gchar *consumer_secret,
+							   const gchar *token_key,
+							   const gchar *token_secret);
+CouchdbCredentialsType couchdb_credentials_get_auth_type (CouchdbCredentials *credentials);
+
+#define COUCHDB_CREDENTIALS_ITEM_OAUTH_CONSUMER_KEY    "oauth_consumer_key"
+#define COUCHDB_CREDENTIALS_ITEM_OAUTH_CONSUMER_SECRET "oauth_consumer_secret"
+#define COUCHDB_CREDENTIALS_ITEM_OAUTH_TOKEN_KEY       "oauth_token_key"
+#define COUCHDB_CREDENTIALS_ITEM_OAUTH_TOKEN_SECRET    "oauth_token_secret"
+
+const gchar           *couchdb_credentials_get_item (CouchdbCredentials *credentials, const gchar *item);
+void                   couchdb_credentials_set_item (CouchdbCredentials *credentials,
+						     const gchar *item,
+						     const gchar *value);
+
+G_END_DECLS
+
+#endif /* __COUCHDB_CREDENTIALS_H__ */



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