|
3 | 3 | namespace Lcobucci\JWT\FunctionalTests; |
4 | 4 |
|
5 | 5 | use DateTimeImmutable; |
| 6 | +use Lcobucci\Clock\FrozenClock; |
6 | 7 | use Lcobucci\JWT\CheckForDeprecations; |
7 | 8 | use Lcobucci\JWT\Configuration; |
8 | 9 | use Lcobucci\JWT\Keys; |
|
12 | 13 | use Lcobucci\JWT\Token\DataSet; |
13 | 14 | use Lcobucci\JWT\Token\Plain; |
14 | 15 | use Lcobucci\JWT\Token\Signature; |
| 16 | +use Lcobucci\JWT\Validation\Constraint\IdentifiedBy; |
| 17 | +use Lcobucci\JWT\Validation\Constraint\IssuedBy; |
| 18 | +use Lcobucci\JWT\Validation\Constraint\PermittedFor; |
| 19 | +use Lcobucci\JWT\Validation\Constraint\RelatedTo; |
15 | 20 | use Lcobucci\JWT\Validation\Constraint\SignedWith; |
| 21 | +use Lcobucci\JWT\Validation\Constraint\ValidAt; |
16 | 22 | use PHPUnit\Framework\TestCase; |
17 | 23 |
|
18 | 24 | use function base64_encode; |
|
39 | 45 | * @covers \Lcobucci\JWT\Token |
40 | 46 | * @covers \Lcobucci\JWT\Token\DataSet |
41 | 47 | * @covers \Lcobucci\JWT\Validation\Validator |
| 48 | + * @covers \Lcobucci\JWT\Validation\Constraint\IssuedBy |
| 49 | + * @covers \Lcobucci\JWT\Validation\Constraint\IdentifiedBy |
| 50 | + * @covers \Lcobucci\JWT\Validation\Constraint\PermittedFor |
| 51 | + * @covers \Lcobucci\JWT\Validation\Constraint\RelatedTo |
42 | 52 | * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith |
| 53 | + * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt |
43 | 54 | */ |
44 | 55 | final class CompatibilityLayerTest extends TestCase |
45 | 56 | { |
@@ -91,6 +102,36 @@ public function registeredDateClaimsShouldBeConvertedToDateObjects() |
91 | 102 | self::assertEquals($expectedNow->modify('+1 hour'), $token2->claims()->get('exp')); |
92 | 103 | } |
93 | 104 |
|
| 105 | + /** @test */ |
| 106 | + public function tokenCanBeValidated() |
| 107 | + { |
| 108 | + $now = new DateTimeImmutable(); |
| 109 | + |
| 110 | + $config = Configuration::forSymmetricSigner(new HmacSha256(), Key\InMemory::plainText('testing')); |
| 111 | + $config->setValidationConstraints( |
| 112 | + new IdentifiedBy('123'), |
| 113 | + new IssuedBy('one', 'two', 'three'), |
| 114 | + new PermittedFor('me'), |
| 115 | + new RelatedTo('user123'), |
| 116 | + new ValidAt(new FrozenClock($now->modify('+10 minutes'))), |
| 117 | + new SignedWith($config->signer(), $config->verificationKey()) |
| 118 | + ); |
| 119 | + |
| 120 | + $token = $config->builder() |
| 121 | + ->issuedAt($now) |
| 122 | + ->issuedBy('two') |
| 123 | + ->permittedFor('me') |
| 124 | + ->identifiedBy('123') |
| 125 | + ->relatedTo('user123') |
| 126 | + ->canOnlyBeUsedAfter($now->modify('+5 minutes')) |
| 127 | + ->expiresAt($now->modify('+1 hour')) |
| 128 | + ->getToken($config->signer(), $config->signingKey()); |
| 129 | + |
| 130 | + $config->validator()->assert($token, ...$config->validationConstraints()); |
| 131 | + |
| 132 | + $this->addToAssertionCount(1); |
| 133 | + } |
| 134 | + |
94 | 135 | /** |
95 | 136 | * @test |
96 | 137 | * |
|
0 commit comments