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.
   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  namespace enrol_lti\local\ltiadvantage\lib;
  18  
  19  use Packback\Lti1p3\Interfaces\ICache;
  20  
  21  /**
  22   * The launch_cache_session, providing a temporary session store for launch information.
  23   *
  24   * This is used to store the launch information while the user is transitioned through the Moodle authentication flows
  25   * and back to the deep linking launch handler (launch_deeplink.php).
  26   *
  27   * @package    enrol_lti
  28   * @copyright  2021 Jake Dallimore <jrhdallimore@gmail.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class launch_cache_session implements ICache {
  32  
  33      /**
  34       * Get the launch data from the cache.
  35       *
  36       * @param string $key the launch id.
  37       * @return array|null the launch data.
  38       */
  39      public function getLaunchData($key): ?array {
  40          global $SESSION;
  41          if (isset($SESSION->enrol_lti_launch[$key])) {
  42              return unserialize($SESSION->enrol_lti_launch[$key]);
  43          }
  44          return null;
  45      }
  46  
  47      /**
  48       * Add launch data to the cache.
  49       *
  50       * @param string $key the launch id.
  51       * @param array $jwtBody the launch data.
  52       */
  53      public function cacheLaunchData(string $key, array $jwtBody): void {
  54          global $SESSION;
  55          $SESSION->enrol_lti_launch[$key] = serialize($jwtBody);
  56      }
  57  
  58      /**
  59       * Cache the nonce.
  60       *
  61       * @param string $nonce the nonce.
  62       * @param string $state the state.
  63       */
  64      public function cacheNonce(string $nonce, string $state): void {
  65          global $SESSION;
  66          $SESSION->enrol_lti_launch_nonce[$nonce] = $state;
  67      }
  68  
  69      /**
  70       * Check whether the cache contains the nonce.
  71       *
  72       * @param string $nonce the nonce
  73       * @param string $state the state
  74       * @return bool true if found, false otherwise.
  75       */
  76      public function checkNonceIsValid(string $nonce, string $state): bool {
  77          global $SESSION;
  78          return isset($SESSION->enrol_lti_launch_nonce[$nonce]) && $SESSION->enrol_lti_launch_nonce[$nonce] == $state;
  79      }
  80  
  81      /**
  82       * Delete all data from the session cache.
  83       */
  84      public function purge() {
  85          global $SESSION;
  86          unset($SESSION->enrol_lti_launch);
  87      }
  88  
  89      /**
  90       * Cache the access token.
  91       *
  92       * @param string $key the key
  93       * @param string $accessToken the access token
  94       */
  95      public function cacheAccessToken(string $key, string $accessToken): void {
  96          global $SESSION;
  97          $SESSION->enrol_lti_launch_token[$key] = $accessToken;
  98      }
  99  
 100      /**
 101       * Get a cached access token.
 102       *
 103       * @param string $key the key to check.
 104       * @return string|null the token string, or null if not found.
 105       */
 106      public function getAccessToken(string $key): ?string {
 107          global $SESSION;
 108          return $SESSION->enrol_lti_launch_token[$key] ?? null;
 109      }
 110  
 111      /**
 112       * Clear the access token from the cache.
 113       *
 114       * @param string $key the key to purge.
 115       */
 116      public function clearAccessToken(string $key): void {
 117          global $SESSION;
 118          unset($SESSION->enrol_lti_launch_token[$key]);
 119      }
 120  }