[grilo-plugins] flickr: Add helper function to check a token



commit 856673d53d2d3669c8c37a30439a78a0e5d0f8f7
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Tue Jul 6 15:53:28 2010 +0200

    flickr: Add helper function to check a token

 src/flickr/gflickr.c         |   89 ++++++++++++++++++++++++++++++++++++++++++
 src/flickr/gflickr.h         |    8 ++++
 src/flickr/grl-flickr-auth.c |   40 +++++++++++++++++++
 src/flickr/grl-flickr-auth.h |    4 ++
 4 files changed, 141 insertions(+), 0 deletions(-)
---
diff --git a/src/flickr/gflickr.c b/src/flickr/gflickr.c
index c6f7569..bfce8f0 100644
--- a/src/flickr/gflickr.c
+++ b/src/flickr/gflickr.c
@@ -24,6 +24,7 @@
 #define FLICKR_TAGS_GETHOTLIST_METHOD "flickr.tags.getHotList"
 #define FLICKR_AUTH_GETFROB_METHOD "flickr.auth.getFrob"
 #define FLICKR_AUTH_GETTOKEN_METHOD "flickr.auth.getToken"
+#define FLICKR_AUTH_CHECKTOKEN_METHOD "flickr.auth.checkToken"
 
 #define FLICKR_PHOTOS_SEARCH                            \
   FLICKR_ENDPOINT                                       \
@@ -66,6 +67,13 @@
   "&method=" FLICKR_AUTH_GETTOKEN_METHOD        \
   "&frob=%s"
 
+#define FLICKR_AUTH_CHECKTOKEN                  \
+  FLICKR_ENDPOINT                               \
+  "api_key=%s"                                  \
+  "&api_sig=%s"                                 \
+  "&method=" FLICKR_AUTH_CHECKTOKEN_METHOD      \
+  "&auth_token=%s"
+
 #define FLICKR_AUTH_LOGINLINK                   \
   FLICKR_AUTHPOINT                              \
   "api_key=%s"                                  \
@@ -293,6 +301,26 @@ get_tag (xmlNodePtr node)
   }
 }
 
+static GHashTable *
+get_token_info (xmlNodePtr node)
+{
+  GHashTable *token = g_hash_table_new_full (g_str_hash,
+                                             g_str_equal,
+                                             g_free,
+                                             g_free);
+  node = node->xmlChildrenNode;
+
+  while (node) {
+    g_hash_table_insert (token,
+                         g_strdup ((const gchar *) node->name),
+                         (gchar *) xmlNodeGetContent (node));
+    add_node (node, token);
+    node = node->next;
+  }
+
+  return token;
+}
+
 static void
 process_photo_result (const gchar *xml_result, gpointer user_data)
 {
@@ -386,6 +414,32 @@ process_taglist_result (const gchar *xml_result, gpointer user_data)
 }
 
 static void
+process_token_result (const gchar *xml_result, gpointer user_data)
+{
+  xmlDocPtr doc;
+  xmlNodePtr node;
+  GFlickrData *data = (GFlickrData *) user_data;
+  GHashTable *token;
+
+  doc = xmlReadMemory (xml_result, xmlStrlen ((xmlChar*) xml_result), NULL,
+                       NULL, XML_PARSE_RECOVER | XML_PARSE_NOBLANKS);
+  node = xmlDocGetRootElement (doc);
+
+  /* Check if result is OK */
+  if (!node || !result_is_correct (node)) {
+    data->hashtable_cb (NULL, NULL, data->user_data);
+  } else {
+    node = node->xmlChildrenNode;
+    token = get_token_info (node);
+    data->hashtable_cb (NULL, token, data->user_data);
+    g_hash_table_unref (token);
+  }
+
+  g_slice_free (GFlickrData, data);
+  xmlFreeDoc (doc);
+}
+
+static void
 read_done_cb (GObject *source_object,
               GAsyncResult *res,
               gpointer user_data)
@@ -756,3 +810,38 @@ g_flickr_auth_getToken (GFlickr *f,
 
   return token;
 }
