def dispatch(self, request, *args, **kwargs): """ Handle LTI verification and user authentication """ # validate the request lti = LTI(request_type="any", role_type="any") try: lti.verify(request) except LTIException: return render(request, "lti_failure.html") if not request.user.is_authenticated: try: lti_user = login_existing_user(request) except EmailAddress.DoesNotExist: lti_email = request.POST.get(LTI_PROPERTY_USER_EMAIL, None) if not lti_email: return render(request, "lti_failure.html") self.create_lti_user(lti_email) try: lti_user = login_existing_user(request) except EmailAddress.DoesNotExist: return render(request, "lti_failure.html") if lti_user is False: return render(request, "lti_failure.html") return super().dispatch(request, *args, **kwargs)
def lti_session(request): try: lti = LTI('session', 'any') if lti.verify(request): return lti except LTINotInSessionException: return None
def test_verify_session(self): lti = LTI('session', 'any') request = RequestFactory().post('/lti/') with self.assertRaises(LTINotInSessionException): request.session = {} lti.verify(request) request.session = {LTI_SESSION_KEY: True} self.assertTrue(lti.verify(request))
def test_verify_any(self): lti = LTI('any', 'any') request = generate_lti_request() with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): # test_verify_request lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY]) # test_verify_session self.assertTrue(lti.verify(request))
def test_lti_session(self): with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): request = generate_lti_request() self.assertIsNone(lti_session(request)) # initialize the session lti = LTI('initial', 'any') self.assertTrue(lti.verify(request)) lti = lti_session(request) self.assertEqual(lti.user_id(request), 'student')
def dispatch(self, request, *args, **kwargs): lti = LTI(self.request_type, self.role_type) # validate the user via oauth user = authenticate(request=request, lti=lti) if user is None: lti.clear_session(request) return HttpResponseRedirect(reverse('lti-fail-auth')) # login login(request, user) self.lti = lti return super(LTILoggedInMixin, self).dispatch(request, *args, **kwargs)
def validate_request(request): consumer_key = settings.SECURE_SETTINGS['CONSUMER_KEY'] shared_secret = settings.SECURE_SETTINGS['LTI_SECRET'] if consumer_key is None or shared_secret is None: raise ImproperlyConfigured( "Unable to validate LTI launch. Missing setting: CONSUMER_KEY or LTI_SECRET" ) # Instantiate an LTI object with an 'initial' request type and 'any' role type lti_object = LTI('initial', 'any') return lti_object._verify_request(request)
def setUp(self): self.request = RequestFactory() self.request.COOKIES = {} middleware = SessionMiddleware() middleware.process_request(self.request) self.request.session.save() for prop, value in BASE_LTI_PARAMS.items(): self.request.session[prop] = value self.lti = LTI('initial', 'any') self.emptyRequest = RequestFactory() self.emptyRequest.COOKIES = {} middleware = SessionMiddleware() middleware.process_request(self.emptyRequest) self.emptyRequest.session.save()
def dispatch(self, request, *args, **kwargs): lti = LTI(self.request_type, self.role_type) # validate the user via oauth user = authenticate(request=request, lti=lti) if user is None: lti.clear_session(request) return HttpResponseRedirect(reverse('lti-fail-auth')) # login login(request, user) # configure course groups if requested try: self.course_configuration(request, lti) except (KeyError, ValueError, LTICourseContext.DoesNotExist): return HttpResponseRedirect(reverse('lti-course-config')) self.lti = lti return super(LTIAuthMixin, self).dispatch(request, *args, **kwargs)
def process_lti_launch_request_view(request): ''' Processes the lti launch request and redirects to the index view ''' #True if this is a typical lti launch. False if not. is_basic_lti_launch = request.method == 'POST' and request.POST.get( 'lti_message_type') == 'basic-lti-launch-request' lti = LTI(request_type="any", role_type="any") try: request_is_valid = lti.verify(request) except LTIException: # oauth session may have timed out or the LTI key/secret pair may be wrong return HttpResponseBadRequest( '<p>Something went wrong. Reload the page, or contact [email protected].<p>' ) if is_basic_lti_launch and request_is_valid: # Store the custom_canvas_course_id in the request's session attribute. request.session['course_id'] = request.POST.get( 'custom_canvas_course_id') return redirect('syllabuspdf:index') else: raise PermissionDenied
def test_verify_request(self): with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): request = generate_lti_request() lti = LTI('initial', 'any') lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY])
class LTITest(TestCase): def setUp(self): self.request = RequestFactory() self.request.COOKIES = {} middleware = SessionMiddleware() middleware.process_request(self.request) self.request.session.save() for prop, value in BASE_LTI_PARAMS.items(): self.request.session[prop] = value self.lti = LTI('initial', 'any') self.emptyRequest = RequestFactory() self.emptyRequest.COOKIES = {} middleware = SessionMiddleware() middleware.process_request(self.emptyRequest) self.emptyRequest.session.save() def test_init(self): self.assertEquals(self.lti.request_type, 'initial') self.assertEquals(self.lti.role_type, 'any') def test_consumer_user_id(self): self.request.session['oauth_consumer_key'] = '1234567890' self.assertEquals(self.lti.consumer_user_id(self.request), '1234567890-student') def test_user_email(self): self.assertIsNone(self.lti.user_email(self.emptyRequest)) self.assertEquals(self.lti.user_email(self.request), '*****@*****.**') def test_user_fullname(self): self.assertEquals(self.lti.user_fullname(self.emptyRequest), '') self.assertEquals(self.lti.user_fullname(self.request), 'Foo Bar Baz') def test_user_roles(self): self.assertEquals(self.lti.user_roles(self.emptyRequest), []) self.assertEquals(self.lti.user_roles(self.request), [ u'urn:lti:instrole:ims/lis/instructor', u'urn:lti:instrole:ims/lis/staff' ]) self.assertTrue(self.lti.is_instructor(self.request)) self.assertFalse(self.lti.is_administrator(self.request)) def test_consumers(self): with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): self.assertEquals(self.lti.consumers(), CONSUMERS) def test_params(self): factory = RequestFactory() request = factory.post('/', {'post': 'data'}) params = self.lti._params(request) self.assertTrue('post' in params) request = factory.post('/', {'get': 'data'}) params = self.lti._params(request) self.assertTrue('get' in params) def test_verify_any(self): lti = LTI('any', 'any') request = generate_lti_request() with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): # test_verify_request lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY]) # test_verify_session self.assertTrue(lti.verify(request)) def test_verify_session(self): lti = LTI('session', 'any') request = RequestFactory().post('/lti/') with self.assertRaises(LTINotInSessionException): request.session = {} lti.verify(request) request.session = {LTI_SESSION_KEY: True} self.assertTrue(lti.verify(request)) def test_verify_request(self): with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): request = generate_lti_request() lti = LTI('initial', 'any') lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY])
class LTITest(TestCase): def setUp(self): self.request = RequestFactory() self.request.COOKIES = {} middleware = SessionMiddleware() middleware.process_request(self.request) self.request.session.save() for prop, value in BASE_LTI_PARAMS.items(): self.request.session[prop] = value self.lti = LTI('initial', 'any') self.emptyRequest = RequestFactory() self.emptyRequest.COOKIES = {} middleware = SessionMiddleware() middleware.process_request(self.emptyRequest) self.emptyRequest.session.save() def test_init(self): self.assertEquals(self.lti.request_type, 'initial') self.assertEquals(self.lti.role_type, 'any') def test_consumer_user_id(self): self.request.session['oauth_consumer_key'] = '1234567890' self.assertEquals( self.lti.consumer_user_id(self.request), '1234567890-student') def test_user_email(self): self.assertIsNone(self.lti.user_email(self.emptyRequest)) self.assertEquals(self.lti.user_email(self.request), '*****@*****.**') def test_user_fullname(self): self.assertEquals(self.lti.user_fullname(self.emptyRequest), '') self.assertEquals(self.lti.user_fullname(self.request), 'Foo Bar Baz') def test_user_roles(self): self.assertEquals(self.lti.user_roles(self.emptyRequest), []) self.assertEquals(self.lti.user_roles(self.request), [ u'urn:lti:instrole:ims/lis/instructor', u'urn:lti:instrole:ims/lis/staff']) self.assertTrue(self.lti.is_instructor(self.request)) self.assertFalse(self.lti.is_administrator(self.request)) def test_consumers(self): with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): self.assertEquals(self.lti.consumers(), CONSUMERS) def test_params(self): factory = RequestFactory() request = factory.post('/', {'post': 'data'}) params = self.lti._params(request) self.assertTrue('post' in params) request = factory.post('/', {'get': 'data'}) params = self.lti._params(request) self.assertTrue('get' in params) def test_verify_any(self): lti = LTI('any', 'any') request = generate_lti_request() with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): # test_verify_request lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY]) # test_verify_session self.assertTrue(lti.verify(request)) def test_verify_session(self): lti = LTI('session', 'any') request = RequestFactory().post('/lti/') with self.assertRaises(LTINotInSessionException): request.session = {} lti.verify(request) request.session = {LTI_SESSION_KEY: True} self.assertTrue(lti.verify(request)) def test_verify_request(self): with self.settings(PYLTI_CONFIG={'consumers': CONSUMERS}): request = generate_lti_request() lti = LTI('initial', 'any') lti.verify(request) self.assertTrue(request.session[LTI_SESSION_KEY])