[gnome-online-accounts] oauth2: Push cookies stored in :preseed-data to the web view



commit 01447f6de49b640b7e298210aeb84390c804e9b3
Author: Emanuele Aina <emanuele aina collabora com>
Date:   Sun Mar 3 17:14:55 2013 +0100

    oauth2: Push cookies stored in :preseed-data to the web view
    
    Push cookies collected from existing browser sessions to the web view to
    skip the username/password prompt if already logged in.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=694313

 src/goabackend/goaoauth2provider.c |   67 ++++++++++++++++++++++++++++++++++++
 src/goabackend/goawebview.c        |   18 ++++++++++
 src/goabackend/goawebview.h        |    2 +
 3 files changed, 87 insertions(+), 0 deletions(-)
---
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index 4bc60fa..d18d97b 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -510,6 +510,66 @@ goa_oauth2_provider_is_identity_node (GoaOAuth2Provider *provider, WebKitDOMHTML
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+static SoupCookie*
+create_cookie_from_variant (GVariant *variant)
+{
+  SoupCookie *cookie;
+  gchar *name, *value, *domain, *path;
+  gboolean secure, http_only;
+
+  g_return_val_if_fail (g_variant_is_of_type (variant, G_VARIANT_TYPE_VARDICT), NULL);
+
+  g_variant_lookup (variant, "name", "&s", &name);
+  g_variant_lookup (variant, "value", "&s", &value);
+  g_variant_lookup (variant, "domain", "&s", &domain);
+  g_variant_lookup (variant, "path", "&s", &path);
+  g_variant_lookup (variant, "secure", "b", &secure);
+  g_variant_lookup (variant, "http_only", "b", &http_only);
+
+  cookie = soup_cookie_new (name, value, domain, path, -1);
+  soup_cookie_set_secure (cookie, secure);
+  soup_cookie_set_http_only (cookie, http_only);
+
+  return cookie;
+}
+
+static GSList*
+extract_cookies_from_preseed_data (GVariant *preseed)
+{
+  GVariant *cookies_v, *cookie_v;
+  GSList *cookies = NULL;
+  GVariantIter iter;
+
+  if (preseed == NULL)
+    return NULL;
+
+  cookies_v = g_variant_lookup_value (preseed, "cookies", NULL);
+  if (cookies_v == NULL)
+    return NULL;
+
+  if (!g_variant_is_of_type (cookies_v, G_VARIANT_TYPE ("av")))
+    {
+      g_warning ("Wrong type for the 'cookies' GVariant, expected 'av' but got '%s'",
+                 (gchar *)g_variant_get_type (cookies_v));
+      goto out;
+    }
+
+  g_variant_iter_init (&iter, cookies_v);
+  while ((cookie_v = g_variant_iter_next_value (&iter)) != NULL)
+    {
+      SoupCookie *cookie = create_cookie_from_variant (g_variant_get_variant (cookie_v));
+      if (cookie != NULL)
+          cookies = g_slist_prepend (cookies, cookie);
+      g_variant_unref(cookie_v);
+    }
+out:
+  g_variant_unref(cookies_v);
+  return cookies;
+}
+
+
+/* ---------------------------------------------------------------------------------------------------- */
+
 static gchar *
 get_tokens_sync (GoaOAuth2Provider  *provider,
                  const gchar        *authorization_code,
@@ -1009,12 +1069,19 @@ get_tokens_and_identity (GoaOAuth2Provider  *provider,
     {
       GtkWidget *web_view;
       GtkWidget *embed;
+      GVariant *preseed_data;
+      GSList *cookies;
 
       web_view = goa_web_view_new ();
       gtk_widget_set_hexpand (web_view, TRUE);
       gtk_widget_set_vexpand (web_view, TRUE);
       embed = goa_web_view_get_view (GOA_WEB_VIEW (web_view));
 
+      preseed_data = goa_provider_get_preseed_data (GOA_PROVIDER (provider));
+      cookies = extract_cookies_from_preseed_data (preseed_data);
+      goa_web_view_add_cookies (GOA_WEB_VIEW (web_view), cookies);
+      soup_cookies_free (cookies);
+
       if (goa_oauth2_provider_get_use_mobile_browser (provider))
         goa_web_view_fake_mobile (GOA_WEB_VIEW (web_view));
 
diff --git a/src/goabackend/goawebview.c b/src/goabackend/goawebview.c
index e8826a5..ba999e5 100644
--- a/src/goabackend/goawebview.c
+++ b/src/goabackend/goawebview.c
@@ -337,3 +337,21 @@ goa_web_view_fake_mobile (GoaWebView *self)
                               "AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile",
                 NULL);
 }
+
+void
+goa_web_view_add_cookies (GoaWebView *self,
+                          GSList     *cookies)
+{
+  SoupCookieJar *cookie_jar;
+  SoupSession *session;
+  GSList *l;
+
+  session = webkit_get_default_session ();
+  cookie_jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));
+
+  for (l = cookies; l != NULL; l = l->next)
+    {
+      SoupCookie *cookie = l->data;
+      soup_cookie_jar_add_cookie (cookie_jar, soup_cookie_copy (cookie));
+    }
+}
diff --git a/src/goabackend/goawebview.h b/src/goabackend/goawebview.h
index fe4e21c..9eb0cf0 100644
--- a/src/goabackend/goawebview.h
+++ b/src/goabackend/goawebview.h
@@ -53,6 +53,8 @@ GType                  goa_web_view_get_type               (void) G_GNUC_CONST;
 GtkWidget             *goa_web_view_new                    (void);
 GtkWidget             *goa_web_view_get_view               (GoaWebView *self);
 void                   goa_web_view_fake_mobile            (GoaWebView *self);
+void                   goa_web_view_add_cookies            (GoaWebView *self,
+                                                            GSList     *cookies);
 
 G_END_DECLS
 


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