Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

   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      private $registration;
  11      private $deployment_id;
  12      private $deep_link_settings;
  13  
  14      public function __construct(ILtiRegistration $registration, string $deployment_id, array $deep_link_settings)
  15      {
  16          $this->registration = $registration;
  17          $this->deployment_id = $deployment_id;
  18          $this->deep_link_settings = $deep_link_settings;
  19      }
  20  
  21      public function getResponseJwt($resources)
  22      {
  23          $message_jwt = [
  24              'iss' => $this->registration->getClientId(),
  25              'aud' => [$this->registration->getIssuer()],
  26              'exp' => time() + 600,
  27              'iat' => time(),
  28              'nonce' => LtiOidcLogin::secureRandomString('nonce-'),
  29              LtiConstants::DEPLOYMENT_ID => $this->deployment_id,
  30              LtiConstants::MESSAGE_TYPE => 'LtiDeepLinkingResponse',
  31              LtiConstants::VERSION => LtiConstants::V1_3,
  32              LtiConstants::DL_CONTENT_ITEMS => array_map(function ($resource) {
  33                  return $resource->toArray();
  34              }, $resources),
  35          ];
  36  
  37          // https://www.imsglobal.org/spec/lti-dl/v2p0/#deep-linking-request-message
  38          // 'data' is an optional property which, if it exists, must be returned by the tool
  39          if (isset($this->deep_link_settings['data'])) {
  40              $message_jwt[LtiConstants::DL_DATA] = $this->deep_link_settings['data'];
  41          }
  42  
  43          return JWT::encode($message_jwt, $this->registration->getToolPrivateKey(), 'RS256', $this->registration->getKid());
  44      }
  45  
  46      public function outputResponseForm($resources)
  47      {
  48          $jwt = $this->getResponseJwt($resources);
  49          /*
  50           * @todo Fix this
  51           */ ?>
  52          <form id="auto_submit" action="<?php echo $this->deep_link_settings['deep_link_return_url']; ?>" method="POST">
  53              <input type="hidden" name="JWT" value="<?php echo $jwt; ?>" />
  54              <input type="submit" name="Go" />
  55          </form>
  56          <script>
  57              document.getElementById('auto_submit').submit();
  58          </script>
  59          <?php
  60      }
  61  }