diff --git a/blti/roles.py b/blti/roles.py new file mode 100644 index 0000000..bce0634 --- /dev/null +++ b/blti/roles.py @@ -0,0 +1,40 @@ +# Copyright 2024 UW-IT, University of Washington +# SPDX-License-Identifier: Apache-2.0 + + +LTI_1p3_ROLES_CLAIM = "https://purl.imsglobal.org/spec/lti/claim/roles" +LTI_1p3_ROLE_PREFIX = "http://purl.imsglobal.org/vocab/lis/v2/" + + +def roles_from_role_name(role_names): + roles = [] + + for role_name in role_names: + if role_name == 'User': + roles += ["system/person#User"] + elif role_name in ['Observer', 'Mentor']: + roles += [ + "institution/person#Observer", + "institution/person#Mentor", + "membership#Mentor"] + elif role_name in ['Student', 'Learner']: + roles += [ + "institution/person#Learner", + "institution/person#Student", + "membeship#Learner"] + elif role_name in ['Instructor', 'Faculty', 'Teacher']: + roles += [ + "institution/person#Instructor", + "institution/person#Faculty", + "membeship#Instructor"] + elif role_name == 'Administrator': + roles += [ + "system/person#User", + "institution/person#Administrator", + "membeship#Administrator"] + elif role_name == 'TeachingAssistant': + roles += ["membership/Instructor#TeachingAssistant"] + elif role_name == 'ContentDeveloper': + roles += ["membership#ContentDeveloper"] + + return LTI_1p3_ROLES_CLAIM, [f"{LTI_1p3_ROLE_PREFIX}{r}" for r in roles] diff --git a/blti/tests/test_lti_1p3.py b/blti/tests/test_lti_1p3.py index d64946d..69fed3b 100644 --- a/blti/tests/test_lti_1p3.py +++ b/blti/tests/test_lti_1p3.py @@ -8,6 +8,7 @@ from django.core.exceptions import ImproperlyConfigured from blti.validators import BLTIRequestValidator, Roles from blti.models import CanvasData +from blti.roles import roles_from_role_name from blti.performance import log_response_time from blti import BLTI, LTI_DATA_KEY from blti.mock_data import Mock1p3Data @@ -135,39 +136,8 @@ def test_authorize_specific(self): CanvasData(**self.params)).authorize, role='Manager') def _set_role(self, role): - self.params[ - "https://purl.imsglobal.org/spec/lti/claim/roles"] = [] - roles = [] - if role == 'User': - roles = ["system/person#User"] - elif role in ['Observer', 'Mentor']: - roles = ["institution/person#Observer", - "institution/person#Mentor", - "membership#Mentor"] - elif role in ['Student', 'Learner']: - roles = [ - "institution/person#Learner", - "institution/person#Student", - "membeship#Learner"] - elif role == 'Instructor': - roles = [ - "institution/person#Instructor", - "institution/person#Faculty", - "membeship#Instructor"] - elif role == 'Administrator': - roles = [ - "system/person#User", - "institution/person#Administrator", - "membeship#Administrator"] - elif role == 'TeachingAssistant': - roles = ["membership/Instructor#TeachingAssistant"] - elif role == 'ContentDeveloper': - roles = ["membership#ContentDeveloper"] - - for r in roles: - self.params[ - "https://purl.imsglobal.org/spec/lti/claim/roles"].append( - f"http://purl.imsglobal.org/vocab/lis/v2/{r}") + claim, roles = roles_from_role_name([role]) + self.params[claim] = roles class BLTI1p3SessionTest(TestCase): diff --git a/blti/views/develop.py b/blti/views/develop.py index bb44c7d..59cfe6d 100644 --- a/blti/views/develop.py +++ b/blti/views/develop.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 from blti.mock_data import Mock1p3Data +from blti.roles import roles_from_role_name from django.views.generic import TemplateView from django.urls import reverse, resolve from django.urls.exceptions import NoReverseMatch @@ -29,22 +30,8 @@ def get_context_data(self, **kwargs): f"uwcourse:{campus}:a-and-s:pych:psych") # insert role - role_base = "http://purl.imsglobal.org/vocab/lis/v2" - roles = [f"{role_base}system/person#User"] - if role == "Instructor": - roles += [f"{role_base}/institution/person#Instructor", - f"{role_base}/membership#Instructor"] - elif role == "TeachingAssistant": - roles += [f"{role_base}/membership#TeachingAssistant"] - elif role == "Student": - roles += [f"{role_base}/institution/person#Student", - f"{role_base}/membership#Learner"] - elif role == "Administrator": - roles += [f"{role_base}/system/person#Administrator"] - elif role == "ContentDeveloper": - roles += [f"{role_base}/membership#ContentDeveloper"] - - mock_jwt["https://purl.imsglobal.org/spec/lti/claim/roles"] = roles + claim, roles = roles_from_role_name(['User', role]) + mock_jwt[claim] = roles launch_module, launch_class = self._launch_module_and_class() launch_class.validate_1p3 = lambda launch_class, request: mock_jwt