Package: lti1p3platform
LTI 1.3 Platform implementation in PythonUsageRegister your platformThe platform should prepare the launch by gathering the necessary context information, including details about the user, the course, and any custom parameters that need to be included in the launch request.fromlti1p3platform.ltiplatformimportLTI1P3PlatformConfAbstractfromlti1p3platform.registrationimportRegistrationclassLTIPlatformConf(LTI1P3PlatformConfAbstract):definit_platform_config(self,platform_settings,platform_key_set):"""register platform configuration"""registration=Registration()\.set_client_id(platform_settings.client_id)\.set_deployment_id(platform_settings.deployment_id)\.set_launch_url(platform_settings.launch_url)\.set_deeplink_launch_url(platform_settings.deeplink_launch_url)\.set_oidc_login_url(platform_settings.oidc_login_url)\.set_tool_key_set_url(platform_settings.key_set_url)\.set_platform_public_key(platform_key_set.public_key)\.set_platform_private_key(platform_key_set.private_key)self._registration=registrationdefget_registered_platform(*args,**kwargs):...returnLTIPlatformConf(*args,**kwargs)# public JWK endpointdefget_jwks(request,*args,**kwargs):platform=get_registered_platform(*args,**kwargs)returnHttpResponseJSON(platform.get_jwks())OIDC initiate loginThe tool consumer (i.e., the LMS) sends a request to the tool provider's application to initiate the OIDC authentication flow.fromlti1p3platform.oidc_loginimportOIDCLoginAbstractclassOIDCLogin(OIDCLoginAbstract):defset_lti_message_hint(self,**kwargs):""" set your own lti_message_hint """passdefget_lti_message_hint(self):""" get your lti_message_hint """passdefget_redirect(self,url):"""This will be invoked in initiate_login, and it depends on which web framework you are using.Here is an example for Django framework:"""returnHttpResponseRedirect(url)# Initiate login endpointdefpreflight_lti_1p3_launch(request,user_id,*args,**kwargs):platform=get_registered_platform(*args,**kwargs)oidc_login=OLOIDCLogin(request,platform)# Redirect the current login user to the tool provider,returnredirect_url.initiate_login(user_id)LTI Message launchThe tool provider redirect to the platform's OIDC auth request endpoint. The platform received the auth request and it will do some little bit of validation, it needs to ensure user is login, also check thelogin_hintis matched with theuser_id. The platform also could get the context from thelti_message_hintwhich is sent in the initiating request and do some other validation.After all verifications, the platform will generate aid_token. The platform encodes all important launch message payload as a JWT and send it asid_tokenparameter in a post request to the tool launch url.fromlti1p3platform.message_launchimportMessageLaunchAbstractclassLTI1p3MessageLaunch(MessageLaunchAbstract):defrender_launch_form(self,launch_data,**kwargs):"""This will be invoked in the last step of `lti_launch`.So you could render a template in this method. This template should render a form, and then submit it to the tool's launch URL. There is a django example in framework/django/message_launch.py"""passdefprepare_launch(self,preflight_response,**kwargs):"""You could do some other checks and get some contexts from `lti_message_hint` you've set in previous requestAlso you could call these methods to prepare your own jwt payload:- set_user_data- set_resource_link_claim- set_launch_context_claim- set_custom_parameters_claimMake sure do these things before lti_launch, it could send necessary launch parameters to the tool."""passdeflti_resource_link_launch(request,*args,**kwargs):platform=get_registered_platform(*args,**kwargs)message_launch=LTI1p3MessageLaunch(request,*args,**kwargs)returnlaunch.lti_launch(*args,**kwargs)ExamplesDjango exampleDevelopmentRun testPrerequisite: tox and python 3.7, 3.8, 3.9, 3.10If you are using pyenv virtualenv, you might need to install all python versions and runpyenv local 3.7.x 3.8.x 3.9.x 3.10.xat the first time.cdlti1p3platform
tox