<?php
namespace Packback\Lti1p3;
use Firebase\JWT\JWT;
use Packback\Lti1p3\Interfaces\ILtiRegistration;
class LtiDeepLink
{
private $registration;
private $deployment_id;
private $deep_link_settings;
public function __construct(ILtiRegistration $registration, string $deployment_id, array $deep_link_settings)
{
$this->registration = $registration;
$this->deployment_id = $deployment_id;
$this->deep_link_settings = $deep_link_settings;
}
public function getResponseJwt($resources)
{
$message_jwt = [
'iss' => $this->registration->getClientId(),
'aud' => [$this->registration->getIssuer()],
'exp' => time() + 600,
'iat' => time(),
'nonce' => LtiOidcLogin::secureRandomString('nonce-'),
LtiConstants::DEPLOYMENT_ID => $this->deployment_id,
< LtiConstants::MESSAGE_TYPE => 'LtiDeepLinkingResponse',
> LtiConstants::MESSAGE_TYPE => LtiConstants::MESSAGE_TYPE_DEEPLINK_RESPONSE,
LtiConstants::VERSION => LtiConstants::V1_3,
< LtiConstants::DL_CONTENT_ITEMS => array_map(function ($resource) { return $resource->toArray(); }, $resources),
> LtiConstants::DL_CONTENT_ITEMS => array_map(function ($resource) {
> return $resource->toArray();
> }, $resources),
];
// https://www.imsglobal.org/spec/lti-dl/v2p0/#deep-linking-request-message
// 'data' is an optional property which, if it exists, must be returned by the tool
if (isset($this->deep_link_settings['data'])) {
$message_jwt[LtiConstants::DL_DATA] = $this->deep_link_settings['data'];
}
return JWT::encode($message_jwt, $this->registration->getToolPrivateKey(), 'RS256', $this->registration->getKid());
}
> /**
public function outputResponseForm($resources)
> * This method builds an auto-submitting HTML form to post the deep linking response message
{
> * back to platform, as per LTI-DL 2.0 specification. The resulting HTML is then written to standard output,
$jwt = $this->getResponseJwt($resources);
> * so calling this method will automatically send an HTTP response to conclude the content selection flow.
/*
> *
* @todo Fix this
> * @param LtiDeepLinkResource[] $resources The list of selected resources to be sent to the platform
*/ ?>
> *
<form id="auto_submit" action="<?php echo $this->deep_link_settings['deep_link_return_url']; ?>" method="POST">
> * @todo Consider wrapping the content inside a well-formed HTML document,
<input type="hidden" name="JWT" value="<?php echo $jwt; ?>" />
> * and returning it instead of directly writing to standard output
<input type="submit" name="Go" />
> */
< /*
< * @todo Fix this
< */ ?>
< <form id="auto_submit" action="<?php echo $this->deep_link_settings['deep_link_return_url']; ?>" method="POST">
< <input type="hidden" name="JWT" value="<?php echo $jwt; ?>" />
> $formActionUrl = $this->deep_link_settings['deep_link_return_url'];
>
> echo <<<HTML
> <form id="auto_submit" action="{$formActionUrl}" method="POST">
> <input type="hidden" name="JWT" value="{$jwt}" />
< <script>
< document.getElementById('auto_submit').submit();
< </script>
< <?php
> <script>document.getElementById('auto_submit').submit();</script>
> HTML;