Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  namespace Packback\Lti1p3;
   4  
   5  use Firebase\JWT\JWT;
   6  use Packback\Lti1p3\Interfaces\IDatabase;
   7  use Packback\Lti1p3\Interfaces\ILtiRegistration;
   8  
   9  class JwksEndpoint
  10  {
  11      private $keys;
  12  
  13      public function __construct(array $keys)
  14      {
  15          $this->keys = $keys;
  16      }
  17  
  18      public static function new(array $keys)
  19      {
  20          return new JwksEndpoint($keys);
  21      }
  22  
  23      public static function fromIssuer(IDatabase $database, $issuer)
  24      {
  25          $registration = $database->findRegistrationByIssuer($issuer);
  26  
  27          return new JwksEndpoint([$registration->getKid() => $registration->getToolPrivateKey()]);
  28      }
  29  
  30      public static function fromRegistration(ILtiRegistration $registration)
  31      {
  32          return new JwksEndpoint([$registration->getKid() => $registration->getToolPrivateKey()]);
  33      }
  34  
  35      public function getPublicJwks()
  36      {
  37          $jwks = [];
  38          foreach ($this->keys as $kid => $private_key) {
  39              $key_res = openssl_pkey_get_private($private_key);
  40              $key_details = openssl_pkey_get_details($key_res);
  41              $components = [
  42                  'kty' => 'RSA',
  43                  'alg' => 'RS256',
  44                  'use' => 'sig',
  45                  'e' => JWT::urlsafeB64Encode($key_details['rsa']['e']),
  46                  'n' => JWT::urlsafeB64Encode($key_details['rsa']['n']),
  47                  'kid' => $kid,
  48              ];
  49              $jwks[] = $components;
  50          }
  51  
  52          return ['keys' => $jwks];
  53      }
  54  
  55      public function outputJwks()
  56      {
  57          echo json_encode($this->getPublicJwks());
  58      }
  59  }