Browse Source

autoconfigure lti for canvas based on tool_consumer_info_product_family_code

pull/75/head
ichuang 1 month ago
parent
commit
10ec729417
Signed by: hz <hartz@mit.edu> GPG Key ID: 5FDD2840E179AD62
1 changed files with 24 additions and 5 deletions
  1. +24
    -5
      catsoop/lti.py

+ 24
- 5
catsoop/lti.py View File

@@ -18,6 +18,7 @@
LTI Tool Provider interface
"""

import re
import uuid
import urllib
import pylti.common
@@ -44,11 +45,12 @@ class lti4cs(pylti.common.LTIBase):
def __init__(self, context, session, lti_args, lti_kwargs):
self.session = session
self.lti_data = {}
self.config = context.get("cs_lti_config", {})
pylti.common.LTIBase.__init__(self, lti_args, lti_kwargs)

self.consumers = context.get("cs_lti_config")["consumers"]
self.lti_session_key = context.get("cs_lti_config")["session_key"]
self.base_url = context.get("cs_lti_config", {}).get("base_url")
self.consumers = self.config["consumers"]
self.lti_session_key = self.config["session_key"]
self.base_url = self.config.get("base_url")

def verify_request(self, params, environment):
try:
@@ -66,7 +68,13 @@ class lti4cs(pylti.common.LTIBase):
self.consumers, url, method, environment, params
)
LOGGER.info("[lti.lti4cs.verify_request] verify_request success")
for prop in pylti.common.LTI_PROPERTY_LIST:
extra_fields = [ self.config.get("lti_user_id_field") ] + ['tool_consumer_info_product_family_code',
'tool_consumer_.*',
'custom_canvas_.*',
]
for prop in pylti.common.LTI_PROPERTY_LIST + extra_fields:
if prop is None:
continue
if params.get(prop, None):
LOGGER.info(
"[lti.lti4cs.verify_request] params %s=%s",
@@ -74,6 +82,12 @@ class lti4cs(pylti.common.LTIBase):
params.get(prop, None),
)
self.lti_data[prop] = params[prop]
continue
for key in params:
m = re.match(prop, key)
if m:
LOGGER.info("[lti.lti4cs.verify_request] params %s=%s", prop, params.get(key, None))
self.lti_data[key] = params[key]

self.session["lti_data"] = self.lti_data
return True
@@ -318,7 +332,12 @@ def serve_lti(context, path_info, environment, params, dispatch_main, return_con
elif l4c is not None:
lti_data = session_data["lti_data"]
lup = context["cs_lti_config"].get("lti_username_prefix", "lti_")
lti_user_id_field = context["cs_lti_config"].get("lti_user_id_field", "user_id") # LTI field to use for uname

default_user_id_field = "user_id" # used by OpenEdX
if 'canvas' in lti_data.get("tool_consumer_info_product_family_code"):
default_user_id_field = "custom_canvas_user_login_id" # used by Canvas LMS
lti_user_id_field = context["cs_lti_config"].get("lti_user_id_field", default_user_id_field) # allow config to override LTI field to use for uname

lti_uname = lti_data[lti_user_id_field]
if not context["cs_lti_config"].get("force_username_from_id"):
lti_uname = lti_data.get(


Loading…
Cancel
Save