Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [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 => LtiConstants::MESSAGE_TYPE_DEEPLINK_RESPONSE,
  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      /**
  47       * This method builds an auto-submitting HTML form to post the deep linking response message
  48       * back to platform, as per LTI-DL 2.0 specification. The resulting HTML is then written to standard output,
  49       * so calling this method will automatically send an HTTP response to conclude the content selection flow.
  50       *
  51       * @param  LtiDeepLinkResource[]  $resources The list of selected resources to be sent to the platform
  52       *
  53       * @todo Consider wrapping the content inside a well-formed HTML document,
  54       * and returning it instead of directly writing to standard output
  55       */
  56      public function outputResponseForm($resources)
  57      {
  58          $jwt = $this->getResponseJwt($resources);
  59          $formActionUrl = $this->deep_link_settings['deep_link_return_url'];
  60  
  61          echo <<<HTML
  62  <form id="auto_submit" action="{$formActionUrl}" method="POST">
  63      <input type="hidden" name="JWT" value="{$jwt}" />
  64      <input type="submit" name="Go" />
  65  </form>
  66  <script>document.getElementById('auto_submit').submit();</script>
  67  HTML;
  68      }
  69  }