Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIMS-1068: Move discovery away from constructor, get cookie expiry dynamically #683

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Changes from 1 commit
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
54 changes: 29 additions & 25 deletions api/src/Authentication/Type/OIDC.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,36 @@ class OIDC extends AuthenticationParent implements AuthenticationInterface
{
private $providerConfig = array();

function __construct() {
global $sso_url, $oidc_client_id, $oidc_client_secret;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://' . $sso_url . '/.well-known/openid-configuration');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$newProviderConfig = json_decode($response);

if(!$newProviderConfig
|| !isset($newProviderConfig->userinfo_endpoint)
|| !isset($newProviderConfig->authorization_endpoint)
|| !isset($newProviderConfig->token_endpoint)) {
error_log("OIDC Authentication provider replied with invalid JSON body");
return;
private function getEndpoints() {
if (empty($providerConfig)) {
gfrn marked this conversation as resolved.
Show resolved Hide resolved
global $sso_url, $oidc_client_id, $oidc_client_secret;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://' . $sso_url . '/.well-known/openid-configuration');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$newProviderConfig = json_decode($response);

if(!$newProviderConfig
|| !isset($newProviderConfig->userinfo_endpoint)
|| !isset($newProviderConfig->authorization_endpoint)
|| !isset($newProviderConfig->token_endpoint)) {
error_log("OIDC Authentication provider replied with invalid JSON body");
return;
}
$newProviderConfig->b64ClientCreds = base64_encode(
$oidc_client_id . ":" . $oidc_client_secret
);

$this->providerConfig = $newProviderConfig;
}
$newProviderConfig->b64ClientCreds = base64_encode(
$oidc_client_id . ":" . $oidc_client_secret
);

$this->providerConfig = $newProviderConfig;
}

private function getUser($token)
{
$this->getEndpoints();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->providerConfig->userinfo_endpoint);
curl_setopt($ch, CURLOPT_HEADER, 0);
Expand Down Expand Up @@ -71,6 +74,7 @@ function check()

function authorise()
{
$this->getEndpoints();
global $oidc_client_id;
$redirect_url = Utils::filterParamFromUrl($_SERVER["HTTP_REFERER"], "code");

Expand All @@ -82,6 +86,7 @@ function authorise()

function authenticateByCode($code)
{
$this->getEndpoints();
global $cacert, $oidc_client_secret, $oidc_client_id, $cookie_key;

$redirect_url = Utils::filterParamFromUrl($_SERVER["HTTP_REFERER"], "code");
Expand All @@ -105,14 +110,14 @@ function authenticateByCode($code)
}

$token = $response_json->access_token;

if(!$token) {
error_log("Invalid authentication attempt, provider returned no access token");
return false;
}

$cookieOpts = array (
'expires' => time() + 60*60*24,
'expires' => time() + $response_json->expires_in,
'path' => '/',
'secure' => true,
'httponly' => true,
Expand All @@ -123,4 +128,3 @@ function authenticateByCode($code)
return $this->getUser($token);
}
}

Loading