+
+void
+g_flickr_auth_checkToken (GFlickr *f,
+                          const gchar *token,
+                          GFlickrHashTableCb callback,
+                          gpointer user_data)
+{
+  gchar *api_sig;
+  gchar *request;
+
+  g_return_if_fail (G_IS_FLICKR (f));
+  g_return_if_fail (token);
+  g_return_if_fail (callback);
+
+  api_sig = get_api_sig (f->priv->auth_secret,
+                         "method", FLICKR_AUTH_CHECKTOKEN_METHOD,
+                         "api_key", f->priv->api_key,
+                         "auth_token", token,
+                         NULL);
+
+  /* Build request */
+  request  = g_strdup_printf (FLICKR_AUTH_CHECKTOKEN,
+                              f->priv->api_key,
+                              api_sig,
+                              token);
+  g_free (api_sig);
+
+  GFlickrData *gfd = g_slice_new (GFlickrData);
+  gfd->parse_xml = process_token_result;
+  gfd->hashtable_cb = callback;
+  gfd->user_data = user_data;
+
+  read_url_async (request, gfd);
+  g_free (request);
+}
diff --git a/src/flickr/gflickr.h b/src/flickr/gflickr.h
index a4b9a4f..b161f4d 100644
--- a/src/flickr/gflickr.h
+++ b/src/flickr/gflickr.h
@@ -77,6 +77,8 @@ typedef void (*GFlickrHashTableCb) (GFlickr *f, GHashTable *result, gpointer use
 
 typedef void (*GFlickrListCb) (GFlickr *f, GList *result, gpointer user_data);
 
+typedef void (*GFlickrCheckToken) (GFlickr *f, GHashTable *tokeninfo, gpointer user_data);
+
 GType g_flickr_get_type (void);
 
 GFlickr *g_flickr_new (const gchar *api_key, const gchar *auth_secret, const gchar *auth_token);
@@ -121,4 +123,10 @@ gchar *
 g_flickr_auth_getToken (GFlickr *f,
                         const gchar *frob);
 
+void
+g_flickr_auth_checkToken (GFlickr *f,
+                          const gchar *token,
+                          GFlickrHashTableCb callback,
+                          gpointer user_data);
+
 #endif /* _G_FLICKR_H_ */
diff --git a/src/flickr/grl-flickr-auth.c b/src/flickr/grl-flickr-auth.c
index a197719..5927494 100644
--- a/src/flickr/grl-flickr-auth.c
+++ b/src/flickr/grl-flickr-auth.c
@@ -1,6 +1,16 @@
 #include "grl-flickr-auth.h"
 #include "gflickr.h"
 
+static void
+check_token_cb (GFlickr *f,
+                GHashTable *result,
+                gpointer user_data)
+{
+  gint *token_is_valid = (gint *) user_data;
+
+  *token_is_valid = (result != NULL);
+}
+
 gchar *
 grl_flickr_get_frob (const gchar *api_key,
                      const gchar *secret)
@@ -57,3 +67,33 @@ grl_flickr_get_token (const gchar *api_key,
 
   return token;
 }
+
+gboolean
+grl_flickr_check_token (const gchar *api_key,
+                        const gchar *secret,
+                        const gchar *token)
+{
+  GFlickr *f;
+  GMainContext *mainloop_ctx;
+  GMainLoop *mainloop;
+  gint token_is_valid = -1;
+
+  f = g_flickr_new (api_key, secret, NULL);
+  if (!f) {
+    return FALSE;
+  }
+
+  g_flickr_auth_checkToken (f, token, check_token_cb, &token_is_valid);
+
+  mainloop = g_main_loop_new (NULL, TRUE);
+  mainloop_ctx = g_main_loop_get_context (mainloop);
+
+  while (token_is_valid == -1) {
+    g_main_context_iteration (mainloop_ctx, TRUE);
+  }
+
+  g_main_loop_unref (mainloop);
+  g_object_unref (f);
+
+  return token_is_valid;
+}
diff --git a/src/flickr/grl-flickr-auth.h b/src/flickr/grl-flickr-auth.h
index 3649109..d61f9b7 100644
--- a/src/flickr/grl-flickr-auth.h
+++ b/src/flickr/grl-flickr-auth.h
@@ -39,4 +39,8 @@ gchar *grl_flickr_get_token (const gchar *api_key,
                              const gchar *secret,
                              const gchar *frob);
 
+gboolean grl_flickr_check_token (const gchar *api_key,
+                                 const gchar *secret,
+                                 const gchar *token);
+
 #endif /* _GRL_FLICKR_AUTH_SOURCE_H_ */



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