| 1 | <?php |
||
| 12 | trait ReferenceTimeValidation |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Validate the claim against given constraint. |
||
| 16 | * |
||
| 17 | * @param mixed $constraint |
||
| 18 | * |
||
| 19 | * @return bool |
||
| 20 | */ |
||
| 21 | abstract public function validate($constraint): bool; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Override default Claim validation. |
||
| 25 | * |
||
| 26 | * Uses reference time of the validation context as a constraint. |
||
| 27 | * |
||
| 28 | * @see \Sop\JWX\JWT\Claim\Claim::validateWithContext() |
||
| 29 | * |
||
| 30 | * @param ValidationContext $ctx |
||
| 31 | * |
||
| 32 | * @return bool |
||
| 33 | */ |
||
| 34 | 17 | public function validateWithContext(ValidationContext $ctx): bool |
|
| 35 | { |
||
| 36 | 17 | if ($ctx->hasReferenceTime()) { |
|
| 37 | // try to validate with leeway added |
||
| 38 | 11 | if ($this->validate($ctx->referenceTime() + $ctx->leeway())) { |
|
| 39 | 5 | return true; |
|
| 40 | } |
||
| 41 | // try to validate with leeway substracted |
||
| 42 | 10 | if ($this->validate($ctx->referenceTime() - $ctx->leeway())) { |
|
| 43 | 5 | return true; |
|
| 44 | } |
||
| 45 | 5 | return false; |
|
| 46 | } |
||
| 47 | 6 | return true; |
|
| 50 |