[Sugar-devel] [PATCH] Create a HTTP Cookie to authenticate with the Schoolserver (Martin Langhoff)

martin.langhoff at gmail.com martin.langhoff at gmail.com
Thu Feb 19 19:21:56 EST 2009


From: Martin Langhoff <martin at laptop.org>

Re-edit of the patch, based on Simon's merge to master.
---
 webactivity.py |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/webactivity.py b/webactivity.py
index e17d3b2..4efa470 100644
--- a/webactivity.py
+++ b/webactivity.py
@@ -1,4 +1,5 @@
 # Copyright (C) 2006, Red Hat, Inc.
+# Copyright (C) 2009 Martin Langhoff, Simon Schampijer
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -26,7 +27,9 @@ import sha
 import base64
 import time
 import shutil
- 
+import sqlite3
+import cjson
+
 from sugar.activity import activity
 from sugar.graphics import style
 import telepathy
@@ -60,6 +63,64 @@ if _profile_version < PROFILE_VERSION:
     f.write(str(PROFILE_VERSION))
     f.close()
 
+def _seed_xs_cookie():
+    ''' Create a HTTP Cookie to authenticate with the Schoolserver
+    '''
+
+    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
+
+    jabber_server = prof.jabber_server
+
+    pubkey = profile.get_profile().pubkey
+    cookie_data = {'color': profile.get_color().to_string(),
+                   'pkey_hash': sha.new(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
+        c.execute('''INSERT INTO moz_cookies (name, value, host, 
+                                              path, expiry, lastAccessed,
+                                              isSecure, isHttpOnly)
+                     VALUES(?,?,?,?,?,?,?,?)''',
+                  ('xoid', cjson.encode(cookie_data), jabber_server,
+                   '/', expire, 0, 0, 0 ))
+        cookies_db.commit()
+        cookies_db.close()
+    except sqlite3.Error, e:
+        _logger.error('seed_xs_cookie: %s' % e)
+    else:
+        _logger.debug('seed_xs_cookie: Updated cookie successfully')
+
+
 import hulahop
 hulahop.set_app_version(os.environ['SUGAR_BUNDLE_VERSION'])
 hulahop.startup(_profile_path)
@@ -104,6 +165,8 @@ class WebActivity(activity.Activity):
         progresslistener.init(self._browser)
         filepicker.init(self)
 
+        _seed_xs_cookie()
+
         toolbox = activity.ActivityToolbox(self)
 
         self._edit_toolbar = EditToolbar(self._browser)
@@ -482,3 +545,4 @@ class WebActivity(activity.Activity):
             downloadmanager.remove_all_downloads()
             self.close(force=True)
 
+
-- 
1.5.6.6



More information about the Sugar-devel mailing list