def setUp(self): # noqa # load a suitable database schema metadata = lti.load_metadata() self.container = metadata.root.DataServices.defaultContainer self.data_source = sql.SQLiteEntityContainer( file_path=':memory:', container=self.container) self.data_source.create_all_tables() self.cipher = wsgi.AppCipher(0, 'secret', self.container['AppKeys']) with self.container['Silos'].OpenCollection() as collection: self.silo = collection.new_entity() self.silo['ID'].set_from_value(wsgi.key60('testing')) self.silo['Slug'].set_from_value('testing') collection.insert_entity(self.silo)
def setUp(self): # noqa # load a suitable database schema metadata = lti.load_metadata() self.container = metadata.root.DataServices.defaultContainer self.data_source = sql.SQLiteEntityContainer( file_path=':memory:', container=self.container) self.data_source.create_all_tables() self.cipher = wsgi.AppCipher(0, 'secret', self.container['AppKeys']) with self.container['Silos'].open() as collection: self.silo = collection.new_entity() self.silo['ID'].set_from_value(wsgi.key60(b'testing')) self.silo['Slug'].set_from_value('testing') collection.insert_entity(self.silo)
def setUp(self): # noqa # load a suitable database schema metadata = lti.load_metadata() self.container = metadata.root.DataServices.defaultContainer self.data_source = sql.SQLiteEntityContainer( file_path=':memory:', container=self.container) self.data_source.create_all_tables() self.cipher = wsgi.AppCipher(0, 'secret', self.container['AppKeys']) with self.container['Silos'].open() as collection: self.silo = collection.new_entity() self.silo['ID'].set_from_value(wsgi.key60('testing')) self.silo['Slug'].set_from_value('testing') collection.insert_entity(self.silo) self.save_time = time.time self.mock_time = MockTime(1420370306 + MockTime.oauth_origin) # patch the time module to mock it time.time = self.mock_time.time
def setUp(self): # noqa # load a suitable database schema metadata = lti.load_metadata() self.container = metadata.root.DataServices.defaultContainer self.data_source = sql.SQLiteEntityContainer(file_path=':memory:', container=self.container) self.data_source.create_all_tables() self.cipher = wsgi.AppCipher(0, 'secret', self.container['AppKeys']) with self.container['Silos'].OpenCollection() as collection: self.silo = collection.new_entity() self.silo['ID'].set_from_value(wsgi.key60('testing')) self.silo['Slug'].set_from_value('testing') collection.insert_entity(self.silo) self.save_time = time.time self.mock_time = MockTime(1420370306 + MockTime.oauth_origin) # patch the time module to mock it time.time = self.mock_time.time
def setUp(self): # noqa metadata = lti.load_metadata() self.container = metadata.root.DataServices.defaultContainer self.data_source = sql.SQLiteEntityContainer( file_path=':memory:', container=self.container) self.data_source.create_all_tables() self.cipher = wsgi.AppCipher(0, 'secret', self.container['AppKeys']) with self.container['Silos'].OpenCollection() as collection: self.silo = collection.new_entity() self.silo['ID'].set_from_value(wsgi.key60('ToolProviderSession')) self.silo['Slug'].set_from_value('ToolProviderSession') collection.insert_entity(self.silo) # create a consumer with self.silo['Consumers'].OpenCollection() as collection: self.consumer = lti.ToolConsumer.new_from_values( collection.new_entity(), self.cipher, 'test', '12345', 'secret') collection.insert_entity(self.consumer.entity)
def setUp(self): # noqa metadata = lti.load_metadata() self.container = metadata.root.DataServices.defaultContainer self.data_source = sql.SQLiteEntityContainer( file_path=':memory:', container=self.container) self.data_source.create_all_tables() self.cipher = wsgi.AppCipher(0, 'secret', self.container['AppKeys']) with self.container['Silos'].open() as collection: self.silo = collection.new_entity() self.silo['ID'].set_from_value(wsgi.key60(b'ToolProviderSession')) self.silo['Slug'].set_from_value('ToolProviderSession') collection.insert_entity(self.silo) # create a consumer with self.silo['Consumers'].open() as collection: self.consumer = lti.ToolConsumer.new_from_values( collection.new_entity(), self.cipher, 'test', '12345', 'secret') collection.insert_entity(self.consumer.entity)
def gclient_action(self, context): """Handles Google+ sign-in postmessage callback Exchange the one-time authorization code for a token and store the token in the session.""" if context.environ['REQUEST_METHOD'].upper() != 'POST': raise wsgi.MethodNotAllowed if not self.google_id: raise wsgi.BadRequest # the wsgi.session_decorator took care of checking the CSRF # token already code = context.get_form_string('code') if code == 'logout': # this is the logout action context.session.del_owner() context.set_status(200) return self.json_response( context, json.dumps("session owner logged out")) # swap this code for an OAuth 2 access token gclient = oauth2.WebApplicationClient(client_id=self.google_id) body = gclient.prepare_request_body( code=code, client_secret=self.google_secret, redirect_uri='postmessage') req = http.ClientRequest("https://accounts.google.com/o/oauth2/token", method="POST", entity_body=str(body)) req.set_accept("application/json") req.set_content_type('application/x-www-form-urlencoded;charset=UTF-8') self.http.process_request(req) if req.status != 200: logging.warn("OAuth request returned status: %i", req.status) raise wsgi.BadRequest gclient.parse_request_body_response(req.res_body) url, headers, data = gclient.add_token( 'https://www.googleapis.com/oauth2/v1/userinfo', http_method="GET") req = http.ClientRequest(url) req.set_accept("application/json") req.set_content_type('application/x-www-form-urlencoded;charset=UTF-8') for h, v in headers.items(): req.set_header(h, v) self.http.process_request(req) if req.status != 200: logging.warn("OAuth request returned status: %i", req.status) raise wsgi.BadRequest userinfo = json.loads(req.res_body.decode('utf-8')) current_owner = context.session.get_owner() if current_owner: if (current_owner['IDType'].value == 'google' and current_owner['ID'].value == userinfo['id']): # we're already logged in to this session logging.warn("google user already logged in") context.set_status(200) return self.json_response( context, json.dumps("Already logged in")) # clear this link with context.session.entity['Owner'].OpenCollection() as \ collection: collection.clear() logging.debug("Google user logged in: %s <%s>", userinfo['name'], userinfo['email']) with self.container['Owners'].OpenCollection() as collection: # let's find this user in our database id = edm.EDMValue.NewSimpleValue(edm.SimpleType.String) id.set_from_value(userinfo['id']) filter = odata.CommonExpression.from_str( "IDType eq 'google' and ID eq :id", {'id': id}) collection.set_filter(filter) owners = collection.values() if len(owners) == 0: # first time we ever saw this person, create an entry owner = collection.new_entity() owner['Key'].set_from_value( wsgi.key60('gmail:' + userinfo['id'])) owner['IDType'].set_from_value('google') owner['ID'].set_from_value(userinfo['id']) owner['GivenName'].set_from_value(userinfo['given_name']) owner['FamilyName'].set_from_value(userinfo['family_name']) owner['FullName'].set_from_value(userinfo['name']) owner['Email'].set_from_value(userinfo['email']) owner['Session'].BindEntity(context.session.entity) # and create them a silo for their data with owner['Silo'].Target().OpenCollection() as silos: silo = silos.new_entity() silo['ID'].set_from_value(owner['Key'].value) silo['Slug'].set_from_value(userinfo['email']) silos.insert_entity(silo) owner['Silo'].BindEntity(silo) collection.insert_entity(owner) # and finally create a default consumer for them with silo['Consumers'].OpenCollection() as collection: consumer = lti.ToolConsumer.new_from_values( collection.new_entity(), self.app_cipher, "default") collection.insert_entity(consumer.entity) elif len(owners) == 1: # we already saw this user owner = owners[0] # update the record from the latest userinfo owner['GivenName'].set_from_value(userinfo['given_name']) owner['FamilyName'].set_from_value(userinfo['family_name']) owner['FullName'].set_from_value(userinfo['name']) owner['Email'].set_from_value(userinfo['email']) owner['Session'].BindEntity(context.session.entity) collection.update_entity(owner) else: logging.error("Duplicate google owner: %s <%s>", id.value, userinfo['email']) raise RuntimeError("Unexpected duplicate in Owners") context.set_status(200) return self.json_response( context, json.dumps("%s now logged in" % userinfo['name']))