From 785464284bbc371d0b9bb3cc08f7a037637fdee1 Mon Sep 17 00:00:00 2001 From: Simon Schampijer Date: Fri, 20 Feb 2009 10:36:40 +0000 Subject: Create a HTTP Cookie to authenticate with the Schoolserver (Martin Langhoff) --- 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) + -- cgit v0.9.1