Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions resources/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use UnityWebPortal\lib\UnityRedis;
use UnityWebPortal\lib\UnityWebhook;
use UnityWebPortal\lib\UnityGithub;
use UnityWebPortal\lib\UnitySite;
use UnityWebPortal\lib\exceptions\SSOException;

//
// Initialize Session
Expand Down Expand Up @@ -92,9 +94,15 @@
// SSO Init
//

$SSO = UnitySSO::getSSO();
if (!is_null($SSO)) {
// SSO is available
if (isset($_SERVER["REMOTE_USER"])) { // Check if SSO is enabled on this page
try {
$SSO = UnitySSO::getSSO();
} catch (SSOException $e) {
$errorid = uniqid("sso-");
$eppn = $_SERVER["REMOTE_USER"];
UnitySite::errorLog("SSO Failure", "{$e} ($errorid)");
UnitySite::die("Invalid eppn: '$eppn'. Please contact {$CONFIG["mail"]["support"]} (id: $errorid)", true);
}
$_SESSION["SSO"] = $SSO;

$OPERATOR = new UnityUser($SSO["user"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
Expand Down
25 changes: 10 additions & 15 deletions resources/lib/UnitySSO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace UnityWebPortal\lib;

use Exception;
use UnityWebPortal\lib\exceptions\SSOException;

class UnitySSO
{
Expand All @@ -17,7 +18,7 @@ private static function eppnToOrg($eppn)
{
$parts = explode("@", $eppn);
if (count($parts) != 2) {
throw new Exception("Malformed remote user detected: '$eppn'");
throw new SSOException("Malformed remote user detected: '$eppn'");
}

$org = $parts[1];
Expand All @@ -27,19 +28,13 @@ private static function eppnToOrg($eppn)

public static function getSSO()
{
if (isset($_SERVER["REMOTE_USER"])) { // Check if SSO is enabled on this page
$SSO = array(
"user" => self::eppnToUID($_SERVER["REMOTE_USER"]),
"org" => self::eppnToOrg($_SERVER["REMOTE_USER"]),
"firstname" => $_SERVER["givenName"],
"lastname" => $_SERVER["sn"],
"name" => $_SERVER["givenName"] . " " . $_SERVER["sn"],
"mail" => isset($_SERVER["mail"]) ? $_SERVER["mail"] : $_SERVER["eppn"]
);

return $SSO;
}

return null;
return array(
"user" => self::eppnToUID($_SERVER["REMOTE_USER"]),
"org" => self::eppnToOrg($_SERVER["REMOTE_USER"]),
"firstname" => $_SERVER["givenName"],
"lastname" => $_SERVER["sn"],
"name" => $_SERVER["givenName"] . " " . $_SERVER["sn"],
"mail" => isset($_SERVER["mail"]) ? $_SERVER["mail"] : $_SERVER["eppn"]
);
}
}
6 changes: 6 additions & 0 deletions resources/lib/exceptions/SSOException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace UnityWebPortal\lib\exceptions;

class SSOException extends \Exception
{
}
48 changes: 48 additions & 0 deletions test/functional/InvalidEPPNTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use UnityWebPortal\lib\exceptions\PhpUnitNoDieException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class InvalidEPPNTest extends TestCase
{
public static function provider()
{
return [
["", false],
["a", false],
["a@b", true],
["a@b@c", false],
];
}

#[DataProvider("provider")]
public function testInitGetSSO(string $eppn, bool $is_valid): void
{
global $SSO;
$original_server = $_SERVER;
$original_sso = $SSO;
if (session_status() == PHP_SESSION_ACTIVE) {
session_write_close();
session_id(uniqid());
}
if (!$is_valid) {
$this->expectException(PhpUnitNoDieException::class);
$this->expectExceptionMessageMatches("/.*Invalid eppn.*/");
}
try {
$_SERVER["REMOTE_USER"] = $eppn;
$_SERVER["REMOTE_ADDR"] = "127.0.0.1";
$_SERVER["eppn"] = $eppn;
$_SERVER["givenName"] = "foo";
$_SERVER["sn"] = "bar";
// can't use http_get because it does `require_once`
// won't use phpunit --process-isolation because when I try that argument all tests fail with a blank error message
include __DIR__ . "/../../resources/init.php";
} finally {
$_SERVER = $original_server;
$SSO = $original_sso;
}
$this->assertTrue(true); // if $is_valid, there are no other assertions
}
}
1 change: 1 addition & 0 deletions test/phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require_once __DIR__ . "/../resources/lib/UnityRedis.php";
require_once __DIR__ . "/../resources/lib/UnityGithub.php";
require_once __DIR__ . "/../resources/lib/exceptions/PhpUnitNoDieException.php";
require_once __DIR__ . "/../resources/lib/exceptions/SSOException.php";

$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] = true;

Expand Down