[gnome-online-accounts] oauth2provider: Hide dialog buttons during the authorization step



commit a4c3b39b2c4b439b2d405d1fd7e6a97b7d1ec62b
Author: Debarshi Ray <debarshir gnome org>
Date:   Sat Mar 17 19:15:31 2012 +0100

    oauth2provider: Hide dialog buttons during the authorization step
    
    Once the user has successfully logged into the service using the
    embedded browser, there is no need for a GTK_STOCK_CANCEL button in
    the dialog. The service itself provides buttons to allow or deny the
    application.
    
    Added a get_authentication_cookie pure virtual method to
    GoaOAuth2Provider to let the providers specify the name of a cookie
    whose presence indicates that the user has been able to log in. The
    button is hidden when such a cookie is found. Currently, this has been
    implemented only in GoaFacebookProvider and GoaWindowsLiveProvider.

 src/goabackend/goafacebookprovider.c    |    7 +++
 src/goabackend/goaoauth2provider.c      |   69 ++++++++++++++++++++++++++++++-
 src/goabackend/goaoauth2provider.h      |    3 +
 src/goabackend/goawindowsliveprovider.c |    7 +++
 4 files changed, 85 insertions(+), 1 deletions(-)
---
diff --git a/src/goabackend/goafacebookprovider.c b/src/goabackend/goafacebookprovider.c
index 18e9415..7560024 100644
--- a/src/goabackend/goafacebookprovider.c
+++ b/src/goabackend/goafacebookprovider.c
@@ -142,6 +142,12 @@ get_client_secret (GoaOAuth2Provider *provider)
   return NULL;
 }
 
+static const gchar *
+get_authentication_cookie (GoaOAuth2Provider *provider)
+{
+  return "c_user";
+}
+
 /* ---------------------------------------------------------------------------------------------------- */
 
 static gchar *
@@ -350,6 +356,7 @@ goa_facebook_provider_class_init (GoaFacebookProviderClass *klass)
   oauth2_class->get_scope                = get_scope;
   oauth2_class->get_client_id            = get_client_id;
   oauth2_class->get_client_secret        = get_client_secret;
+  oauth2_class->get_authentication_cookie = get_authentication_cookie;
   oauth2_class->get_identity_sync        = get_identity_sync;
   oauth2_class->get_use_external_browser = get_use_external_browser;
   oauth2_class->add_account_key_values   = add_account_key_values;
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index 800fa7a..07d1e11 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 
 #include <rest/oauth2-proxy.h>
+#include <libsoup/soup.h>
 #include <libsoup/soup-gnome.h>
 #include <webkit/webkit.h>
 #include <json-glib/json-glib.h>
@@ -351,6 +352,26 @@ goa_oauth2_provider_get_client_secret (GoaOAuth2Provider *provider)
 }
 
 /**
+ * goa_oauth2_provider_get_authentication_cookie:
+ * @provider: A #GoaOAuth2Provider.
+ *
+ * Gets the name of a cookie whose presence indicates that the user has been able to
+ * log in during the authorization step. This is used to modify the embedded web
+ * browser UI that is presented to the user.
+ *
+ * This is a pure virtual method - a subclass must provide an
+ * implementation.
+ *
+ * Returns: (transfer none): A string owned by @provider - do not free.
+ */
+const gchar *
+goa_oauth2_provider_get_authentication_cookie (GoaOAuth2Provider *provider)
+{
+  g_return_val_if_fail (GOA_IS_OAUTH2_PROVIDER (provider), NULL);
+  return GOA_OAUTH2_PROVIDER_GET_CLASS (provider)->get_authentication_cookie (provider);
+}
+
+/**
  * goa_oauth2_provider_get_identity_sync:
  * @provider: A #GoaOAuth2Provider.
  * @access_token: A valid OAuth 2.0 access token.
@@ -548,6 +569,48 @@ typedef struct
   gchar *presentation_identity;
 } IdentifyData;
 
+static void
+check_cookie (SoupCookie *cookie, gpointer user_data)
+{
+  IdentifyData *data = user_data;
+  GList *children;
+  GList *l;
+  GoaOAuth2Provider *provider = data->provider;
+  GtkDialog *dialog = data->dialog;
+  GtkWidget *action_area;
+  const gchar *auth_cookie;
+  const gchar *name;
+
+  auth_cookie = goa_oauth2_provider_get_authentication_cookie (provider);
+  name = soup_cookie_get_name (cookie);
+  if (g_strcmp0 (auth_cookie, name) != 0)
+    return;
+
+  action_area = gtk_dialog_get_action_area (dialog);
+  children = gtk_container_get_children (GTK_CONTAINER (action_area));
+  for (l = children; l != NULL; l = l->next)
+    {
+      GtkWidget *child = l->data;
+      gtk_container_remove (GTK_CONTAINER (action_area), child);
+    }
+  g_list_free (children);
+}
+
+static void
+on_web_view_document_load_finished (WebKitWebView *web_view, WebKitWebFrame *frame, gpointer user_data)
+{
+  IdentifyData *data = user_data;
+  GSList *slist;
+  SoupCookieJar *cookie_jar;
+  SoupSession *session;
+
+  session = webkit_get_default_session ();
+  cookie_jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));
+  slist = soup_cookie_jar_all_cookies (cookie_jar);
+  g_slist_foreach (slist, (GFunc) check_cookie, data);
+  g_slist_free_full (slist, (GDestroyNotify) soup_cookie_free);
+}
+
 static gboolean
 on_web_view_navigation_policy_decision_requested (WebKitWebView             *webView,
                                                   WebKitWebFrame            *frame,
@@ -704,6 +767,7 @@ get_tokens_and_identity (GoaOAuth2Provider  *provider,
 
   memset (&data, '\0', sizeof (IdentifyData));
   data.provider = provider;
+  data.dialog = dialog;
   data.loop = g_main_loop_new (NULL, FALSE);
 
   /* TODO: use oauth2_proxy_build_login_url_full() */
