Skip to content

Commit 69fba59

Browse files
committed
Check for LMS ID first when authenticating from LMS
1 parent 44e76a4 commit 69fba59

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

lib/WeBWorK/Authen/LTIAdvantage.pm

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,14 @@ sub get_credentials ($self) {
132132
return 0;
133133
}
134134

135-
# Determine the user_id to use, if possible.
136-
if (!$ce->{LTI}{v1p3}{preferred_source_of_username}) {
137-
warn 'LTI is not properly configured (no preferred_source_of_username). '
138-
. "Please contact your instructor or system administrator.\n";
139-
$self->{error} = $c->maketext(
140-
'There was an error during the login process. Please speak to your instructor or system administrator.');
141-
debug("No preferred_source_of_username in $ce->{courseName} so LTIAdvantage::get_credentials is returning 0.");
142-
return 0;
143-
}
135+
# First check if we already have a user with the current lis_source_did
136+
my $user = ($c->db->getUsersWhere({ lis_source_did => $c->stash->{lti_lms_user_id} }))[0] // ''
137+
if $c->stash->{lti_lms_user_id};
144138

139+
my $user_id;
145140
my $user_id_source = '';
146141
my $type_of_source = '';
147142

148-
$self->{email} = $claims->{email} // '';
149-
150143
my $extract_claim = sub ($key) {
151144
my $value = $claims;
152145
for (split '#', $key) {
@@ -159,19 +152,42 @@ sub get_credentials ($self) {
159152
return $value;
160153
};
161154

162-
if (my $user_id = $extract_claim->($ce->{LTI}{v1p3}{preferred_source_of_username})) {
163-
$user_id_source = $ce->{LTI}{v1p3}{preferred_source_of_username};
164-
$type_of_source = 'preferred_source_of_username';
165-
$self->{user_id} = $user_id;
166-
}
155+
if ($user) {
156+
$user_id_source = $c->stash->{lti_lms_user_id};
157+
$type_of_source = 'lis_source_did';
158+
$self->{user_id} = $user->user_id;
159+
} else {
160+
# Determine the user_id to use, if possible.
161+
if (!$ce->{LTI}{v1p3}{preferred_source_of_username}) {
162+
warn 'LTI is not properly configured (no preferred_source_of_username). '
163+
. "Please contact your instructor or system administrator.\n";
164+
$self->{error} = $c->maketext(
165+
'There was an error during the login process. Please speak to your instructor or system administrator.'
166+
);
167+
debug(
168+
"No preferred_source_of_username in $ce->{courseName} so LTIAdvantage::get_credentials is returning 0."
169+
);
170+
return 0;
171+
}
172+
173+
if ($user_id = $extract_claim->($ce->{LTI}{v1p3}{preferred_source_of_username})) {
174+
$user_id_source = $ce->{LTI}{v1p3}{preferred_source_of_username};
175+
$type_of_source = 'preferred_source_of_username';
176+
$self->{user_id} = $user_id;
177+
}
167178

168-
# Fallback if necessary
169-
if (!defined $self->{user_id} && (my $user_id = $extract_claim->($ce->{LTI}{v1p3}{fallback_source_of_username}))) {
170-
$user_id_source = $ce->{LTI}{v1p3}{fallback_source_of_username};
171-
$type_of_source = 'fallback_source_of_username';
172-
$self->{user_id} = $user_id;
179+
# Fallback if necessary
180+
if (!defined $self->{user_id}
181+
&& (my $user_id = $extract_claim->($ce->{LTI}{v1p3}{fallback_source_of_username})))
182+
{
183+
$user_id_source = $ce->{LTI}{v1p3}{fallback_source_of_username};
184+
$type_of_source = 'fallback_source_of_username';
185+
$self->{user_id} = $user_id;
186+
}
173187
}
174188

189+
$self->{email} = $claims->{email} // '';
190+
175191
if ($self->{user_id}) {
176192
# Strip off the part of the address after @ if the email address was used and it was requested to do so.
177193
$self->{user_id} =~ s/@.*$// if $user_id_source eq 'email' && $ce->{LTI}{v1p3}{strip_domain_from_email};
@@ -188,6 +204,8 @@ sub get_credentials ($self) {
188204
[ recitation => 'https://purl.imsglobal.org/spec/lti/claim/custom#recitation' ],
189205
);
190206

207+
$self->{lis_source_did} = $c->stash->{lti_lms_user_id} if $c->stash->{lti_lms_user_id};
208+
191209
$self->{student_id} =
192210
$ce->{LTI}{v1p3}{preferred_source_of_student_id}
193211
? ($extract_claim->($ce->{LTI}{v1p3}{preferred_source_of_student_id}) // '')

0 commit comments

Comments
 (0)