[gnome-online-accounts/wip/gnome-3-8-backport: 2/4] oauth2: Clean up
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-online-accounts/wip/gnome-3-8-backport: 2/4] oauth2: Clean up
- Date: Wed, 30 Oct 2013 13:26:52 +0000 (UTC)
commit 54f83138a36c4b9b8fe3c21a935c8a103cf26de3
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 71c5723..c05da8e 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -811,120 +811,123 @@ on_web_view_navigation_policy_decision_requested (WebKitWebView *web
gpointer user_data)
{
IdentifyData *data = user_data;
+ 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 (data->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);
- data->authorization_code = g_strdup (g_hash_table_lookup (key_value_pairs, "code"));
- if (data->authorization_code != NULL)
- {
- gtk_dialog_response (data->dialog, GTK_RESPONSE_OK);
- }
+ data->authorization_code = g_strdup (g_hash_table_lookup (key_value_pairs, "code"));
+ if (data->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 (data->dialog, GTK_RESPONSE_CANCEL);
- else
- {
- g_set_error (&data->error,
- GOA_ERROR,
- GOA_ERROR_NOT_AUTHORIZED,
- _("Authorization response was \"%s\""),
- oauth2_error);
- gtk_dialog_response (data->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.
- */
- data->access_token = g_strdup (g_hash_table_lookup (key_value_pairs, "access_token"));
- if (data->access_token != NULL)
- {
- gchar *expires_in_str = NULL;
+ /* We might use oauth2_proxy_extract_access_token() here but
+ * we can also extract other information.
+ */
+ data->access_token = g_strdup (g_hash_table_lookup (key_value_pairs, "access_token"));
+ if (data->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)
- data->access_token_expires_in = atoi (expires_in_str);
+ if (expires_in_str != NULL)
+ data->access_token_expires_in = atoi (expires_in_str);
- data->refresh_token = g_strdup (g_hash_table_lookup (key_value_pairs, "refresh_token"));
+ data->refresh_token = g_strdup (g_hash_table_lookup (key_value_pairs, "refresh_token"));
- gtk_dialog_response (data->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 (data->dialog, GTK_RESPONSE_CANCEL);
- else
- {
- g_set_error (&data->error,
- GOA_ERROR,
- GOA_ERROR_NOT_AUTHORIZED,
- _("Authorization response was \"%s\""),
- oauth2_error);
- gtk_dialog_response (data->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 (&data->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 (data->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]