import logging from gettext import gettext as _ import re import sha import xmlrpclib import gconf import os import base64 import time import shutil import cjson from sugar import profile class XSCookieTransport(xmlrpclib.Transport): XSCOOKIE = 'xoid' def __init__(self, SESSION_ID_STRING='PHPSESSID'): xmlrpclib.Transport.__init__(self) self.mycookies=None self.mysessid=None self.SESSION_ID_STRING = SESSION_ID_STRING def request(self, host, handler, request_body, verbose=0): # issue XML-RPC request h = self.make_connection(host) if verbose: h.set_debuglevel(1) self.send_request(h, handler, request_body) self.send_host(h, host) h.putheader("Cookie", "%s=%s" % ('xoid',self.__get_xs_cookie()) ) self.send_content(h, request_body) errcode, errmsg, headers = h.getreply() if errcode != 200: raise xmlrpclib.ProtocolError( host + handler, errcode, errmsg, headers ) self.verbose = verbose try: sock = h._conn.sock except AttributeError: sock = None return self._parse_response(h.getfile(), sock) def __get_xs_cookie(self,): ''' Create a HTTP Cookie to authenticate with the Schoolserver ''' # Get Sugar's public key, and use its hash and the colors as the cookie pubkey = profile.get_profile().pubkey cookie_data = {'color': profile.get_color().to_string(), 'pkey_hash': sha.new(pubkey).hexdigest()} cookie = cjson.encode(cookie_data) return cookie