1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Packback\Lti1p3;
|
|
|
4 |
|
|
|
5 |
use Firebase\JWT\JWT;
|
|
|
6 |
use Packback\Lti1p3\Interfaces\ILtiRegistration;
|
|
|
7 |
|
|
|
8 |
class LtiDeepLink
|
|
|
9 |
{
|
|
|
10 |
public function __construct(
|
|
|
11 |
private ILtiRegistration $registration,
|
|
|
12 |
private string $deployment_id,
|
|
|
13 |
private array $deep_link_settings
|
|
|
14 |
) {
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
public function getResponseJwt(array $resources): string
|
|
|
18 |
{
|
|
|
19 |
$message_jwt = [
|
|
|
20 |
'iss' => $this->registration->getClientId(),
|
|
|
21 |
'aud' => [$this->registration->getIssuer()],
|
|
|
22 |
'exp' => time() + 600,
|
|
|
23 |
'iat' => time(),
|
|
|
24 |
'nonce' => LtiOidcLogin::secureRandomString('nonce-'),
|
|
|
25 |
LtiConstants::DEPLOYMENT_ID => $this->deployment_id,
|
|
|
26 |
LtiConstants::MESSAGE_TYPE => LtiConstants::MESSAGE_TYPE_DEEPLINK_RESPONSE,
|
|
|
27 |
LtiConstants::VERSION => LtiConstants::V1_3,
|
|
|
28 |
LtiConstants::DL_CONTENT_ITEMS => array_map(function ($resource) {
|
|
|
29 |
return $resource->toArray();
|
|
|
30 |
}, $resources),
|
|
|
31 |
];
|
|
|
32 |
|
|
|
33 |
// https://www.imsglobal.org/spec/lti-dl/v2p0/#deep-linking-request-message
|
|
|
34 |
// 'data' is an optional property which, if it exists, must be returned by the tool
|
|
|
35 |
if (isset($this->deep_link_settings['data'])) {
|
|
|
36 |
$message_jwt[LtiConstants::DL_DATA] = $this->deep_link_settings['data'];
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
return JWT::encode($message_jwt, $this->registration->getToolPrivateKey(), 'RS256', $this->registration->getKid());
|
|
|
40 |
}
|
|
|
41 |
}
|