/** * Override default Claim validation. * * Uses reference time of the validation context as a constraint. * * @see JWX\JWT\Claim\Claim::validateWithContext * @param ValidationContext $ctx * @return bool */ public function validateWithContext(ValidationContext $ctx) { if ($ctx->hasReferenceTime()) { // try to validate with leeway added if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { return true; } // try to validate with leeway substracted if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { return true; } return false; } return true; }
/** * Check whether a claims set is valid in the given context. * * @param ValidationContext $ctx Validation context * @return bool */ public function isValid(ValidationContext $ctx) { try { $ctx->validate($this); } catch (\RuntimeException $e) { return false; } return true; }
/** * Get validated payload from an encrypted JWE. * * @param JWE $jwe JWE * @param ValidationContext $ctx Validation context * @throws ValidationException If decryption fails * @return string */ private static function _validatedPayloadFromJWE(JWE $jwe, ValidationContext $ctx) { try { $keys = $ctx->keys(); // explicitly defined key if (1 == count($keys)) { return $jwe->decryptWithJWK($keys->first()); } return $jwe->decryptWithJWKSet($keys); } catch (\RuntimeException $e) { throw new ValidationException("JWE validation failed.", null, $e); } }
/** * Validate the claim in a given context. * * @param ValidationContext $ctx * @return bool True if claim is valid */ public function validateWithContext(ValidationContext $ctx) { // if validator has no constraint for the claim if (!$ctx->hasConstraint($this->_name)) { return true; } $constraint = $ctx->constraint($this->_name); // if validation context has an explicitly // defined validator for the claim if ($ctx->hasValidator($this->_name)) { return $ctx->validator($this->_name)->validate($this->_value, $constraint); } // validate using claim's default validator return $this->validate($ctx->constraint($this->_name)); }