[frogr] Implemented 1st phase of authorization process in terms of OAuth.



commit 8a00171522d7e587c5d0075d10da195db60c71e2
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Wed Apr 4 03:32:45 2012 +0200

    Implemented 1st phase of authorization process in terms of OAuth.
    
    It's now possible to make frogr open the new authorization url that
    leads the user to getting a verifier code to enter later on in
    frogr. Still, a lot of work is still needed, like allowing the user
    introduce that verifier code somewhere to continue with the process.
    
    But it's a good start already.

 src/flicksoup/fsp-error.c   |    1 -
 src/flicksoup/fsp-error.h   |    3 +-
 src/flicksoup/fsp-parser.c  |   95 +++++++++++++++++--------------------------
 src/flicksoup/fsp-parser.h  |    4 +-
 src/flicksoup/fsp-session.c |   63 +++++++++++++++++------------
 src/frogr-controller.c      |    5 ++
 6 files changed, 82 insertions(+), 89 deletions(-)
---
diff --git a/src/flicksoup/fsp-error.c b/src/flicksoup/fsp-error.c
index 9bc18f7..5a1357c 100644
--- a/src/flicksoup/fsp-error.c
+++ b/src/flicksoup/fsp-error.c
@@ -264,7 +264,6 @@ fsp_error_get_from_response_code        (FspErrorMethod method, gint code)
     {
       switch (method)
         {
-        case FSP_ERROR_METHOD_GET_FROB:
         case FSP_ERROR_METHOD_GET_AUTH_TOKEN:
         case FSP_ERROR_METHOD_GET_UPLOAD_STATUS:
         case FSP_ERROR_METHOD_GROUP_GET_LIST:
diff --git a/src/flicksoup/fsp-error.h b/src/flicksoup/fsp-error.h
index 69b30d0..5148542 100644
--- a/src/flicksoup/fsp-error.h
+++ b/src/flicksoup/fsp-error.h
@@ -79,7 +79,7 @@ typedef enum {
   FSP_ERROR_SERVICE_UNAVAILABLE,
 
   /* Errors from flicksoup only */
-  FSP_ERROR_NOFROB,
+  FSP_ERROR_REQUEST_TOKEN,
 
   /* Default fallback for other kind of errors */
   FSP_ERROR_OTHER
@@ -87,7 +87,6 @@ typedef enum {
 
 typedef enum {
   FSP_ERROR_METHOD_UNDEFINED,
-  FSP_ERROR_METHOD_GET_FROB,
   FSP_ERROR_METHOD_GET_AUTH_TOKEN,
   FSP_ERROR_METHOD_GET_UPLOAD_STATUS,
   FSP_ERROR_METHOD_PHOTO_UPLOAD,
diff --git a/src/flicksoup/fsp-parser.c b/src/flicksoup/fsp-parser.c
index dc06fbb..c882b20 100644
--- a/src/flicksoup/fsp-parser.c
+++ b/src/flicksoup/fsp-parser.c
@@ -62,9 +62,6 @@ _get_error_method_from_parser           (gpointer (*body_parser)
                                          (xmlDoc  *doc,
                                           GError **error));
 static gpointer
-_get_frob_parser                        (xmlDoc  *doc,
-                                         GError **error);
-static gpointer
 _get_auth_token_parser                  (xmlDoc  *doc,
                                          GError **error);
 static gpointer
@@ -261,9 +258,7 @@ _get_error_method_from_parser           (gpointer (*body_parser)
 {
   FspErrorMethod error_method = FSP_ERROR_METHOD_UNDEFINED;
 
-  if (body_parser ==  _get_frob_parser)
-    error_method = FSP_ERROR_METHOD_GET_FROB;
-  else if (body_parser == _get_auth_token_parser)
+  if (body_parser == _get_auth_token_parser)
     error_method = FSP_ERROR_METHOD_GET_AUTH_TOKEN;
   else if (body_parser == _get_upload_status_parser)
     error_method = FSP_ERROR_METHOD_GET_UPLOAD_STATUS;
@@ -339,49 +334,6 @@ _process_xml_response                          (FspParser  *self,
 }
 
 static gpointer
-_get_frob_parser                        (xmlDoc  *doc,
-                                         GError **error)
-{
-  xmlXPathContext *xpathCtx = NULL;
-  xmlXPathObject * xpathObj = NULL;
-  gchar *frob = NULL;
-  GError *err = NULL;
-
-  g_return_val_if_fail (doc != NULL, NULL);
-
-  xpathCtx = xmlXPathNewContext (doc);
-  xpathObj = xmlXPathEvalExpression ((xmlChar *)"/rsp/frob", xpathCtx);
-
-  if ((xpathObj != NULL) && (xpathObj->nodesetval->nodeNr > 0))
-    {
-      /* Matching nodes found */
-      xmlNode *node = NULL;
-      xmlChar *content = NULL;
-
-      /* Get the frob */
-      node = xpathObj->nodesetval->nodeTab[0];
-      content = xmlNodeGetContent (node);
-      if (content != NULL)
-        {
-          frob = g_strdup ((gchar *) content);
-          xmlFree (content);
-        }
-    }
-  else
-    err = g_error_new (FSP_ERROR, FSP_ERROR_MISSING_DATA,
-                       "No frob found in the response");
-  /* Free */
-  xmlXPathFreeObject (xpathObj);
-  xmlXPathFreeContext (xpathCtx);
-
-  /* Propagate error */
-  if (err != NULL)
-    g_propagate_error (error, err);
-
-  return frob;
-}
-
-static gpointer
 _get_auth_token_parser                  (xmlDoc  *doc,
                                          GError **error)
 {
@@ -1130,23 +1082,50 @@ fsp_parser_get_instance          (void)
   return FSP_PARSER (g_object_new (FSP_TYPE_PARSER, NULL));
 }
 
-gchar *
-fsp_parser_get_frob                     (FspParser  *self,
-                                         const gchar      *buffer,
-                                         gulong            buf_size,
-                                         GError          **error)
+FspDataAuthToken *
+fsp_parser_get_request_token            (FspParser   *self,
+                                         const gchar *buffer,
+                                         gulong       buf_size,
+                                         GError     **error)
 {
-  gchar *frob = NULL;
+  FspDataAuthToken *auth_token = NULL;
+  gchar *response_str = NULL;
+  gchar **response_array = NULL;
+  gint i = 0;
 
   g_return_val_if_fail (FSP_IS_PARSER (self), NULL);
   g_return_val_if_fail (buffer != NULL, NULL);
 
   /* Process the response */
-  frob = (gchar *) _process_xml_response (self, buffer, buf_size,
-                                          _get_frob_parser, error);
+  auth_token = FSP_DATA_AUTH_TOKEN (fsp_data_new (FSP_AUTH_TOKEN));
+  response_str = g_strndup (buffer, buf_size);
+  response_array = g_strsplit (response_str, "&", -1);
+  g_free (response_str);
+
+  for (i = 0; response_array[i]; i++)
+    {
+      if (g_str_has_prefix (response_array[i], "oauth_token="))
+        auth_token->token = g_strdup (&response_array[i][12]);
+
+      if (g_str_has_prefix (response_array[i], "oauth_token_secret="))
+        auth_token->token_secret = g_strdup (&response_array[i][19]);
+    }
+  g_strfreev (response_array);
+
+  /* Create the GError if needed*/
+  if (!auth_token->token || !auth_token->token_secret)
+    {
+      GError *err = NULL;
+      err = g_error_new (FSP_ERROR, FSP_ERROR_REQUEST_TOKEN,
+                         "An error happened requesting a new token");
+
+      /* Propagate error */
+      if (err != NULL)
+        g_propagate_error (error, err);
+    }
 
   /* Return value */
-  return frob;
+  return auth_token;
 }
 
 FspDataAuthToken *
diff --git a/src/flicksoup/fsp-parser.h b/src/flicksoup/fsp-parser.h
index 24dc963..5d5b0e3 100644
--- a/src/flicksoup/fsp-parser.h
+++ b/src/flicksoup/fsp-parser.h
@@ -67,8 +67,8 @@ fsp_parser_get_type              (void) G_GNUC_CONST;
 FspParser *
 fsp_parser_get_instance          (void);
 
-gchar *
-fsp_parser_get_frob                     (FspParser  *self,
+FspDataAuthToken *
+fsp_parser_get_request_token            (FspParser  *self,
                                          const gchar      *buffer,
                                          gulong            buf_size,
                                          GError          **error);
diff --git a/src/flicksoup/fsp-session.c b/src/flicksoup/fsp-session.c
index 0cae6ae..6d59d48 100644
--- a/src/flicksoup/fsp-session.c
+++ b/src/flicksoup/fsp-session.c
@@ -133,7 +133,7 @@ static SoupSession *
 _get_soup_session                       (FspSession *self);
 
 static void
-_get_frob_soup_session_cb               (SoupSession *session,
+_get_request_token_session_cb           (SoupSession *session,
                                          SoupMessage *msg,
                                          gpointer     data);
 static void
@@ -480,7 +480,7 @@ _get_soup_session                       (FspSession *self)
 }
 
 static void
-_get_frob_soup_session_cb               (SoupSession *session,
+_get_request_token_session_cb           (SoupSession *session,
                                          SoupMessage *msg,
                                          gpointer data)
 {
@@ -489,7 +489,7 @@ _get_frob_soup_session_cb               (SoupSession *session,
 
   /* Handle message with the right parser */
   _handle_soup_response (msg,
-                         (FspParserFunc) fsp_parser_get_frob,
+                         (FspParserFunc) fsp_parser_get_request_token,
                          data);
 }
 
@@ -1250,7 +1250,6 @@ _handle_soup_response                   (SoupMessage   *msg,
   if (!_check_errors_on_soup_response (msg, &err))
     result = parserFunc (parser, response_str, response_len, &err);
 
-
   /* Build response and call async callback */
   _build_async_result_and_complete (clos, result, err);
 
@@ -1662,18 +1661,26 @@ fsp_session_get_auth_url_async          (FspSession          *self,
 
   g_return_if_fail (FSP_IS_SESSION (self));
 
-  /* Build the signed url */
   priv = self->priv;
+
+  /* We need to make sure that any token is removed at this point to
+     avoid calculating the signature wrongly, and also because they
+     will get replaced anyway by the new token. */
+  g_free (priv->token);
+  priv->token = NULL;
+  g_free (priv->token_secret);
+  priv->token_secret = NULL;
+
+  /* Build the signed url */
   url = _get_signed_url (self,
-                         FLICKR_API_BASE_URL,
-                         AUTHORIZATION_METHOD_ORIGINAL,
-                         "method", "flickr.auth.getFrob",
-                         "api_key", priv->api_key,
+                         FLICKR_REQUEST_TOKEN_OAUTH_URL,
+                         AUTHORIZATION_METHOD_OAUTH_1,
+                         "oauth_callback", OAUTH_CALLBACK_URL,
                          NULL);
 
   /* Perform the async request */
   _perform_async_request (priv->soup_session, url,
-                          _get_frob_soup_session_cb, G_OBJECT (self),
+                          _get_request_token_session_cb, G_OBJECT (self),
                           c, cb, fsp_session_get_auth_url_async, data);
 
   g_free (url);
@@ -1684,31 +1691,35 @@ fsp_session_get_auth_url_finish         (FspSession    *self,
                                          GAsyncResult  *res,
                                          GError       **error)
 {
-  gchar *frob = NULL;
+  FspDataAuthToken *auth_token = NULL;
   gchar *auth_url = NULL;
 
-  g_return_val_if_fail (FSP_IS_SESSION (self), NULL);
-  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
+  g_return_val_if_fail (FSP_IS_SESSION (self), FALSE);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
 
-  frob = (gchar*) _finish_async_request (G_OBJECT (self), res,
-                                         fsp_session_get_auth_url_async, error);
+  auth_token =
+    FSP_DATA_AUTH_TOKEN (_finish_async_request (G_OBJECT (self), res,
+                                                fsp_session_get_auth_url_async,
+                                                error));
   /* Build the auth URL from the frob */
-  if (frob != NULL)
+  if (auth_token != NULL)
     {
       FspSessionPrivate *priv = self->priv;
 
-      /* Save the frob */
-      g_free (priv->frob);
-      priv->frob = frob;
+      /* Save the data */
+      g_free (priv->token);
+      priv->token = g_strdup (auth_token->token);
+
+      g_free (priv->token_secret);
+      priv->token_secret = g_strdup (auth_token->token_secret);
 
       /* Build the authorization url */
-      auth_url = _get_signed_url (self,
-                                  FLICKR_API_AUTH_URL,
-                                  AUTHORIZATION_METHOD_ORIGINAL,
-                                  "api_key", priv->api_key,
-                                  "perms", "write",
-                                  "frob", priv->frob,
-                                  NULL);
+      auth_url = g_strdup_printf ("http://www.flickr.com/services/oauth/authorize";
+                                  "?perms=write"
+                                  "&oauth_token=%s",
+                                  priv->token);
+
+      fsp_data_free (FSP_DATA (auth_token));
     }
 
   return auth_url;
diff --git a/src/frogr-controller.c b/src/frogr-controller.c
index 3a5d214..6b22627 100644
--- a/src/frogr-controller.c
+++ b/src/frogr-controller.c
@@ -357,6 +357,11 @@ _notify_error_to_user (FrogrController *self, GError *error)
       error_function = frogr_util_show_error_dialog;
       break;
 
+    case FSP_ERROR_REQUEST_TOKEN:
+      msg = g_strdup_printf (_("Unable to authenticate in flickr"));
+      error_function = frogr_util_show_error_dialog;
+      break;
+
     default:
       /* General error: just dump the raw error description */
       msg = g_strdup_printf (_("An error happened: %s."),



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