Skip to content

Commit

Permalink
Merge pull request #116 from packbackbooks/oidc-login-url
Browse files Browse the repository at this point in the history
Change how the OIDC Login URL is used, mark the Redirect for deprecation
  • Loading branch information
dbhynds authored Dec 15, 2023
2 parents 68a1f6a + 5a4b2ed commit 70743a4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/LtiOidcLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class LtiOidcLogin
public const ERROR_MSG_LAUNCH_URL = 'No launch URL configured';
public const ERROR_MSG_ISSUER = 'Could not find issuer';
public const ERROR_MSG_LOGIN_HINT = 'Could not find login hint';

/**
* @todo Type these in v6
*/
private $db;
private $cache;
private $cookie;
Expand All @@ -26,6 +30,9 @@ class LtiOidcLogin
*/
public function __construct(IDatabase $database, ?ICache $cache = null, ?ICookie $cookie = null)
{
/**
* @todo Make these arguments not nullable in v6
*/
$this->db = $database;
$this->cache = $cache;
$this->cookie = $cookie;
Expand All @@ -40,15 +47,12 @@ public static function new(IDatabase $database, ?ICache $cache = null, ?ICookie
}

/**
* Calculate the redirect location to return to based on an OIDC third party initiated login request.
*
* @param string $launchUrl URL to redirect back to after the OIDC login. This URL must match exactly a URL white listed in the platform.
* @param array $request An array of request parameters. If not set will default to $_REQUEST.
* @return Redirect returns a redirect object containing the fully formed OIDC login URL
* @deprecated Use getRedirectUrl() to get the URL and then redirect to it yourself. Will be removed in v6.0
*/
public function doOidcLoginRedirect($launchUrl, ?array $request = null)
{
// @todo remove this in v6.0
trigger_error('Method '.__METHOD__.' is deprecated', E_USER_DEPRECATED);

if ($request === null) {
$request = $_REQUEST;
}
Expand All @@ -57,6 +61,21 @@ public function doOidcLoginRedirect($launchUrl, ?array $request = null)
throw new OidcException(static::ERROR_MSG_LAUNCH_URL, 1);
}

$authLoginReturnUrl = $this->getRedirectUrl($launchUrl, $request);

// Return auth redirect.
return new Redirect($authLoginReturnUrl);
}

/**
* Calculate the redirect location to return to based on an OIDC third party initiated login request.
*
* @param string $launchUrl URL to redirect back to after the OIDC login. This URL must match exactly a URL white listed in the platform.
* @param array $request An array of request parameters.
* @return string returns the fully formed OIDC login URL
*/
public function getRedirectUrl(string $launchUrl, array $request): string
{
// Validate Request Data.
$registration = $this->validateOidcLogin($request);

Expand Down Expand Up @@ -92,10 +111,7 @@ public function doOidcLoginRedirect($launchUrl, ?array $request = null)
$authParams['lti_message_hint'] = $request['lti_message_hint'];
}

$authLoginReturnUrl = Helpers::buildUrlWithQueryParams($registration->getAuthLoginUrl(), $authParams);

// Return auth redirect.
return new Redirect($authLoginReturnUrl);
return Helpers::buildUrlWithQueryParams($registration->getAuthLoginUrl(), $authParams);
}

public function validateOidcLogin($request)
Expand Down
12 changes: 12 additions & 0 deletions src/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Packback\Lti1p3\Interfaces\ICookie;

/**
* @deprecated Use LtiOidcLogin::getRedirectUrl() to get the URL and then redirect to it yourself
*/
class Redirect
{
private $location;
Expand All @@ -16,8 +19,12 @@ public function __construct(string $location, ?string $referer_query = null)
$this->referer_query = $referer_query;
}

/**
* @deprecated
*/
public function doRedirect()
{
trigger_error('Method '.__METHOD__.' is deprecated', E_USER_DEPRECATED);
header('Location: '.$this->location, true, 302);
exit;
}
Expand All @@ -35,8 +42,13 @@ public function doHybridRedirect(ICookie $cookie)
$this->doJsRedirect();
}

/**
* @deprecated
*/
public function getRedirectUrl()
{
trigger_error('Method '.__METHOD__.' is deprecated', E_USER_DEPRECATED);

return $this->location;
}

Expand Down

0 comments on commit 70743a4

Please sign in to comment.