forked from adlnet/moodle-jwt-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
executable file
·444 lines (354 loc) · 13 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Auth using a JWT on a protected Moodle instance.
*
* @package auth_jwt
* @author Trey Hayden <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/authlib.php');
/**
* Plugin for no authentication.
*/
class auth_plugin_jwt extends auth_plugin_base {
/**
* Constructor.
*/
public function __construct() {
$this->authtype = 'jwt';
$this->config = get_config('auth_jwt');
$this->config->field_lock_email = "locked";
}
/**
* Old syntax of class constructor. Deprecated in PHP7.
*
* @deprecated since Moodle 3.1
*/
public function auth_plugin_jwt() {
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
self::__construct();
}
/**
* Catch the initial request before we send the user to a login page.
*
* This will be our primary way of checking the JWT Bearer token used in the
* Authorization header.
*/
public function pre_loginpage_hook() {
$this->attempt_jwt_login();
}
/**
* Catch the request a login page is actually loaded manually.
*
* This will allow a user to automatically log-in when pressing the
* dashboard's Log In button.
*/
public function loginpage_hook() {
$this->attempt_jwt_login();
}
private function attempt_jwt_login() {
global $CFG, $DB;
$authHeader = null;
/**
* Most deployments will be through Apache, at least for ADL, so
* we can leverage a convenient getter to help out.
*/
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
$authHeader = $headers['Authorization'];
}
}
/**
* Older versions of Moodle and those without an Apache server base
* will miss the previous check, so we can also check the older syntax
* if necessary.
*/
if (!isset($authHeader)) {
if (isset($_SERVER['Authorization'])) {
$authHeader = $_SERVER['Authorization'];
}
else if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
$authHeader = $_SERVER['HTTP_AUTHORIZATION'];
}
}
if (!isset($authHeader))
return;
$payload = $this->parse_jwt_component($authHeader);
if (is_null($payload))
return;
/**
* We allow the environment to specify whether to perform an issuer check.
*
* For some environments, this will be necessary, but for ADL's P1 deployment
* this doesn't add any extra security.
*/
if ($this->has_env_bool("MOODLE_JWT_CHECK_ISSUER")) {
$issuer = $payload->iss;
$issuerExpected = getenv("MOODLE_JWT_ISSUER");
if ($issuer != $issuerExpected)
return;
}
/**
* Similarly for the token's client value.
*
* For Client, this is a bit less obvious as these can be auto-generated by the
* deployment environment and should be provided by the Ops / Hosting team.
*/
if ($this->has_env_bool("MOODLE_JWT_CHECK_CLIENT")) {
$client = $payload->azp;
$clientExpected = getenv("MOODLE_JWT_CLIENT_ID");
if ($client != $clientExpected)
return;
}
$userExists = $DB->record_exists('user', ["email" => $payload->email]);
if (!$userExists) {
$username = $this->get_expected_username($payload);
$password = null;
/**
* As of Moodle 4.3, created user passwords can no longer be null.
*
* Since this auth method does not allow manual logins anyway, the
* approach will be to simply create a pseudo-randomized password
* for this account, which will be blocked from manual entry anyway.
*/
if ($this->has_env_bool("MOODLE_JWT_ASSIGN_RANDOM_PASSWORD")) {
/**
* The "salt" here will simply be a character block to satisfy password reqs.
*
* There are several fairly random properties to choose from, but we will leave
* the specification to the configuration folks. If not specified, then we will
* use JWT-standard properties in their place.
*/
$requirementSalt = "aA_12345678";
$envPropertyFirst = getenv("MOODLE_JWT_ASSIGN_RANDOM_PASSWORD_PROPERTY_FIRST");
$envPropertySecond = getenv("MOODLE_JWT_ASSIGN_RANDOM_PASSWORD_PROPERTY_SECOND");
$firstChunk = $payload->sub;
$secondChunk = $payload->iss;
if ($envPropertyFirst != false) {
if (property_exists($payload, $envPropertyFirst)) {
$firstChunk = $payload->$envPropertyFirst;
}
}
if ($envPropertySecond != false) {
if (property_exists($payload, $envPropertySecond)) {
$secondChunk = $payload->$envPropertySecond;
}
}
$password = time() . $firstChunk . $secondChunk . $requirementSalt;
}
$user = create_user_record($username, $password, "jwt");
$user->email = $payload->email;
$user->firstname = $payload->given_name;
$user->lastname = $payload->family_name;
$user->mnethostid = $CFG->mnet_localhost_id;
$user->confirmed = true;
$user->policyagreed = true;
$DB->update_record("user", $user);
}
else {
$existingUser = $DB->get_record("user", ["email" => $payload->email]);
$expectedUsername = $this->get_expected_username($payload);
if ($existingUser->username != $expectedUsername) {
$existingUser->username = $expectedUsername;
$DB->update_record("user", $existingUser);
}
}
$updatedUser = $DB->get_record("user", ["email" => $payload->email]);
/**
* This call will automatically complete the user's login process,
* so if that doesn't happen then something else failed above.
*/
complete_user_login($updatedUser);
}
/**
* Use the information provided in the cert + environment variables to determine
* the expected username for this account.
*
* If nothing is set to manipulate this, it will return the 'preferred_username'
* property with no modifications.
*/
private function get_expected_username($cert) {
$envEDIPIProperty = getenv("MOODLE_JWT_EDIPI_PROPERTY");
$useEDIPI = $this->has_env_bool("MOODLE_JWT_USE_EDIPI");
$configuredForEDIPI = $envEDIPIProperty != false;
if ($useEDIPI && $configuredForEDIPI) {
$edipiNumber = $this->get_edipi_number($cert, $envEDIPIProperty);
$foundEDIPI = !is_null($edipiNumber);
if ($foundEDIPI) {
return $edipiNumber;
}
}
$envCustomProperty = getenv("MOODLE_JWT_USERNAME_PROPERTY");
$useCustomProperty = $envCustomProperty != false;
if ($useCustomProperty) {
$hasCustomProperty = property_exists($cert, $envCustomProperty);
if ($hasCustomProperty) {
return $cert->$envCustomProperty;
}
}
return $this->get_default_username($cert);
}
/**
* Parses a given property of the cert for an EDIPI number.
*
* This will return the default username
*/
private function get_edipi_number($cert, $edipiProperty) {
$certHasProperty = property_exists($cert, $edipiProperty);
if ($certHasProperty == false)
return null;
$edipiRaw = $cert->$edipiProperty;
$edipiParsedAsString = is_string($edipiRaw);
if ($edipiParsedAsString == false)
return null;
$edipiParts = explode(".", $edipiRaw);
$edipiLastPart = end($edipiParts);
$edipiNumber = preg_replace("/[^0-9]/", "", $edipiLastPart);
$hasCorrectLength = strlen($edipiNumber) == 10;
if ($hasCorrectLength) {
return $edipiNumber;
}
return null;
}
/**
* Optionally use a different property for the username, controlled through
* an environment variable.
*
* Ignored if not set, will use the standard 'preferred_username' instead.
*/
private function get_default_username($cert) {
return $cert->preferred_username;
}
private function parse_jwt_component($authHeader) {
if (strlen($authHeader) < 7)
return null;
$authtoken = trim(substr($authHeader, 7));
$token_parts = explode('.', $authtoken);
if (count($token_parts) != 3)
return null;
$headerEncoded = $token_parts[0];
$payloadEncoded = $token_parts[1];
$signatureEncoded = $token_parts[2];
$decodedStr = $this->decode_base_64($payloadEncoded);
$jsonObj = json_decode($decodedStr);
return $jsonObj;
}
private function decode_base_64($encodedStr) {
$a = str_replace('-','+', $encodedStr);
$b = str_replace('_', '/', $a);
return base64_decode($b);
}
private function has_env_bool($variableName) {
$value = getenv($variableName);
$exists = $value != false;
return $exists && (strcasecmp($value, "true") == 0);
}
/**
* Unused atm.
*/
private function ensure_user_is_site_admin($user) {
global $DB;
$adminRecord = $DB->get_record('config', ["name" => "siteadmins"]);
$siteAdmins = explode(",", $adminRecord->value);
$alreadyAdmin = in_array(strval($user->id), $siteAdmins);
if ($alreadyAdmin)
return;
array_push($siteAdmins, $user->id);
$adminRecord->value = implode(",", $siteAdmins);
$DB->update_record("config", $adminRecord);
}
/**
* Returns true if the username and password work or don't exist and false
* if the user exists and the password is wrong.
*
* @param string $username The username
* @param string $password The password
* @return bool Authentication success or failure.
*/
function user_login ($username, $password) {
// global $DB;
// return $DB->record_exists('user', [
// "username" => $username,
// "auth" => "jwt"
// ]);
return false;
}
/**
* Updates the user's password.
*
* called when the user password is updated.
*
* @param object $user User table object
* @param string $newpassword Plaintext password
* @return boolean result
*
*/
function user_update_password($user, $newpassword) {
$user = get_complete_user_data('id', $user->id);
// This will also update the stored hash to the latest algorithm
// if the existing hash is using an out-of-date algorithm (or the
// legacy md5 algorithm).
return update_internal_user_password($user, $newpassword);
}
function prevent_local_passwords() {
return false;
}
/**
* Returns true if this authentication plugin is 'internal'.
*
* @return bool
*/
function is_internal() {
return true;
}
/**
* Returns true if this authentication plugin can change the user's
* password.
*
* @return bool
*/
function can_change_password() {
return true;
}
/**
* Returns the URL for changing the user's pw, or empty if the default can
* be used.
*
* @return moodle_url
*/
function change_password_url() {
return null;
}
/**
* Returns true if plugin allows resetting of internal password.
*
* @return bool
*/
function can_reset_password() {
return true;
}
/**
* Returns true if plugin can be manually set.
*
* @return bool
*/
function can_be_manually_set() {
return true;
}
}