Skip to content

Commit ed141e0

Browse files
authored
adding role requirement check
1 parent cf852aa commit ed141e0

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

auth.php

+37
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ private function attempt_jwt_login() {
108108
if (is_null($payload))
109109
return;
110110

111+
$satisfiesRoleRequirement = doesPayloadSatisfyRoleRequirement($payload);
112+
if ($satisfiesRoleRequirement == false)
113+
return;
114+
111115
/**
112116
* We allow the environment to specify whether to perform an issuer check.
113117
*
@@ -217,6 +221,39 @@ private function attempt_jwt_login() {
217221
complete_user_login($updatedUser);
218222
}
219223

224+
/**
225+
* Check whether a parsed JWT payload satisfies the environmental role checks.
226+
*
227+
* This will require that the following two values are specified:
228+
*
229+
* 1. MOODLE_JWT_CHECK_FOR_ROLE: Whether to use this check, expects string `true`.
230+
* 2. MOODLE_JWT_REQUIRED_ROLE: The role to require before allowing logins.
231+
* 3. MOODLE_JWT_ROLE_FIELD: The field to check for the specified role.
232+
*
233+
* If either of these are not present, then the user will be able to log in.
234+
*/
235+
function doesPayloadSatisfyRoleRequirement($payload) {
236+
237+
$checkForRole = getenv('MOODLE_JWT_USE_ROLE_CHECK');
238+
$roleField = getenv('MOODLE_JWT_ROLE_FIELD');
239+
$requiredRole = getenv('MOODLE_JWT_REQUIRED_ROLE');
240+
241+
if (empty($checkForRole) || $checkForRole != "true") {
242+
return true;
243+
}
244+
245+
if (empty($roleField) || empty($requiredRole)) {
246+
return false;
247+
}
248+
249+
if (isset($payload->$roleField) && is_array($payload->$roleField)) {
250+
return in_array($requiredRole, $payload->$roleField);
251+
}
252+
253+
// Return false if the field is not present or the role is not found
254+
return false;
255+
}
256+
220257
/**
221258
* Use the information provided in the cert + environment variables to determine
222259
* the expected username for this account.

debug/check.php

+22-11
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,40 @@
3535

3636
function doesPayloadSatisfyRoleRequirement($payload) {
3737

38-
$roleField = getenv('MOODLE_JWT_ROLE_FIELD');
39-
$requiredRole = getenv('MOODLE_JWT_REQUIRED_ROLE');
40-
41-
if (empty($roleField) || empty($requiredRole)) {
42-
return true;
43-
}
38+
$checkForRole = getenv('MOODLE_JWT_USE_ROLE_CHECK');
39+
$roleField = getenv('MOODLE_JWT_ROLE_FIELD');
40+
$requiredRole = getenv('MOODLE_JWT_REQUIRED_ROLE');
4441

45-
if (isset($payload->$roleField) && is_array($payload->$roleField)) {
46-
return in_array($requiredRole, $payload->$roleField);
47-
}
42+
if (empty($checkForRole) || $checkForRole != "true") {
43+
return true;
44+
}
45+
46+
if (empty($roleField) || empty($requiredRole)) {
47+
return false;
48+
}
4849

49-
// Return false if the field is not present or the role is not found
50-
return false;
50+
if (isset($payload->$roleField) && is_array($payload->$roleField)) {
51+
return in_array($requiredRole, $payload->$roleField);
52+
}
53+
54+
// Return false if the field is not present or the role is not found
55+
return false;
5156
}
5257

5358
$payloadWithRole = (object)[
5459
'roles' => ['admin', 'editor', 'user']
5560
];
5661

62+
putenv("MOODLE_JWT_USE_ROLE_CHECK=");
5763
echo "EXPECTING SUCCESS ...";
5864
echo true == doesPayloadSatisfyRoleRequirement($payloadWithRole);
5965
echo "\n";
6066

67+
putenv("MOODLE_JWT_USE_ROLE_CHECK=true");
68+
echo "EXPECTING FAILURE ...";
69+
echo false == doesPayloadSatisfyRoleRequirement($payloadWithRole);
70+
echo "\n";
71+
6172
echo "EXPECTING FAILURE ...";
6273
putenv("MOODLE_JWT_ROLE_FIELD=non-existent-property");
6374
putenv("MOODLE_JWT_REQUIRED_ROLE=admin");

0 commit comments

Comments
 (0)