Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This files exposes functions for LTI 1.3 Key Management.
  19   *
  20   * @package    mod_lti
  21   * @copyright  2020 Claude Vervoort (Cengage)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace mod_lti\local\ltiopenid;
  25  
  26  /**
  27   * This class exposes functions for LTI 1.3 Key Management.
  28   *
  29   * @package    mod_lti
  30   * @copyright  2020 Claude Vervoort (Cengage)
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class jwks_helper {
  34  
  35      /**
  36       * Returns the private key to use to sign outgoing JWT.
  37       *
  38       * @return array keys are kid and key in PEM format.
  39       */
  40      public static function get_private_key() {
  41          $privatekey = get_config('mod_lti', 'privatekey');
  42          $kid = get_config('mod_lti', 'kid');
  43          return [
  44              "key" => $privatekey,
  45              "kid" => $kid
  46          ];
  47      }
  48  
  49      /**
  50       * Returns the JWK Key Set for this site.
  51       * @return array keyset exposting the site public key.
  52       */
  53      public static function get_jwks() {
  54          $jwks = array('keys' => array());
  55  
  56          $privatekey = self::get_private_key();
  57          $res = openssl_pkey_get_private($privatekey['key']);
  58          $details = openssl_pkey_get_details($res);
  59  
  60          $jwk = array();
  61          $jwk['kty'] = 'RSA';
  62          $jwk['alg'] = 'RS256';
  63          $jwk['kid'] = $privatekey['kid'];
  64          $jwk['e'] = rtrim(strtr(base64_encode($details['rsa']['e']), '+/', '-_'), '=');
  65          $jwk['n'] = rtrim(strtr(base64_encode($details['rsa']['n']), '+/', '-_'), '=');
  66          $jwk['use'] = 'sig';
  67  
  68          $jwks['keys'][] = $jwk;
  69          return $jwks;
  70      }
  71  
  72  }