Skip to content

Commit

Permalink
updating env checks
Browse files Browse the repository at this point in the history
  • Loading branch information
adl-trey committed Jan 29, 2024
1 parent 5431efe commit 74f3334
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ private function attempt_jwt_login() {
* For some environments, this will be necessary, but for ADL's P1 deployment
* this doesn't add any extra security.
*/
$checkIssuer = getenv("MOODLE_JWT_CHECK_ISSUER");
if (isset($checkIssuer) && $checkIssuer) {
if ($this->has_env_bool("MOODLE_JWT_CHECK_ISSUER")) {

$issuer = $payload->iss;
$issuerExpected = getenv("MOODLE_JWT_ISSUER");
Expand All @@ -136,8 +135,7 @@ private function attempt_jwt_login() {
* 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.
*/
$checkClient = getenv("MOODLE_JWT_CHECK_CLIENT");
if (isset($checkClient) && $checkClient) {
if ($this->has_env_bool("MOODLE_JWT_CHECK_CLIENT")) {

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

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

if (isset($envPropertyFirst) && $envPropertyFirst) {
$firstChunk = $payload->$envPropertyFirst;
if ($envPropertyFirst != false) {
if (property_exists($payload, $envPropertyFirst)) {
$firstChunk = $payload->$envPropertyFirst;
}
}

if (isset($envPropertySecond) && $envPropertySecond) {
$secondChunk = $payload->$envPropertySecond;
if ($envPropertySecond != false) {
if (property_exists($payload, $envPropertySecond)) {
$secondChunk = $payload->$envPropertySecond;
}
}


Expand Down Expand Up @@ -231,11 +232,10 @@ private function attempt_jwt_login() {
*/
private function get_expected_username($cert) {

$envUseEDIPI = getenv("MOODLE_JWT_USE_EDIPI");
$envEDIPIProperty = getenv("MOODLE_JWT_EDIPI_PROPERTY");

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

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

$envCustomProperty = getenv("MOODLE_JWT_USERNAME_PROPERTY");

$useCustomProperty = isset($envCustomProperty);
$useCustomProperty = $envCustomProperty != false;
$hasCustomProperty = property_exists($cert, $envCustomProperty);

if ($useCustomProperty && $hasCustomProperty) {
Expand Down Expand Up @@ -304,6 +304,13 @@ private function decode_base_64($encodedStr) {
return base64_decode($b);
}

private function has_env_bool($variableName) {
$value = getenv($variableName);
$exists = $value != false;

return $exists && strcasecmp($value, "true");
}

/**
* Unused atm.
*/
Expand Down

0 comments on commit 74f3334

Please sign in to comment.