Skip to content

Commit eea4b70

Browse files
committed
Restrict LTI authentication by LTI course map.
This was requested by @dlglin, and restricts authentication into a webwork course to only users in the LMS course that matches the context id of the webwork course set in the LTI course map for the site. If the new `$LTI{v1p3}{restrictAuthenticationByCourseMap}` option is set to 1, then this restriction is in effect.
1 parent cb7ebbc commit eea4b70

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

conf/authen_LTI_1_3.conf.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ $LTI{v1p3}{ignoreMissingSourcedID} = 0;
261261

262262
$LTI{v1p3}{autoSyncSetDatesToLMS} = 0;
263263

264+
# If this is set then authentication into a webwork course is only allowed if the LMS context id
265+
# is set in the LTI course map for the site and that matches that of the LMS course from which
266+
# the current user is attempting to sign in.
267+
$LTI{v1p3}{restrictAuthenticationByCourseMap} = 0;
268+
264269
# If this is set and an instructor attempts to use content selection from an LMS course that is
265270
# that is not in the LTI course map, then the instructor will be offered a list of WeBWorK
266271
# courses to choose from. The WeBWorK courses that will be listed for the instructor are

lib/WeBWorK/Authen/LTIAdvantage.pm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ sub get_credentials ($self) {
101101
return 0;
102102
}
103103

104+
if ($ce->{LTI}{v1p3}{restrictAuthenticationByCourseMap} && !$c->stash->{courseChoices}) {
105+
my $courseMap = $c->db->getLTICourseMap($ce->{courseName});
106+
unless ($courseMap
107+
&& $courseMap->lms_context_id eq
108+
$c->stash->{lti_jwt_claims}{'https://purl.imsglobal.org/spec/lti/claim/context'}{id})
109+
{
110+
warn 'LTI authentication into incorrect course attempted. '
111+
. "Please contact your instructor or system administrator.\n";
112+
$self->{error} = $c->maketext(
113+
'There was an error during the login process. Please speak to your instructor or system administrator.'
114+
);
115+
debug("LTI authentication into $ce->{courseName} attempted, but this course is not in the LTI course map "
116+
. 'or the context id of the LMS course from which the user came does not match.');
117+
return 0;
118+
}
119+
}
120+
104121
# Determine the user_id to use, if possible.
105122
if (!$ce->{LTI}{v1p3}{preferred_source_of_username}) {
106123
warn 'LTI is not properly configured (no preferred_source_of_username). '

lib/WeBWorK/DB.pm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,16 @@ BEGIN {
976976
*deleteLTICourseMapWhere = gen_delete_where("lti_course_map");
977977
}
978978

979+
sub getLTICourseMap {
980+
my ($self, $courseID) = shift->checkArgs(\@_, qw/course_id/);
981+
return ($self->getLTICourseMaps($courseID))[0];
982+
}
983+
984+
sub getLTICourseMaps {
985+
my ($self, @courseIDs) = shift->checkArgs(\@_, qw/course_id*/);
986+
return $self->{lti_course_map}->gets(map { [$_] } @courseIDs);
987+
}
988+
979989
sub setLTICourseMap {
980990
my ($self, $course_id, $lms_context_id) = shift->checkArgs(\@_, qw/course_id lms_context_id/);
981991
if ($self->existsLTICourseMapWhere({ course_id => $course_id })) {

0 commit comments

Comments
 (0)