[PATCH 02/18] flickr: Add helper function to create flickr's API signature



---
 src/flickr/grl-flickr-auth.c |   58 ++++++++++++++++++++++++++++++++++-------
 1 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/src/flickr/grl-flickr-auth.c b/src/flickr/grl-flickr-auth.c
index fee3f83..300ee6c 100644
--- a/src/flickr/grl-flickr-auth.c
+++ b/src/flickr/grl-flickr-auth.c
@@ -3,14 +3,57 @@
 #include <gio/gio.h>
 #include <libxml/parser.h>
 #include <libxml/xpath.h>
+#include <string.h>
 
 #define FLICKR_ENTRYPOINT "http://api.flickr.com/services/rest/?";
+#define FLICKR_AUTH       "http://flickr.com/services/auth/?";
+
+static gchar *
+get_api_sig (const gchar *secret, ...)
+{
+  GHashTable *hash;
+  GList *key_iter;
+  GList *keys;
+  GString *to_sign;
+  gchar *api_sig;
+  gchar *key;
+  gchar *value;
+  gint text_size = strlen (secret);
+  va_list va_params;
+
+  hash = g_hash_table_new (g_str_hash, g_str_equal);
+
+  va_start (va_params, secret);
+  while ((key = va_arg (va_params, gchar *))) {
+    text_size += strlen (key);
+    value = va_arg (va_params, gchar *);
+    text_size += strlen (value);
+    g_hash_table_insert (hash, key, value);
+  }
+  va_end (va_params);
+
+  to_sign = g_string_sized_new (text_size);
+  g_string_append (to_sign, secret);
+
+  keys = g_hash_table_get_keys (hash);
+  keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
+  for (key_iter = keys; key_iter; key_iter = g_list_next (key_iter)) {
+    g_string_append (to_sign, key_iter->data);
+    g_string_append (to_sign, g_hash_table_lookup (hash, key_iter->data));
+  }
+
+  api_sig = g_compute_checksum_for_string (G_CHECKSUM_MD5, to_sign->str, -1);
+  g_hash_table_unref (hash);
+  g_list_free (keys);
+  g_string_free (to_sign, TRUE);
+
+  return api_sig;
+}
 
 gchar *
 grl_flickr_get_frob (const gchar *api_key,
                      const gchar *secret)
 {
-  gchar *to_sign;
   gchar *api_sig;
   gchar *url;
   GVfs *vfs;
@@ -22,15 +65,10 @@ grl_flickr_get_frob (const gchar *api_key,
   GError *error = NULL;
   gchar *frob = NULL;
 
-  /* Get api sig */
-  to_sign = g_strdup_printf ("%s"
-                             "api_key%s"
-                             "method" "flickr.auth.getFrob",
-                             secret,
-                             api_key);
-  api_sig = g_compute_checksum_for_string (G_CHECKSUM_MD5, to_sign, -1);
-  g_free (to_sign);
-
+  api_sig = get_api_sig (secret,
+                         "api_key", api_key,
+                         "method", "flickr.auth.getFrob",
+                         NULL);
   /* Build url */
   url = g_strdup_printf (FLICKR_ENTRYPOINT
                          "method=flickr.auth.getFrob&"
-- 
1.7.0.4



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