Skip to content

Commit 484d9a6

Browse files
authored
Merge pull request #562 from b-water/audienceSingleElementArray
Fix validation of the audience claim on the new API
2 parents 320b9f0 + a64bafd commit 484d9a6

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

src/Token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function isExpired(DateTimeInterface $now = null)
308308
*/
309309
public function isPermittedFor($audience)
310310
{
311-
return $this->claims->get(RegisteredClaims::AUDIENCE) === $audience;
311+
return in_array($audience, $this->claims->get(RegisteredClaims::AUDIENCE, []), true);
312312
}
313313

314314
/**

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
*

test/unit/TokenTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public function isPermittedForShouldReturnFalseWhenAudienceDoesNotMatch()
536536
{
537537
$token = new Token(
538538
[],
539-
[RegisteredClaims::AUDIENCE => 'test']
539+
[RegisteredClaims::AUDIENCE => ['test']]
540540
);
541541

542542
self::assertFalse($token->isPermittedFor('testing'));
@@ -554,7 +554,7 @@ public function isPermittedForShouldReturnFalseWhenAudienceTypeDoesNotMatch()
554554
{
555555
$token = new Token(
556556
[],
557-
[RegisteredClaims::AUDIENCE => 10]
557+
[RegisteredClaims::AUDIENCE => [10]]
558558
);
559559

560560
self::assertFalse($token->isPermittedFor('10'));
@@ -572,7 +572,7 @@ public function isPermittedForShouldReturnTrueWhenAudienceMatches()
572572
{
573573
$token = new Token(
574574
[],
575-
[RegisteredClaims::AUDIENCE => 'testing']
575+
[RegisteredClaims::AUDIENCE => ['testing']]
576576
);
577577

578578
self::assertTrue($token->isPermittedFor('testing'));

test/unit/Validation/Constraint/PermittedForTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function assertShouldRaiseExceptionWhenAudienceValueDoesNotMatch()
4242
$this->expectExceptionMessage('The token is not allowed to be used by this audience');
4343

4444
$constraint = new PermittedFor('test.com');
45-
$constraint->assert($this->buildToken([RegisteredClaims::AUDIENCE => 'aa.com']));
45+
$constraint->assert($this->buildToken([RegisteredClaims::AUDIENCE => ['aa.com']]));
4646
}
4747

4848
/**
@@ -57,7 +57,7 @@ public function assertShouldRaiseExceptionWhenAudienceTypeDoesNotMatch()
5757
$this->expectExceptionMessage('The token is not allowed to be used by this audience');
5858

5959
$constraint = new PermittedFor('123');
60-
$constraint->assert($this->buildToken([RegisteredClaims::AUDIENCE => 123]));
60+
$constraint->assert($this->buildToken([RegisteredClaims::AUDIENCE => [123]]));
6161
}
6262

6363
/**
@@ -68,7 +68,7 @@ public function assertShouldRaiseExceptionWhenAudienceTypeDoesNotMatch()
6868
*/
6969
public function assertShouldNotRaiseExceptionWhenAudienceMatches()
7070
{
71-
$token = $this->buildToken([RegisteredClaims::AUDIENCE => 'test.com']);
71+
$token = $this->buildToken([RegisteredClaims::AUDIENCE => ['test.com']]);
7272
$constraint = new PermittedFor('test.com');
7373

7474
$constraint->assert($token);

0 commit comments

Comments
 (0)