[gnome-online-accounts] oauth2: Clean up



commit 870f8408d78b74ebfe8eef2e6cb333c1ce67a04c
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Oct 18 12:13:52 2013 +0200

    oauth2: Clean up
    
    Simplify the maze of nested if-else branches and multiple return sites.
    
    Fixes: https://bugzilla.gnome.org/710363

 src/goabackend/goaoauth2provider.c |  173 ++++++++++++++++++------------------
 1 files changed, 88 insertions(+), 85 deletions(-)
---
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index ef6df3b..6017615 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -876,120 +876,123 @@ on_web_view_navigation_policy_decision_requested (WebKitWebView             *web
 {
   GoaOAuth2Provider *provider = GOA_OAUTH2_PROVIDER (user_data);
   GoaOAuth2ProviderPrivate *priv = provider->priv;
+  SoupMessage *message;
+  SoupURI *uri;
+  const gchar *fragment;
   const gchar *oauth2_error;
+  const gchar *query;
   const gchar *redirect_uri;
   const gchar *requested_uri;
+  gint response_id;
 
   /* TODO: use oauth2_proxy_extract_access_token() */
 
   requested_uri = webkit_network_request_get_uri (request);
   redirect_uri = goa_oauth2_provider_get_redirect_uri (provider);
-  if (g_str_has_prefix (requested_uri, redirect_uri))
+  if (!g_str_has_prefix (requested_uri, redirect_uri))
+    goto default_behaviour;
+
+  message = webkit_network_request_get_message (request);
+  uri = soup_message_get_uri (message);
+  fragment = soup_uri_get_fragment (uri);
+  query = soup_uri_get_query (uri);
+
+  /* Two cases:
+   * 1) we can have either the auth code in the query part of the
+   *    URI, with which we'll obtain the token, or
+   * 2) the access_token and other information directly in the
+   *    fragment part of the URI.
+   */
+  if (query != NULL)
     {
-      SoupMessage *message;
-      SoupURI *uri;
-      const gchar *fragment;
-      const gchar *query;
-
-      message = webkit_network_request_get_message (request);
-      uri = soup_message_get_uri (message);
-      fragment = soup_uri_get_fragment (uri);
-      query = soup_uri_get_query (uri);
-
-      /* Two cases:
-       * 1) we can have either the auth code in the query part of the
-       *    URI, with which we'll obtain the token, or
-       * 2) the access_token and other information directly in the
-       *    fragment part of the URI.
-       */
-      if (query != NULL)
-        {
-          GHashTable *key_value_pairs;
+      GHashTable *key_value_pairs;
 
-          key_value_pairs = soup_form_decode (query);
+      key_value_pairs = soup_form_decode (query);
 
-          priv->authorization_code = g_strdup (g_hash_table_lookup (key_value_pairs, "code"));
-          if (priv->authorization_code != NULL)
-            {
-              gtk_dialog_response (priv->dialog, GTK_RESPONSE_OK);
-            }
+      priv->authorization_code = g_strdup (g_hash_table_lookup (key_value_pairs, "code"));
+      if (priv->authorization_code != NULL)
+        {
+          response_id = GTK_RESPONSE_OK;
+        }
+      else
+        {
+          oauth2_error = (const gchar *) g_hash_table_lookup (key_value_pairs, "error");
+          if (g_strcmp0 (oauth2_error, GOA_OAUTH2_ACCESS_DENIED) == 0)
+            response_id = GTK_RESPONSE_CANCEL;
           else
             {
-              oauth2_error = (const gchar *) g_hash_table_lookup (key_value_pairs, "error");
-              if (g_strcmp0 (oauth2_error, GOA_OAUTH2_ACCESS_DENIED) == 0)
-                gtk_dialog_response (priv->dialog, GTK_RESPONSE_CANCEL);
-              else
-                {
-                  g_set_error (&priv->error,
-                               GOA_ERROR,
-                               GOA_ERROR_NOT_AUTHORIZED,
-                               _("Authorization response was ‘%s’"),
-                               oauth2_error);
-                  gtk_dialog_response (priv->dialog, GTK_RESPONSE_CLOSE);
-                }
+              g_set_error (&priv->error,
+                           GOA_ERROR,
+                           GOA_ERROR_NOT_AUTHORIZED,
+                           _("Authorization response was ‘%s’"),
+                           oauth2_error);
+              response_id = GTK_RESPONSE_CLOSE;
             }
-          g_hash_table_unref (key_value_pairs);
-          webkit_web_policy_decision_ignore (policy_decision);
         }
-      else if (fragment != NULL)
-        {
-          GHashTable *key_value_pairs;
+      g_hash_table_unref (key_value_pairs);
+      goto ignore_request;
+    }
+  else if (fragment != NULL)
+    {
+      GHashTable *key_value_pairs;
 
-          /* fragment is encoded into a key/value pairs for the token and
-           * expiration values, using the same syntax as a URL query */
-          key_value_pairs = soup_form_decode (fragment);
+      /* fragment is encoded into a key/value pairs for the token and
+       * expiration values, using the same syntax as a URL query */
+      key_value_pairs = soup_form_decode (fragment);
 
-          /* We might use oauth2_proxy_extract_access_token() here but
-           * we can also extract other information.
-           */
-          priv->access_token = g_strdup (g_hash_table_lookup (key_value_pairs, "access_token"));
-          if (priv->access_token != NULL)
-            {
-              gchar *expires_in_str = NULL;
+      /* We might use oauth2_proxy_extract_access_token() here but
+       * we can also extract other information.
+       */
+      priv->access_token = g_strdup (g_hash_table_lookup (key_value_pairs, "access_token"));
+      if (priv->access_token != NULL)
+        {
+          gchar *expires_in_str = NULL;
 
-              expires_in_str = g_hash_table_lookup (key_value_pairs, "expires_in");
-              /* sometimes "expires_in" appears as "expires" */
-              if (expires_in_str == NULL)
-                expires_in_str = g_hash_table_lookup (key_value_pairs, "expires");
+          expires_in_str = g_hash_table_lookup (key_value_pairs, "expires_in");
+          /* sometimes "expires_in" appears as "expires" */
+          if (expires_in_str == NULL)
+            expires_in_str = g_hash_table_lookup (key_value_pairs, "expires");
 
-              if (expires_in_str != NULL)
-                 priv->access_token_expires_in = atoi (expires_in_str);
+          if (expires_in_str != NULL)
+            priv->access_token_expires_in = atoi (expires_in_str);
 
-              priv->refresh_token = g_strdup (g_hash_table_lookup (key_value_pairs, "refresh_token"));
+          priv->refresh_token = g_strdup (g_hash_table_lookup (key_value_pairs, "refresh_token"));
 
-              gtk_dialog_response (priv->dialog, GTK_RESPONSE_OK);
-            }
-          else
-            {
-              oauth2_error = (const gchar *) g_hash_table_lookup (key_value_pairs, "error");
-              if (g_strcmp0 (oauth2_error, GOA_OAUTH2_ACCESS_DENIED) == 0)
-                gtk_dialog_response (priv->dialog, GTK_RESPONSE_CANCEL);
-              else
-                {
-                  g_set_error (&priv->error,
-                               GOA_ERROR,
-                               GOA_ERROR_NOT_AUTHORIZED,
-                               _("Authorization response was ‘%s’"),
-                               oauth2_error);
-                  gtk_dialog_response (priv->dialog, GTK_RESPONSE_CLOSE);
-                }
-            }
-          g_hash_table_unref (key_value_pairs);
-          webkit_web_policy_decision_ignore (policy_decision);
+          response_id = GTK_RESPONSE_OK;
         }
       else
         {
-          /* this actually means that something unexpected happened, either we
-           * did something wrong or the provider's flow changed */
-          goa_debug ("URI format not recognized, DEFAULT BEHAVIOUR");
-          return FALSE;
+          oauth2_error = (const gchar *) g_hash_table_lookup (key_value_pairs, "error");
+          if (g_strcmp0 (oauth2_error, GOA_OAUTH2_ACCESS_DENIED) == 0)
+            response_id = GTK_RESPONSE_CANCEL;
+          else
+            {
+              g_set_error (&priv->error,
+                           GOA_ERROR,
+                           GOA_ERROR_NOT_AUTHORIZED,
+                           _("Authorization response was ‘%s’"),
+                           oauth2_error);
+              response_id = GTK_RESPONSE_CLOSE;
+            }
         }
-      return TRUE; /* ignore the request */
+      g_hash_table_unref (key_value_pairs);
+      goto ignore_request;
     }
   else
     {
-      return FALSE; /* make default behavior apply */
+      /* this actually means that something unexpected happened, either we
+       * did something wrong or the provider's flow changed */
+      goa_debug ("URI format not recognized, DEFAULT BEHAVIOUR");
+      goto default_behaviour;
     }
+
+ ignore_request:
+  gtk_dialog_response (priv->dialog, response_id);
+  webkit_web_policy_decision_ignore (policy_decision);
+  return TRUE;
+
+ default_behaviour:
+  return FALSE;
 }
 
 static void


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