1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
<?php
declare(strict_types=1);
namespace Lcobucci\JWT\Tests\Validation\Constraint;
use DateInterval;
use DateTimeImmutable;
use Lcobucci\Clock\Clock;
use Lcobucci\Clock\FrozenClock;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\Validation\Constraint;
use Lcobucci\JWT\Validation\Constraint\LeewayCannotBeNegative;
use Lcobucci\JWT\Validation\ConstraintViolation;
use PHPUnit\Framework\Attributes as PHPUnit;
abstract class ValidAtTestCase extends ConstraintTestCase
{
protected Clock $clock;
#[PHPUnit\Before]
final public function createDependencies(): void
{
$this->clock = new FrozenClock(new DateTimeImmutable());
}
abstract protected function buildValidAtConstraint(Clock $clock, ?DateInterval $leeway = null): Constraint;
#[PHPUnit\Test]
final public function constructShouldRaiseExceptionOnNegativeLeeway(): void
{
$leeway = new DateInterval('PT30S');
$leeway->invert = 1;
$this->expectException(LeewayCannotBeNegative::class);
$this->expectExceptionMessage('Leeway cannot be negative');
$this->buildValidAtConstraint($this->clock, $leeway);
}
#[PHPUnit\Test]
final public function assertShouldRaiseExceptionWhenTokenIsExpired(): void
{
$now = $this->clock->now();
$claims = [
RegisteredClaims::ISSUED_AT => $now->modify('-20 seconds'),
RegisteredClaims::NOT_BEFORE => $now->modify('-10 seconds'),
RegisteredClaims::EXPIRATION_TIME => $now->modify('-10 seconds'),
];
$constraint = $this->buildValidAtConstraint($this->clock);
$this->expectException(ConstraintViolation::class);
$this->expectExceptionMessage('The token is expired');
$constraint->assert($this->buildToken($claims));
}
#[PHPUnit\Test]
final public function assertShouldRaiseExceptionWhenMinimumTimeIsNotMet(): void
{
$now = $this->clock->now();
$claims = [
RegisteredClaims::ISSUED_AT => $now->modify('-20 seconds'),
RegisteredClaims::NOT_BEFORE => $now->modify('+40 seconds'),
RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
];
$constraint = $this->buildValidAtConstraint($this->clock);
$this->expectException(ConstraintViolation::class);
$this->expectExceptionMessage('The token cannot be used yet');
$constraint->assert($this->buildToken($claims));
}
#[PHPUnit\Test]
final public function assertShouldRaiseExceptionWhenTokenWasIssuedInTheFuture(): void
{
$now = $this->clock->now();
$claims = [
RegisteredClaims::ISSUED_AT => $now->modify('+20 seconds'),
RegisteredClaims::NOT_BEFORE => $now->modify('+40 seconds'),
RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
];
$constraint = $this->buildValidAtConstraint($this->clock);
$this->expectException(ConstraintViolation::class);
$this->expectExceptionMessage('The token was issued in the future');
$constraint->assert($this->buildToken($claims));
}
#[PHPUnit\Test]
final public function assertShouldNotRaiseExceptionWhenLeewayIsUsed(): void
{
$now = $this->clock->now();
$claims = [
RegisteredClaims::ISSUED_AT => $now->modify('+5 seconds'),
RegisteredClaims::NOT_BEFORE => $now->modify('+5 seconds'),
RegisteredClaims::EXPIRATION_TIME => $now->modify('-5 seconds'),
];
$constraint = $this->buildValidAtConstraint($this->clock, new DateInterval('PT6S'));
$constraint->assert($this->buildToken($claims));
$this->addToAssertionCount(1);
}
#[PHPUnit\Test]
final public function assertShouldNotRaiseExceptionWhenTokenIsUsedInTheRightMoment(): void
{
$constraint = $this->buildValidAtConstraint($this->clock);
$now = $this->clock->now();
$token = $this->buildToken(
[
RegisteredClaims::ISSUED_AT => $now->modify('-40 seconds'),
RegisteredClaims::NOT_BEFORE => $now->modify('-20 seconds'),
RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
],
);
$constraint->assert($token);
$this->addToAssertionCount(1);
$token = $this->buildToken(
[
RegisteredClaims::ISSUED_AT => $now,
RegisteredClaims::NOT_BEFORE => $now,
RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
],
);
$constraint->assert($token);
$this->addToAssertionCount(1);
}
}
|