@@ -74,8 +74,7 @@ public function loginpage_hook() {
74
74
private function attempt_jwt_login () {
75
75
global $ CFG , $ DB ;
76
76
77
- $ authtoken = null ;
78
- $ authtokenRaw = null ;
77
+ $ authHeader = null ;
79
78
80
79
/**
81
80
* Most deployments will be through Apache, at least for ADL, so
@@ -84,7 +83,7 @@ private function attempt_jwt_login() {
84
83
if (function_exists ('apache_request_headers ' )) {
85
84
$ headers = apache_request_headers ();
86
85
if (isset ($ headers ['Authorization ' ])) {
87
- $ authtokenRaw = $ headers ['Authorization ' ];
86
+ $ authHeader = $ headers ['Authorization ' ];
88
87
}
89
88
}
90
89
@@ -93,26 +92,21 @@ private function attempt_jwt_login() {
93
92
* will miss the previous check, so we can also check the older syntax
94
93
* if necessary.
95
94
*/
96
- if (!isset ($ authtokenRaw )) {
95
+ if (!isset ($ authHeader )) {
97
96
if (isset ($ _SERVER ['Authorization ' ])) {
98
- $ authtokenRaw = $ _SERVER ['Authorization ' ];
97
+ $ authHeader = $ _SERVER ['Authorization ' ];
99
98
}
100
99
else if (isset ($ _SERVER ['HTTP_AUTHORIZATION ' ])) {
101
- $ authtokenRaw = $ _SERVER ['HTTP_AUTHORIZATION ' ];
100
+ $ authHeader = $ _SERVER ['HTTP_AUTHORIZATION ' ];
102
101
}
103
102
}
104
103
105
- if (!isset ($ authtokenRaw ))
104
+ if (!isset ($ authHeader ))
106
105
return ;
107
106
108
- $ authtoken = trim (substr ($ authtokenRaw , 7 ));
109
- $ token_parts = explode ('. ' , $ authtoken );
110
-
111
- $ headerEncoded = $ token_parts [0 ];
112
- $ payloadEncoded = $ token_parts [1 ];
113
- $ signatureEncoded = $ token_parts [2 ];
114
-
115
- $ payload = $ this ->parse_jwt_component ($ payloadEncoded );
107
+ $ payload = $ this ->parse_jwt_component ($ authHeader );
108
+ if (is_null ($ payload ))
109
+ return ;
116
110
117
111
/**
118
112
* We allow the environment to specify whether to perform an issuer check.
@@ -233,8 +227,8 @@ private function attempt_jwt_login() {
233
227
private function get_expected_username ($ cert ) {
234
228
235
229
$ envEDIPIProperty = getenv ("MOODLE_JWT_EDIPI_PROPERTY " );
236
-
237
230
$ useEDIPI = $ this ->has_env_bool ("MOODLE_JWT_USE_EDIPI " );
231
+
238
232
$ configuredForEDIPI = $ envEDIPIProperty != false ;
239
233
240
234
if ($ useEDIPI && $ configuredForEDIPI ) {
@@ -247,14 +241,17 @@ private function get_expected_username($cert) {
247
241
}
248
242
249
243
$ envCustomProperty = getenv ("MOODLE_JWT_USERNAME_PROPERTY " );
250
-
251
244
$ useCustomProperty = $ envCustomProperty != false ;
252
- $ hasCustomProperty = property_exists ($ cert , $ envCustomProperty );
253
-
254
- if ($ useCustomProperty && $ hasCustomProperty ) {
255
- return $ cert ->$ envCustomProperty ;
245
+
246
+ if ($ useCustomProperty ) {
247
+
248
+ $ hasCustomProperty = property_exists ($ cert , $ envCustomProperty );
249
+ if ($ hasCustomProperty ) {
250
+ return $ cert ->$ envCustomProperty ;
251
+ }
256
252
}
257
253
254
+
258
255
return $ this ->get_default_username ($ cert );
259
256
}
260
257
@@ -298,11 +295,24 @@ private function get_default_username($cert) {
298
295
return $ cert ->preferred_username ;
299
296
}
300
297
301
- private function parse_jwt_component ($ encodedStr ) {
298
+ private function parse_jwt_component ($ authHeader ) {
302
299
303
- $ decodedStr = $ this ->decode_base_64 ($ encodedStr );
304
- $ jsonObj = json_decode ($ decodedStr );
300
+ if (strlen ($ authHeader ) < 7 )
301
+ return null ;
302
+
303
+ $ authtoken = trim (substr ($ authHeader , 7 ));
304
+ $ token_parts = explode ('. ' , $ authtoken );
305
305
306
+ if (count ($ token_parts ) != 3 )
307
+ return null ;
308
+
309
+ $ headerEncoded = $ token_parts [0 ];
310
+ $ payloadEncoded = $ token_parts [1 ];
311
+ $ signatureEncoded = $ token_parts [2 ];
312
+
313
+ $ decodedStr = $ this ->decode_base_64 ($ payloadEncoded );
314
+ $ jsonObj = json_decode ($ decodedStr );
315
+
306
316
return $ jsonObj ;
307
317
}
308
318
0 commit comments