| 1 | 
           efrain | 
           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 | 
               /**
  | 
        
        
            | 
            | 
           21 | 
                * @throws LtiException
  | 
        
        
            | 
            | 
           22 | 
                */
  | 
        
        
            | 
            | 
           23 | 
               public static function validateGenericMessage(array $jwtBody): void
  | 
        
        
            | 
            | 
           24 | 
               {
  | 
        
        
            | 
            | 
           25 | 
                   if (empty($jwtBody['sub'])) {
  | 
        
        
            | 
            | 
           26 | 
                       throw new LtiException('Must have a user (sub)');
  | 
        
        
            | 
            | 
           27 | 
                   }
  | 
        
        
            | 
            | 
           28 | 
                   if (!isset($jwtBody[LtiConstants::VERSION])) {
  | 
        
        
            | 
            | 
           29 | 
                       throw new LtiException('Missing LTI Version');
  | 
        
        
            | 
            | 
           30 | 
                   }
  | 
        
        
            | 
            | 
           31 | 
                   if ($jwtBody[LtiConstants::VERSION] !== LtiConstants::V1_3) {
  | 
        
        
            | 
            | 
           32 | 
                       throw new LtiException('Incorrect version, expected 1.3.0');
  | 
        
        
            | 
            | 
           33 | 
                   }
  | 
        
        
            | 
            | 
           34 | 
                   if (!isset($jwtBody[LtiConstants::ROLES])) {
  | 
        
        
            | 
            | 
           35 | 
                       throw new LtiException('Missing Roles Claim');
  | 
        
        
            | 
            | 
           36 | 
                   }
  | 
        
        
            | 
            | 
           37 | 
               }
  | 
        
        
            | 
            | 
           38 | 
           }
  |