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

Change how the OIDC Login URL is used, mark the Redirect for deprecation #116

Merged
merged 7 commits into from
Dec 15, 2023
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
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be overkill but shouldn't a deprication warning be also be shown for getRedirectUrl?

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