Skip to content

Commit 1ed98b7

Browse files
Nico Schmitzlcobucci
authored andcommitted
Add test for issue #560
1 parent 320b9f0 commit 1ed98b7

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test/functional/CompatibilityLayerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Lcobucci\JWT\FunctionalTests;
44

55
use DateTimeImmutable;
6+
use Lcobucci\Clock\FrozenClock;
67
use Lcobucci\JWT\CheckForDeprecations;
78
use Lcobucci\JWT\Configuration;
89
use Lcobucci\JWT\Keys;
@@ -12,7 +13,12 @@
1213
use Lcobucci\JWT\Token\DataSet;
1314
use Lcobucci\JWT\Token\Plain;
1415
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;
1520
use Lcobucci\JWT\Validation\Constraint\SignedWith;
21+
use Lcobucci\JWT\Validation\Constraint\ValidAt;
1622
use PHPUnit\Framework\TestCase;
1723

1824
use function base64_encode;
@@ -39,7 +45,12 @@
3945
* @covers \Lcobucci\JWT\Token
4046
* @covers \Lcobucci\JWT\Token\DataSet
4147
* @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
4252
* @covers \Lcobucci\JWT\Validation\Constraint\SignedWith
53+
* @covers \Lcobucci\JWT\Validation\Constraint\ValidAt
4354
*/
4455
final class CompatibilityLayerTest extends TestCase
4556
{
@@ -91,6 +102,36 @@ public function registeredDateClaimsShouldBeConvertedToDateObjects()
91102
self::assertEquals($expectedNow->modify('+1 hour'), $token2->claims()->get('exp'));
92103
}
93104

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+
94135
/**
95136
* @test
96137
*

0 commit comments

Comments
 (0)