[Sugar-devel] [PATCH] webactivity: seed the XS cookie at startup

martin.langhoff at gmail.com martin.langhoff at gmail.com
Thu Feb 12 00:47:20 EST 2009


From: Martin Langhoff <martin at laptop.org>

When starting up, call seed_xs_cookie() to
try to add a cookie for the XS with

 - the hashed pubkey of the XO - the sha1 hash
   is much smaller than the whole pubkey

 - the XO colors
---
 webactivity.py |   75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 75 insertions(+), 0 deletions(-)

diff --git a/webactivity.py b/webactivity.py
index e17d3b2..f3bc87a 100644
--- a/webactivity.py
+++ b/webactivity.py
@@ -26,6 +26,8 @@ import sha
 import base64
 import time
 import shutil
+import sqlite3
+import cjson
  
 from sugar.activity import activity
 from sugar.graphics import style
@@ -98,6 +100,10 @@ class WebActivity(activity.Activity):
 
         self._browser = Browser()
 
+        # call after browser init
+        # to ensure the storage dir exists
+        self.seed_xs_cookie()
+
         temp_path = os.path.join(self.get_activity_root(), 'instance')
         downloadmanager.init(self._browser, self, temp_path)
         sessionhistory.init(self._browser)
@@ -482,3 +488,72 @@ class WebActivity(activity.Activity):
             downloadmanager.remove_all_downloads()
             self.close(force=True)
 
+    def seed_xs_cookie(self):
+
+        _logger.debug('seed_xs_cookie')
+        db_path = os.path.join(_profile_path, 'cookies.sqlite')
+
+        # have we registered with the XS?
+        prof = profile.get_profile()
+        # profile.jabber_registered is old and buggy
+        # - check for jabber_server instead
+        if not prof.jabber_server:
+            _logger.debug('seed_xs_cookie: not registered yet')
+            return
+
+        xs_fqdn = prof.jabber_server
+
+        # the XS wants a unique identifier - it has
+        # the SN and the pubkey. The pubkey is a bit
+        # less widely known than the SN. Hash it to
+        # make it shorter (590 chars to 40).
+        cookie_data = { 'color': prof.color.to_string(),
+                        'pkey_hash': sha.new(prof.pubkey).hexdigest() }
+
+	# gecko creates the DB at startup
+	# so we seed it after Browse() is invoked
+        _logger.debug('cookies file:' + db_path)
+
+        try:
+            jar_db = sqlite3.connect(db_path)
+            c = jar_db.cursor()
+            _logger.debug('opened file')
+
+            # gecko delays creating the table
+            # until it gets a cookie...
+            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', xs_fqdn, '/'))
+        
+            # the row exists, we're done
+            if c.fetchone():
+                _logger.debug('cookie exists')
+                return
+
+            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', cjson.encode(cookie_data), xs_fqdn,
+                        '/', expire, 0,
+                        0, 0 ))
+            jar_db.commit()
+            jar_db.close()
+            _logger.debug('updated cookies successfully')
+        except:
+            _logger.error('error seeding cookies file')
+
-- 
1.5.6.6



More information about the Sugar-devel mailing list