Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  namespace Packback\Lti1p3\MessageValidators;
   4  
   5  use Packback\Lti1p3\Interfaces\IMessageValidator;
   6  use Packback\Lti1p3\LtiConstants;
   7  use Packback\Lti1p3\LtiException;
   8  
   9  abstract class AbstractMessageValidator implements IMessageValidator
  10  {
  11      abstract public static function getMessageType(): string;
  12  
  13      public static function canValidate(array $jwtBody): bool
  14      {
  15          return $jwtBody[LtiConstants::MESSAGE_TYPE] === static::getMessageType();
  16      }
  17  
  18      abstract public static function validate(array $jwtBody): void;
  19  
  20      public static function validateGenericMessage(array $jwtBody): void
  21      {
  22          if (empty($jwtBody['sub'])) {
  23              throw new LtiException('Must have a user (sub)');
  24          }
  25          if (!isset($jwtBody[LtiConstants::VERSION])) {
  26              throw new LtiException('Missing LTI Version');
  27          }
  28          if ($jwtBody[LtiConstants::VERSION] !== LtiConstants::V1_3) {
  29              throw new LtiException('Incorrect version, expected 1.3.0');
  30          }
  31          if (!isset($jwtBody[LtiConstants::ROLES])) {
  32              throw new LtiException('Missing Roles Claim');
  33          }
  34      }
  35  }