Skip to content

Commit

Permalink
Merge pull request #150 from open-craft/pooja/fix-parameter-processor…
Browse files Browse the repository at this point in the history
…-logic

[SE-4235] Add missing parameter processors code
  • Loading branch information
giovannicimolin committed Aug 6, 2021
2 parents a5ca2cd + 1926299 commit 41d9dd8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lti_consumer/lti_1p1/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def __init__(self, lti_launch_url, oauth_key, oauth_secret):
self.lti_launch_presentation_locale = None
self.lti_custom_parameters = None

# Extra claims - used for custom parameter processors
self.extra_claims = {}

def set_user_data(
self,
user_id,
Expand Down Expand Up @@ -241,6 +244,14 @@ def set_custom_parameters(self, custom_parameters):

self.lti_custom_parameters = custom_parameters

def set_extra_claims(self, claim):
"""
Updates launch extra claims using python's dict .update method
"""
if not isinstance(claim, dict):
raise ValueError('Invalid extra claim: {!r} is not a dict.'.format(claim))
self.extra_claims.update(claim)

def generate_launch_request(self, resource_link_id):
"""
Signs LTI launch request and returns signature and OAuth parameters.
Expand Down Expand Up @@ -288,6 +299,10 @@ def generate_launch_request(self, resource_link_id):
if self.lti_custom_parameters:
lti_parameters.update(self.lti_custom_parameters)

# Extra claims - from custom parameter processors
if self.extra_claims:
lti_parameters.update(self.extra_claims)

headers = {
# This is needed for body encoding:
'Content-Type': 'application/x-www-form-urlencoded',
Expand Down
10 changes: 10 additions & 0 deletions lti_consumer/lti_xblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,16 @@ def lti_launch_handler(self, request, suffix=''): # pylint: disable=unused-argu

lti_consumer.set_custom_parameters(self.prefixed_custom_parameters)

for processor in self.get_parameter_processors():
try:
default_params = getattr(processor, 'lti_xblock_default_params', {})
lti_consumer.set_extra_claims(default_params)
lti_consumer.set_extra_claims(processor(self) or {})
except Exception: # pylint: disable=broad-except
# Log the error without causing a 500-error.
# Useful for catching casual runtime errors in the processors.
log.exception('Error in XBlock LTI parameter processor "%s"', processor)

lti_parameters = lti_consumer.generate_launch_request(self.resource_link_id)
loader = ResourceLoader(__name__)
context = self._get_context_for_template()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def is_requirement(line):

setup(
name='lti-consumer-xblock',
version='3.0.2',
version='3.0.3',
author='Open edX project',
author_email='oscm@edx.org',
description='This XBlock implements the consumer side of the LTI specification.',
Expand Down

0 comments on commit 41d9dd8

Please sign in to comment.