[Sugar-devel] [PATCH v3 Browse] Store web session cookies in SQlite database, for SL #3456

Manuel Quiñones manuq at laptop.org
Thu Apr 19 12:45:46 EDT 2012


There was code in Browse to create a SQlite database under data
directory in the activity profile.  This was for creating a cookie to
authenticate the laptop with a schoolserver.

Now we use the same database via the Soup library to store all cookies
so they are persistent between sessions.  A Soup cookie jar is being
attached to the WebKit session, cookies are stored in a Mozilla
compatible SQLite format.

The schoolserver function has been changed to use the Soup API.

The code is based in Epiphany, embed/ephy-embed-single.c .

------

v1->v2: let Soup take care of database creation, use Soup API in
schoolserver cookie seed function

v2->v3: use proper constructors for PyGI instead of new()

Signed-off-by: Manuel Quiñones <manuq at laptop.org>
---
 webactivity.py |   79 ++++++++++++++++++++++++-------------------------------
 1 files changed, 35 insertions(+), 44 deletions(-)

diff --git a/webactivity.py b/webactivity.py
index aadc29a..611e3b2 100644
--- a/webactivity.py
+++ b/webactivity.py
@@ -28,6 +28,8 @@ from gi.repository import Gtk
 from gi.repository import Gdk
 from gi.repository import GdkPixbuf
 from gi.repository import WebKit
+from gi.repository import Soup
+from gi.repository import SoupGNOME
 
 import base64
 import time
@@ -58,6 +60,7 @@ PROFILE_VERSION = 2
 _profile_version = 0
 _profile_path = os.path.join(activity.get_activity_root(), 'data/gecko')
 _version_file = os.path.join(_profile_path, 'version')
+_cookies_db_path = os.path.join(_profile_path, 'cookies.sqlite')
 
 if os.path.exists(_version_file):
     f = open(_version_file)
@@ -76,15 +79,25 @@ if _profile_version < PROFILE_VERSION:
     f.close()
 
 
-def _seed_xs_cookie():
-    ''' Create a HTTP Cookie to authenticate with the Schoolserver
-    '''
+def _seed_xs_cookie(cookie_jar):
+    """Create a HTTP Cookie to authenticate with the Schoolserver.
+
+    Do nothing if the laptop is not registered with Schoolserver, or
+    if the cookie already exists.
+
+    """
     client = GConf.Client.get_default()
     backup_url = client.get_string('/desktop/sugar/backup_url')
-    if not backup_url:
+    if backup_url == '':
         _logger.debug('seed_xs_cookie: Not registered with Schoolserver')
         return
 
+    soup_uri = Soup.URI.new(uri_string=backup_url)
+    xs_cookie = cookie_jar.get_cookies(soup_uri, for_http=False)
+    if xs_cookie is not None:
+        _logger.debug('seed_xs_cookie: Cookie exists already')
+        return
+
     jabber_server = client.get_string(
         '/desktop/sugar/collaboration/jabber_server')
 
@@ -92,45 +105,16 @@ def _seed_xs_cookie():
     cookie_data = {'color': profile.get_color().to_string(),
                    'pkey_hash': sha1(pubkey).hexdigest()}
 
-    db_path = os.path.join(_profile_path, 'cookies.sqlite')
-    try:
-        cookies_db = sqlite3.connect(db_path)
-        c = cookies_db.cursor()
-
-        c.execute('''CREATE TABLE IF NOT EXISTS
-                     moz_cookies
-                     (id INTEGER PRIMARY KEY,
-                      name TEXT,
-                      value TEXT,
-                      host TEXT,
-                      path TEXT,
-                      expiry INTEGER,
-                      lastAccessed INTEGER,
-                      isSecure INTEGER,
-                      isHttpOnly INTEGER)''')
-
-        c.execute('''SELECT id
-                     FROM moz_cookies
-                     WHERE name=? AND host=? AND path=?''',
-                  ('xoid', jabber_server, '/'))
-
-        if c.fetchone():
-            _logger.debug('seed_xs_cookie: Cookie exists already')
-            return
+    expire = int(time.time()) + 10 * 365 * 24 * 60 * 60
 
-        expire = int(time.time()) + 10 * 365 * 24 * 60 * 60
-        c.execute('''INSERT INTO moz_cookies (name, value, host,
-                                              path, expiry, lastAccessed,
-                                              isSecure, isHttpOnly)
-                     VALUES(?,?,?,?,?,?,?,?)''',
-                  ('xoid', json.loads(cookie_data), jabber_server,
-                   '/', expire, 0, 0, 0))
-        cookies_db.commit()
-        cookies_db.close()
-    except sqlite3.Error:
-        _logger.exception('seed_xs_cookie: could not write cookie')
-    else:
-        _logger.debug('seed_xs_cookie: Updated cookie successfully')
+    xs_cookie = Soup.Cookie()
+    xs_cookie.set_name('xoid')
+    xs_cookie.set_value(json.loads(cookie_data))
+    xs_cookie.set_domain(jabber_server)
+    xs_cookie.set_path('/')
+    xs_cookie.set_max_age(expire)
+    cookie_jar.add_cookie(xs_cookie)
+    _logger.debug('seed_xs_cookie: Updated cookie successfully')
 
 
 def _set_char_preference(name, value):
@@ -170,6 +154,15 @@ class WebActivity(activity.Activity):
         session = WebKit.get_default_session()
         session.set_property('accept-language-auto', True)
 
+        # By default, cookies are not stored persistently, we have to
+        # add a cookie jar so that they get saved to disk.  We use one
+        # with a SQlite database:
+        cookie_jar = SoupGNOME.CookieJarSqlite(filename=_cookies_db_path,
+                                               read_only=False)
+        session.add_feature(cookie_jar)
+
+        _seed_xs_cookie(cookie_jar)
+
         # FIXME
         # downloadmanager.remove_old_parts()
 
@@ -177,8 +170,6 @@ class WebActivity(activity.Activity):
         self._tabbed_view = TabbedView()
         self._tabbed_view.connect('focus-url-entry', self._on_focus_url_entry)
 
-        _seed_xs_cookie()
-
         # HACK
         # Currently, the multiple tabs feature crashes the Browse activity
         # on cairo versions 1.8.10 or later. The exact cause for this
-- 
1.7.7.6



More information about the Sugar-devel mailing list