Skip to content

Commit 74f3334

Browse files
committed
updating env checks
1 parent 5431efe commit 74f3334

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

auth.php

+21-14
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ private function attempt_jwt_login() {
120120
* For some environments, this will be necessary, but for ADL's P1 deployment
121121
* this doesn't add any extra security.
122122
*/
123-
$checkIssuer = getenv("MOODLE_JWT_CHECK_ISSUER");
124-
if (isset($checkIssuer) && $checkIssuer) {
123+
if ($this->has_env_bool("MOODLE_JWT_CHECK_ISSUER")) {
125124

126125
$issuer = $payload->iss;
127126
$issuerExpected = getenv("MOODLE_JWT_ISSUER");
@@ -136,8 +135,7 @@ private function attempt_jwt_login() {
136135
* For Client, this is a bit less obvious as these can be auto-generated by the
137136
* deployment environment and should be provided by the Ops / Hosting team.
138137
*/
139-
$checkClient = getenv("MOODLE_JWT_CHECK_CLIENT");
140-
if (isset($checkClient) && $checkClient) {
138+
if ($this->has_env_bool("MOODLE_JWT_CHECK_CLIENT")) {
141139

142140
$client = $payload->azp;
143141
$clientExpected = getenv("MOODLE_JWT_CLIENT_ID");
@@ -160,8 +158,7 @@ private function attempt_jwt_login() {
160158
* approach will be to simply create a pseudo-randomized password
161159
* for this account, which will be blocked from manual entry anyway.
162160
*/
163-
$assignRandomPassword = getenv("MOODLE_JWT_ASSIGN_RANDOM_PASSWORD");
164-
if (isset($assignRandomPassword) && $assignRandomPassword) {
161+
if ($this->has_env_bool("MOODLE_JWT_ASSIGN_RANDOM_PASSWORD")) {
165162

166163
/**
167164
* The "salt" here will simply be a character block to satisfy password reqs.
@@ -178,12 +175,16 @@ private function attempt_jwt_login() {
178175
$firstChunk = $payload->sub;
179176
$secondChunk = $payload->iss;
180177

181-
if (isset($envPropertyFirst) && $envPropertyFirst) {
182-
$firstChunk = $payload->$envPropertyFirst;
178+
if ($envPropertyFirst != false) {
179+
if (property_exists($payload, $envPropertyFirst)) {
180+
$firstChunk = $payload->$envPropertyFirst;
181+
}
183182
}
184183

185-
if (isset($envPropertySecond) && $envPropertySecond) {
186-
$secondChunk = $payload->$envPropertySecond;
184+
if ($envPropertySecond != false) {
185+
if (property_exists($payload, $envPropertySecond)) {
186+
$secondChunk = $payload->$envPropertySecond;
187+
}
187188
}
188189

189190

@@ -231,11 +232,10 @@ private function attempt_jwt_login() {
231232
*/
232233
private function get_expected_username($cert) {
233234

234-
$envUseEDIPI = getenv("MOODLE_JWT_USE_EDIPI");
235235
$envEDIPIProperty = getenv("MOODLE_JWT_EDIPI_PROPERTY");
236236

237-
$useEDIPI = isset($envUseEDIPI) && strcasecmp($envUseEDIPI, "true");
238-
$configuredForEDIPI = isset($envEDIPIProperty);
237+
$useEDIPI = $this->has_env_bool("MOODLE_JWT_USE_EDIPI");
238+
$configuredForEDIPI = $envEDIPIProperty != false;
239239

240240
if ($useEDIPI && $configuredForEDIPI) {
241241
$edipiNumber = $this->get_edipi_number($cert, $envEDIPIProperty);
@@ -248,7 +248,7 @@ private function get_expected_username($cert) {
248248

249249
$envCustomProperty = getenv("MOODLE_JWT_USERNAME_PROPERTY");
250250

251-
$useCustomProperty = isset($envCustomProperty);
251+
$useCustomProperty = $envCustomProperty != false;
252252
$hasCustomProperty = property_exists($cert, $envCustomProperty);
253253

254254
if ($useCustomProperty && $hasCustomProperty) {
@@ -304,6 +304,13 @@ private function decode_base_64($encodedStr) {
304304
return base64_decode($b);
305305
}
306306

307+
private function has_env_bool($variableName) {
308+
$value = getenv($variableName);
309+
$exists = $value != false;
310+
311+
return $exists && strcasecmp($value, "true");
312+
}
313+
307314
/**
308315
* Unused atm.
309316
*/

0 commit comments

Comments
 (0)