[grilo] test-ui: use new flickr plugin
- From: Juan A. Suarez Romero <jasuarez src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [grilo] test-ui: use new flickr plugin
- Date: Tue, 23 Apr 2013 08:29:45 +0000 (UTC)
commit 21339e9723f62aee1c68bc341773388df52bc2b6
Author: Marek Chalupa <mchalupa redhat com>
Date: Mon Apr 22 09:19:25 2013 +0200
test-ui: use new flickr plugin
flickr-auth.[ch] was replaced by their oauth alternatives and main.c was
rewritten to use them.
Into authenticate flickr popup window was added text entry to enter the
verifier given by flickr.
https://bugzilla.gnome.org/show_bug.cgi?id=697175
Signed-off-by: Juan A. Suarez Romero <jasuarez igalia com>
configure.ac | 8 +-
tools/grilo-test-ui/Makefile.am | 6 +-
tools/grilo-test-ui/flickr-auth.c | 257 ------------------------------
tools/grilo-test-ui/flickr-auth.h | 43 -----
tools/grilo-test-ui/flickr-oauth.c | 317 +++++++++++++++++++++++++++++++++++++
tools/grilo-test-ui/flickr-oauth.h | 90 +++++++++++
tools/grilo-test-ui/main.c | 44 +++--
7 files changed, 449 insertions(+), 316 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 0918b54..8bcc4d2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -121,6 +121,7 @@ PKG_CHECK_MODULES(DEPS, glib-2.0 >= 2.29.10 \
gio-2.0 \
libxml-2.0)
+
GLIB_GENMARSHAL=`pkg-config --variable=glib_genmarshal glib-2.0`
GLIB_MKENUMS=`pkg-config --variable=glib_mkenums glib-2.0`
AC_SUBST(GLIB_GENMARSHAL)
@@ -134,6 +135,8 @@ PKG_CHECK_MODULES([GTU], [ gtk+-3.0 >= 3.0 ],
[ HAVE_GTK=yes ],
[ HAVE_GTK=no ])
+PKG_CHECK_MODULES(OAUTH, oauth, [HAVE_OAUTH=yes], [HAVE_OAUTH=no])
+
AC_ARG_ENABLE(test-ui,
AS_HELP_STRING([--enable-test-ui],
[Build Test UI (default: auto)]),
@@ -143,6 +146,9 @@ AC_ARG_ENABLE(test-ui,
if test "x$HAVE_GTK" = "xno"; then
AC_MSG_ERROR([gtk+-3.0 >= 3.0 not found, install it or use --disable-test-ui])
fi
+ if test "x$HAVE_OAUTH" = "xno"; then
+ AC_MSG_ERROR([OAuth not found, install it or use --disable-test-ui])
+ fi
;;
no)
HAVE_GTK=no
@@ -150,7 +156,7 @@ AC_ARG_ENABLE(test-ui,
esac
])
-AM_CONDITIONAL(BUILD_GRILO_TEST_UI, test "x$HAVE_GTK" = "xyes")
+AM_CONDITIONAL(BUILD_GRILO_TEST_UI, test "x$HAVE_GTK" = "xyes" -a "x$HAVE_OAUTH" = "xyes")
# ----------------------------------------------------------
# NET LIBRARY
diff --git a/tools/grilo-test-ui/Makefile.am b/tools/grilo-test-ui/Makefile.am
index 35ca65a..f0bd832 100644
--- a/tools/grilo-test-ui/Makefile.am
+++ b/tools/grilo-test-ui/Makefile.am
@@ -11,12 +11,13 @@ bin_PROGRAMS = grilo-test-ui- GRL_MAJORMINOR@
grilo_test_ui_ GRL_MAJORMINOR@_SOURCES = \
main.c \
- flickr-auth.h \
- flickr-auth.c
+ flickr-oauth.h \
+ flickr-oauth.c
grilo_test_ui_ GRL_MAJORMINOR@_CFLAGS = \
-DPREFIX=$(prefix) \
$(GTU_CFLAGS) \
+ $(OAUTH_CFLAGS) \
$(GRL_FLICKR_CFLAGS) \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/data
@@ -24,6 +25,7 @@ grilo_test_ui_ GRL_MAJORMINOR@_CFLAGS = \
grilo_test_ui_ GRL_MAJORMINOR@_LDADD = \
$(DEPS_LIBS) \
$(GTU_LIBS) \
+ $(OAUTH_LIBS) \
$(GRL_FLICKR_LIBS) \
$(top_builddir)/src/lib GRL_NAME@.la
diff --git a/tools/grilo-test-ui/flickr-oauth.c b/tools/grilo-test-ui/flickr-oauth.c
new file mode 100644
index 0000000..90679d2
--- /dev/null
+++ b/tools/grilo-test-ui/flickr-oauth.c
@@ -0,0 +1,317 @@
+#include "flickr-oauth.h"
+
+#include <glib.h>
+#include <stdlib.h>
+
+#include <string.h>
+#include <oauth.h>
+
+/* ---------- private functions declarations ---------- */
+
+static gchar *
+parse_request_token (const gchar *string, gchar **secret);
+
+static gchar *
+parse_access_token (const gchar *string, gchar **secret);
+
+static gchar *
+parse_token (const gint parts_no, const gchar *string, gchar **secret);
+
+static gchar *
+get_param_value (const gchar *string);
+
+static gchar *
+get_timestamp (void);
+
+static void
+free_params (gchar **params, gint params_no);
+
+/* ---------- public API ---------- */
+
+gchar *
+flickroauth_get_signature (const gchar *consumer_secret,
+ const gchar *token_secret,
+ const gchar *url,
+ gchar **params,
+ gint params_no)
+{
+ gchar *params_string;
+ gchar *base_string;
+ gchar *encryption_key;
+ gchar *signature;
+
+ qsort (params, params_no, sizeof (gchar *), oauth_cmpstringp);
+
+ params_string = oauth_serialize_url (params_no, 0, params);
+
+ base_string = oauth_catenc (3, FLICKR_OAUTH_HTTP_METHOD,
+ url, params_string);
+
+ g_free (params_string);
+
+ if (token_secret == NULL)
+ encryption_key = g_strdup_printf ("%s&", consumer_secret);
+ else
+ encryption_key = g_strdup_printf ("%s&%s", consumer_secret, token_secret);
+
+ signature = oauth_sign_hmac_sha1 (base_string, encryption_key);
+
+ g_free (encryption_key);
+ g_free (base_string);
+
+ return signature;
+}
+
+gchar *
+flickroauth_get_request_token (const gchar *consumer_key,
+ const gchar *consumer_secret,
+ gchar **secret)
+{
+ gchar *signature;
+ gchar *url;
+ gchar *timestamp;
+ gchar *nonce;
+ gchar *params[7];
+ gchar *params_string; /* one string later created from params[] */
+ gchar *http_reply;
+
+ timestamp = get_timestamp ();
+ nonce = oauth_gen_nonce ();
+
+ params[0] = g_strdup_printf ("oauth_callback=%s", FLICKR_OAUTH_CALLBACK);
+ params[1] = g_strdup_printf ("oauth_consumer_key=%s", consumer_key);
+ params[2] = g_strdup_printf ("oauth_nonce=%s", nonce);
+ params[3] = g_strdup_printf ("oauth_signature_method=%s",
+ FLICKR_OAUTH_SIGNATURE_METHOD);
+ params[4] = g_strdup_printf ("oauth_timestamp=%s", timestamp);
+ params[5] = g_strdup_printf ("oauth_version=%s", FLICKR_OAUTH_VERSION);
+
+
+ g_free (timestamp);
+ g_free (nonce);
+
+ signature = flickroauth_get_signature (consumer_secret, NULL,
+ FLICKR_OAUTH_REQUESTTOKEN_URL,
+ params, 6);
+
+ params[6] = g_strdup_printf ("oauth_signature=%s", signature);
+ g_free (signature);
+
+ params_string = oauth_serialize_url (7, 0, params);
+
+ free_params (params, 7);
+
+ url = g_strdup_printf ("%s?%s", FLICKR_OAUTH_REQUESTTOKEN_URL, params_string);
+ g_free (params_string);
+
+ http_reply = oauth_http_get2 (url, NULL, NULL);
+ g_free (url);
+
+ return parse_request_token (http_reply, secret);
+}
+
+gchar *
+flickroauth_get_access_token (const gchar *consumer_key,
+ const gchar *consumer_secret,
+ const gchar *oauth_token,
+ const gchar *oauth_token_secret,
+ const gchar *verifier,
+ gchar **secret)
+{
+ gchar *signature;
+ gchar *url;
+ gchar *timestamp;
+ gchar *nonce;
+ gchar *params[8];
+ gchar *params_string; /* one string later created from params[] */
+ gchar *http_reply;
+
+ timestamp = get_timestamp ();
+ nonce = oauth_gen_nonce ();
+
+ params[0] = g_strdup_printf ("oauth_verifier=%s", verifier);
+ params[1] = g_strdup_printf ("oauth_consumer_key=%s", consumer_key);
+ params[2] = g_strdup_printf ("oauth_nonce=%s", nonce);
+ params[3] = g_strdup_printf ("oauth_signature_method=%s",
+ FLICKR_OAUTH_SIGNATURE_METHOD);
+ params[4] = g_strdup_printf ("oauth_timestamp=%s", timestamp);
+ params[5] = g_strdup_printf ("oauth_version=%s", FLICKR_OAUTH_VERSION);
+ params[6] = g_strdup_printf ("oauth_token=%s", oauth_token);
+
+ g_free (timestamp);
+ g_free (nonce);
+
+ signature = flickroauth_get_signature (consumer_secret,
+ oauth_token_secret,
+ FLICKR_OAUTH_ACCESSTOKEN_URL,
+ params, 7);
+
+ params[7] = g_strdup_printf ("oauth_signature=%s", signature);
+ g_free (signature);
+
+ params_string = oauth_serialize_url (8, 0, params);
+
+ free_params (params, 8);
+
+ url = g_strdup_printf ("%s?%s", FLICKR_OAUTH_ACCESSTOKEN_URL, params_string);
+ g_free (params_string);
+
+ http_reply = oauth_http_get2 (url, NULL, NULL);
+ g_free (url);
+
+ return parse_access_token (http_reply, secret);
+}
+
+gchar *
+flickroauth_create_api_url (const gchar *consumer_key,
+ const gchar *consumer_secret,
+ const gchar *oauth_token,
+ const gchar *oauth_token_secret,
+ gchar **params,
+ const guint params_no)
+{
+ guint i;
+ gchar *nonce;
+ gchar *timestamp;
+ gchar *signature;
+ gchar *url;
+ gchar *params_string;
+
+ g_return_val_if_fail (consumer_key, NULL);
+
+ /* handle Non-authorised call */
+ if (oauth_token == NULL)
+ {
+ params_string = oauth_serialize_url (params_no, 0, params);
+
+ url = g_strdup_printf ("%s?api_key=%s&%s", FLICKR_API_URL,
+ consumer_key,
+ params_string);
+
+ g_free (params_string);
+
+ return url;
+ }
+
+ /* there are 7 pre-filled parameters in authorize call*/
+ guint params_all_no = params_no + 7;
+ gchar **params_all = g_malloc ((params_all_no) * sizeof (gchar *));
+
+ if (params_all == NULL)
+ return NULL;
+
+ nonce = oauth_gen_nonce ();
+ timestamp = get_timestamp ();
+
+ params_all[0] = g_strdup_printf ("oauth_nonce=%s", nonce);
+ params_all[1] = g_strdup_printf ("oauth_timestamp=%s", timestamp);
+ params_all[2] = g_strdup_printf ("oauth_consumer_key=%s", consumer_key);
+ params_all[3] = g_strdup_printf ("oauth_signature_method=%s",
+ FLICKR_OAUTH_SIGNATURE_METHOD);
+ params_all[4] = g_strdup_printf ("oauth_version=%s", FLICKR_OAUTH_VERSION);
+ params_all[5] = g_strdup_printf ("oauth_token=%s", oauth_token);
+
+ /* copy user parameters to the params_all */
+ for (i = 0; i < params_no; i++)
+ params_all[7 + i - 1] = g_strdup (params[i]);
+
+ g_free (nonce);
+ g_free (timestamp);
+
+ signature = flickroauth_get_signature (consumer_secret,
+ oauth_token_secret,
+ FLICKR_API_URL, params_all,
+ params_all_no - 1);
+
+ params_all[params_all_no - 1] = g_strdup_printf ("oauth_signature=%s",
+ signature);
+ g_free (signature);
+
+ params_string = oauth_serialize_url (params_all_no, 0, params_all);
+
+ free_params (params_all, params_all_no);
+ g_free (params_all);
+
+ url = g_strdup_printf ("%s?%s", FLICKR_API_URL, params_string);
+
+ return url;
+}
+
+inline gchar *
+flickroauth_authorization_url (const gchar *oauth_token, const gchar *perms)
+{
+ gchar *url;
+ if (perms == NULL)
+ url = g_strdup_printf ("%s?oauth_token=%s", FLICKR_OAUTH_AUTHPOINT,
+ oauth_token);
+ else
+ url = g_strdup_printf ("%s?oauth_token=%s&perms=%s", FLICKR_OAUTH_AUTHPOINT,
+ oauth_token, perms);
+
+ return url;
+}
+
+/* ---------- private functions ---------- */
+
+inline static gchar *
+get_timestamp (void)
+{
+ GTimeVal tm;
+ g_get_current_time (&tm);
+
+ return g_strdup_printf ("%lu", tm.tv_sec);
+}
+
+static gchar
+*get_param_value (const gchar *string)
+{
+ gchar *eq = strchr (string, '=');
+
+ if (eq == NULL)
+ return NULL;
+
+ return g_strdup (eq + 1);
+}
+
+static gchar *
+parse_token (const gint parts_no, const gchar *string, gchar **secret)
+{
+ gchar **array;
+ gchar *token = NULL;
+ gint i = 0;
+
+ array = g_strsplit (string, "&", parts_no);
+
+ while (array[i] != NULL) {
+ if (g_str_has_prefix (array[i], "oauth_token_secret"))
+ (*secret) = get_param_value (array[i]);
+ else if (g_str_has_prefix (array[i], "oauth_token"))
+ token = get_param_value(array[i]);
+
+ i++;
+ }
+
+ g_strfreev (array);
+
+ return token;
+}
+
+inline static gchar *
+parse_access_token (const gchar *string, gchar **secret)
+{
+ return parse_token(5, string, secret);
+}
+
+inline static gchar *
+parse_request_token (const gchar *string, gchar **secret)
+{
+ return parse_token(3, string, secret);
+}
+
+static void
+free_params (gchar **params, gint params_no)
+{
+ gint i;
+ for (i = 0; i < params_no; i++)
+ g_free (params[i]);
+}
diff --git a/tools/grilo-test-ui/flickr-oauth.h b/tools/grilo-test-ui/flickr-oauth.h
new file mode 100644
index 0000000..1b38431
--- /dev/null
+++ b/tools/grilo-test-ui/flickr-oauth.h
@@ -0,0 +1,90 @@
+/*
+ * Flickr authentication library (using OAuth)
+ *
+ * Authors: Marek Chalupa <mchalupa redhat com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser 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 St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include <glib.h>
+
+#ifndef FLICKR_OAUTH_H
+#define FLICKR_OAUTH_H
+
+#define FLICKR_API_URL "http://api.flickr.com/services/rest"
+#define FLICKR_OAUTH_ACCESSTOKEN_URL "http://www.flickr.com/services/oauth/access_token"
+#define FLICKR_OAUTH_REQUESTTOKEN_URL "http://www.flickr.com/services/oauth/request_token"
+#define FLICKR_OAUTH_AUTHPOINT "http://www.flickr.com/services/oauth/authorize"
+
+/* OAuth definitions */
+#define FLICKR_OAUTH_SIGNATURE_METHOD "HMAC-SHA1"
+#define FLICKR_OAUTH_VERSION "1.0"
+#define FLICKR_OAUTH_CALLBACK "oob"
+#define FLICKR_OAUTH_HTTP_METHOD "GET"
+
+/* ------------------------------------------------------------- */
+#define DEBUG
+/* for debugging */
+#ifdef DEBUG
+
+/*
+gchar *dev_consumer_key = "e6b8c86373f5e44e78fed8805e675ede";
+gchar *dev_consumer_secret = "39cea595d57bb9f5";
+
+#include <stdio.h>
+#define dbg(A, ...) fprintf (stderr, "-- (DBG) %s:%d --\n" A "\n", __FUNCTION__,\
+ __LINE__, __VA_ARGS__)
+
+*/
+#endif
+/* ------------------------------------------------------------- */
+
+
+/* public API */
+
+gchar *
+flickroauth_get_signature (const gchar *consumer_secret,
+ const gchar *token_secret,
+ const gchar *url,
+ gchar **params,
+ gint params_no);
+
+gchar *
+flickroauth_get_request_token (const gchar *consumer_key,
+ const gchar *consumer_secret,
+ gchar **secret);
+
+gchar *
+flickroauth_get_access_token (const gchar *consumer_key,
+ const gchar *consumer_secret,
+ const gchar *oauth_token,
+ const gchar *oauth_token_secret,
+ const gchar *verifier,
+ gchar **secret);
+
+gchar *
+flickroauth_create_api_url (const gchar *consumer_key,
+ const gchar *consumer_secret,
+ const gchar *oauth_token,
+ const gchar *oauth_token_secret,
+ gchar **params,
+ const guint params_no);
+
+gchar *
+flickroauth_authorization_url (const gchar *oauth_token, const gchar *perms);
+
+#endif /* FLICKR_OAUTH_H */
diff --git a/tools/grilo-test-ui/main.c b/tools/grilo-test-ui/main.c
index 07a0a36..19c5369 100644
--- a/tools/grilo-test-ui/main.c
+++ b/tools/grilo-test-ui/main.c
@@ -25,12 +25,12 @@
#include <config.h>
+#include "flickr-oauth.h"
+
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <string.h>
-#include "flickr-auth.h"
-
#define GRL_LOG_DOMAIN_DEFAULT test_ui_log_domain
GRL_LOG_DOMAIN_STATIC(test_ui_log_domain);
@@ -1394,7 +1394,7 @@ get_config_dir (void)
}
static gchar *
-load_flickr_token (void)
+load_flickr_token (gchar **secret)
{
GKeyFile *keyfile;
gchar *path;
@@ -1415,6 +1415,7 @@ load_flickr_token (void)
goto bailout;
token = g_key_file_get_value (keyfile, "flickr", "auth-token", NULL);
+ (*secret) = g_key_file_get_value (keyfile, "flickr", "auth-token-secret", NULL);
bailout:
g_free (file);
@@ -1423,7 +1424,7 @@ bailout:
}
static void
-save_flickr_token (const gchar *token)
+save_flickr_token (const gchar *token, const gchar *secret)
{
GKeyFile *keyfile;
gchar *path;
@@ -1441,6 +1442,7 @@ save_flickr_token (const gchar *token)
keyfile = g_key_file_new ();
g_key_file_load_from_file (keyfile, file, G_KEY_FILE_NONE, NULL);
g_key_file_set_value (keyfile, "flickr", "auth-token", token);
+ g_key_file_set_value (keyfile, "flickr", "auth-token-secret", secret);
{
GError *error = NULL;
@@ -1494,18 +1496,24 @@ authorize_flickr (void)
GtkWidget *fail_dialog;
GtkWidget *label;
GtkWidget *view;
+ GtkWidget *input;
+ const gchar *verifier;
gchar *markup;
gchar *token = NULL;
gchar *login_link;
GtkWidget *ok_button;
- gchar *frob = flickr_get_frob (FLICKR_KEY, FLICKR_SECRET);
- if (!frob) {
- GRL_WARNING ("Unable to obtain a Flickr's frob");
+ gchar *rt_secret;
+ gchar *at_secret;
+
+ gchar *rt = flickroauth_get_request_token (FLICKR_KEY, FLICKR_SECRET, &rt_secret);
+ if (!rt) {
+ GRL_WARNING ("Unable to obtain a Flickr's Request Key");
return NULL;
}
- login_link = flickr_get_login_link (FLICKR_KEY, FLICKR_SECRET, frob, "read");
+ login_link = flickroauth_authorization_url (rt, "read");
+
view = gtk_text_view_new ();
gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)),
FLICKR_AUTHORIZE_MSG,
@@ -1522,6 +1530,8 @@ authorize_flickr (void)
gtk_label_set_markup (GTK_LABEL (label), markup);
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+ input = gtk_entry_new ();
+
dialog =
gtk_dialog_new_with_buttons ("Authorize Flickr access",
GTK_WINDOW (gtk_widget_get_parent_window (view)),
@@ -1530,6 +1540,7 @@ authorize_flickr (void)
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), view, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), label, TRUE, TRUE, 0);
+ gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), input, TRUE, TRUE, 0);
ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_OK, GTK_RESPONSE_OK);
gtk_widget_set_sensitive (ok_button, FALSE);
@@ -1542,9 +1553,10 @@ authorize_flickr (void)
gtk_widget_show_all (dialog);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
- token = flickr_get_token (FLICKR_KEY, FLICKR_SECRET, frob);
+ verifier = gtk_entry_get_text (GTK_ENTRY (input));
+ token = flickroauth_get_access_token (FLICKR_KEY, FLICKR_SECRET, rt, rt_secret, verifier, &at_secret);
if (token) {
- save_flickr_token (token);
+ save_flickr_token (token, at_secret);
} else {
fail_dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_parent_window (view)),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
@@ -1557,7 +1569,10 @@ authorize_flickr (void)
}
gtk_widget_destroy (dialog);
- g_free (frob);
+ g_free (rt);
+ g_free (rt_secret);
+ g_free (at_secret);
+
g_free (login_link);
g_free (markup);
@@ -1570,6 +1585,7 @@ set_flickr_config (void)
GrlConfig *config;
GrlRegistry *registry;
gchar *token;
+ gchar *secret;
registry = grl_registry_get_default ();
@@ -1578,13 +1594,13 @@ set_flickr_config (void)
grl_config_set_api_secret (config, FLICKR_SECRET);
grl_registry_add_config (registry, config, NULL);
- token = load_flickr_token ();
+ token = load_flickr_token (&secret);
if (!token) {
token = authorize_flickr ();
if (!token) {
/* Save empty token to avoid asking again */
- save_flickr_token ("");
+ save_flickr_token ("", "");
}
}
@@ -1593,9 +1609,11 @@ set_flickr_config (void)
grl_config_set_api_key (config, FLICKR_KEY);
grl_config_set_api_secret (config, FLICKR_SECRET);
grl_config_set_api_token (config, token);
+ grl_config_set_api_token_secret (config, secret);
grl_registry_add_config (registry, config, NULL);
}
g_free (token);
+ g_free (secret);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]