def get_user_from_external_user_id(external_user_id): """ Import ExternalId model and find user by external_user_id """ # pylint: disable=import-error,import-outside-toplevel from openedx.core.djangoapps.external_user_ids.models import ExternalId try: external_id = ExternalId.objects.get(external_user_id=external_user_id, external_id_type__name='lti') return external_id.user except ExternalId.DoesNotExist as exception: raise LtiError('Invalid User') from exception except ValidationError as exception: raise LtiError('Invalid userID') from exception
def public_keyset_endpoint(request, usage_id=None): """ Gate endpoint to fetch public keysets from a problem This is basically a passthrough function that uses the OIDC response parameter `login_hint` to locate the block and run the proper handler. """ try: lti_config = LtiConfiguration.objects.get( location=UsageKey.from_string(usage_id)) if lti_config.version != lti_config.LTI_1P3: raise LtiError( "LTI Error: LTI 1.1 blocks do not have a public keyset endpoint." ) # Retrieve block's Public JWK # The underlying method will generate a new Private-Public Pair if one does # not exist, and retrieve the values. response = JsonResponse(lti_config.lti_1p3_public_jwk) response['Content-Disposition'] = 'attachment; filename=keyset.json' return response except (LtiError, InvalidKeyError, ObjectDoesNotExist) as exc: log.info("Error while retrieving keyset for usage_id %r: %s", usage_id, exc) raise Http404 from exc
def test_parse_suffix_raises_error(self, mock_parse_suffix): """ Test 404 response returned when the user id cannot be parsed from the request path suffix """ mock_parse_suffix.side_effect = LtiError() response = self.xblock.result_service_handler(make_request('', 'GET')) self.assertEqual(response.status_code, 404)
def test_get_result_raises_error(self, mock_parse_suffix, mock_get_result): """ Test 404 response returned when the LtiConsumer result service handler methods raise an exception """ mock_parse_suffix.return_value = FAKE_USER_ID mock_get_result.side_effect = LtiError() response = self.xblock.result_service_handler(make_request('', 'GET')) self.assertEqual(response.status_code, 404)
def test_verify_headers_raises_error(self, mock_parse_suffix, mock_verify_result_headers): """ Test 401 response returned when `verify_result_headers` raises LtiError """ mock_parse_suffix.return_value = FAKE_USER_ID mock_verify_result_headers.side_effect = LtiError() response = self.xblock.result_service_handler(make_request('', 'GET')) self.assertEqual(response.status_code, 401)
def get_course_members(course_key): """ Returns a dict containing all users associated with the given course """ # pylint: disable=import-error,import-outside-toplevel from lms.djangoapps.course_api.api import get_course_members as core_get_course_members from lms.djangoapps.course_api.exceptions import OverEnrollmentLimitException try: return core_get_course_members(course_key) except OverEnrollmentLimitException as ex: raise LtiError( 'NRPS is not available for {}'.format(course_key)) from ex