Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cookiemagic.py
blob: de1b02442642db66b00416eae37b20438f10debc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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