@@ -777,6 +841,10 @@ get_tokens_and_identity (GoaOAuth2Provider  *provider,
       web_view = webkit_web_view_new ();
       webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), url);
       g_signal_connect (web_view,
+                        "document-load-finished",
+                        G_CALLBACK (on_web_view_document_load_finished),
+                        &data);
+      g_signal_connect (web_view,
                         "navigation-policy-decision-requested",
                         G_CALLBACK (on_web_view_navigation_policy_decision_requested),
                         &data);
@@ -792,7 +860,6 @@ get_tokens_and_identity (GoaOAuth2Provider  *provider,
       gtk_container_add (GTK_CONTAINER (vbox), scrolled_window);
       gtk_widget_show_all (scrolled_window);
     }
-  data.dialog = dialog;
   gtk_dialog_run (GTK_DIALOG (dialog));
 
   /* we can have either the auth code, with which we'll obtain the token, or
diff --git a/src/goabackend/goaoauth2provider.h b/src/goabackend/goaoauth2provider.h
index c8a4bbf..a8882b8 100644
--- a/src/goabackend/goaoauth2provider.h
+++ b/src/goabackend/goaoauth2provider.h
@@ -64,6 +64,7 @@ struct _GoaOAuth2Provider
  * @get_scope: Virtual function for goa_oauth2_provider_get_scope().
  * @get_client_id: Virtual function for goa_oauth2_provider_get_client_id().
  * @get_client_secret: Virtual function for goa_oauth2_provider_get_client_secret().
+ * @get_authentication_cookie: Virtual function for goa_oauth2_provider_get_authentication_cookie().
  * @get_identity_sync: Virtual function for goa_oauth2_provider_get_identity_sync().
  * @build_authorization_uri: Virtual function for goa_oauth2_provider_build_authorization_uri().
  * @get_use_external_browser: Virtual function for goa_oauth2_provider_get_use_external_browser().
@@ -81,6 +82,7 @@ struct _GoaOAuth2ProviderClass
   const gchar *(*get_scope)                (GoaOAuth2Provider  *provider);
   const gchar *(*get_client_id)            (GoaOAuth2Provider  *provider);
   const gchar *(*get_client_secret)        (GoaOAuth2Provider  *provider);
+  const gchar *(*get_authentication_cookie) (GoaOAuth2Provider  *provider);
   gchar       *(*get_identity_sync)        (GoaOAuth2Provider  *provider,
                                             const gchar        *access_token,
                                             gchar             **out_presentation_identity,
@@ -110,6 +112,7 @@ const gchar *goa_oauth2_provider_get_redirect_uri         (GoaOAuth2Provider  *p
 const gchar *goa_oauth2_provider_get_scope                (GoaOAuth2Provider  *provider);
 const gchar *goa_oauth2_provider_get_client_id            (GoaOAuth2Provider  *provider);
 const gchar *goa_oauth2_provider_get_client_secret        (GoaOAuth2Provider  *provider);
+const gchar *goa_oauth2_provider_get_authentication_cookie (GoaOAuth2Provider  *provider);
 gchar       *goa_oauth2_provider_get_identity_sync        (GoaOAuth2Provider  *provider,
                                                            const gchar        *access_token,
                                                            gchar             **out_presentation_identity,
diff --git a/src/goabackend/goawindowsliveprovider.c b/src/goabackend/goawindowsliveprovider.c
index e7d1dca..dbf287a 100644
--- a/src/goabackend/goawindowsliveprovider.c
+++ b/src/goabackend/goawindowsliveprovider.c
@@ -136,6 +136,12 @@ get_client_secret (GoaOAuth2Provider *provider)
   return NULL;
 }
 
+static const gchar *
+get_authentication_cookie (GoaOAuth2Provider *provider)
+{
+  return "MSNPPAuth";
+}
+
 /* ---------------------------------------------------------------------------------------------------- */
 
 static gchar *
@@ -367,6 +373,7 @@ goa_windows_live_provider_class_init (GoaWindowsLiveProviderClass *klass)
   oauth2_class->get_scope                = get_scope;
   oauth2_class->get_client_id            = get_client_id;
   oauth2_class->get_client_secret        = get_client_secret;
+  oauth2_class->get_authentication_cookie = get_authentication_cookie;
   oauth2_class->get_identity_sync        = get_identity_sync;
   oauth2_class->get_use_external_browser = get_use_external_browser;
   oauth2_class->add_account_key_values   = add_account_key_values;



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