Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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) { return $resource->toArray(); }, $resources),
  33          ];
  34  
  35          // https://www.imsglobal.org/spec/lti-dl/v2p0/#deep-linking-request-message
  36          // 'data' is an optional property which, if it exists, must be returned by the tool
  37          if (isset($this->deep_link_settings['data'])) {
  38              $message_jwt[LtiConstants::DL_DATA] = $this->deep_link_settings['data'];
  39          }
  40  
  41          return JWT::encode($message_jwt, $this->registration->getToolPrivateKey(), 'RS256', $this->registration->getKid());
  42      }
  43  
  44      public function outputResponseForm($resources)
  45      {
  46          $jwt = $this->getResponseJwt($resources);
  47          /*
  48           * @todo Fix this
  49           */ ?>
  50          <form id="auto_submit" action="<?php echo $this->deep_link_settings['deep_link_return_url']; ?>" method="POST">
  51              <input type="hidden" name="JWT" value="<?php echo $jwt; ?>" />
  52              <input type="submit" name="Go" />
  53          </form>
  54          <script>
  55              document.getElementById('auto_submit').submit();
  56          </script>
  57          <?php
  58      }
  59  }