Skip to content

Commit

Permalink
Merge pull request #235 from mfn/mfn-issue-225-spread-aud
Browse files Browse the repository at this point in the history
lcobucci/jwt: add array support for `aud` claim
  • Loading branch information
Messhias authored Feb 21, 2024
2 parents bc819fd + 1a99a16 commit 826aa52
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ You can find and compare releases at the GitHub release page.

### Added
- Different TTL configurations for each guard
- lcobucci/jwt: add array support for `aud` claim

## [2.0.0] 2022-09-08
- No changes to 2.0.0-RC1
Expand Down
6 changes: 5 additions & 1 deletion src/Providers/JWT/Lcobucci.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ protected function addClaim($key, $value)
$this->builder->issuedBy($value);
break;
case RegisteredClaims::AUDIENCE:
$this->builder->permittedFor($value);
if (is_array($value)) {
$this->builder->permittedFor(...$value);
} else {
$this->builder->permittedFor($value);
}
break;
case RegisteredClaims::SUBJECT:
$this->builder->relatedTo($value);
Expand Down
42 changes: 42 additions & 0 deletions tests/Providers/JWT/LcobucciTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,48 @@ public function itShouldCorrectlyInstantiateAnEcdsaSigner()
$this->assertSame('ES256', $provider->getConfig()->signer()->algorithmId());
}

public function testEncodeAudienceClaimString(): void
{
$payload = [
'aud' => 'foo',
];

$dataSet = new DataSet($payload, 'payload');

$this->builder->shouldReceive('permittedFor')->once()->andReturnSelf(); // aud
$this->builder
->shouldReceive('getToken')
->once()
->with(\Mockery::type(Signer::class), \Mockery::type(Key::class))
->andReturn(new Token\Plain(new DataSet([], 'header'), $dataSet, new Token\Signature('', 'signature')));

/** @var Token $token */
$token = $this->getProvider('secret', 'HS256')->encode($payload);

$this->assertSame('header.payload.signature', $token);
}

public function testEncodeAudienceClaimArray(): void
{
$payload = [
'aud' => ['foo', 'bar'],
];

$dataSet = new DataSet($payload, 'payload');

$this->builder->shouldReceive('permittedFor')->once()->andReturnSelf(); // aud
$this->builder
->shouldReceive('getToken')
->once()
->with(\Mockery::type(Signer::class), \Mockery::type(Key::class))
->andReturn(new Token\Plain(new DataSet([], 'header'), $dataSet, new Token\Signature('', 'signature')));

/** @var Token $token */
$token = $this->getProvider('secret', 'HS256')->encode($payload);

$this->assertSame('header.payload.signature', $token);
}

public function getProvider($secret, $algo, array $keys = [])
{
$provider = new Lcobucci($secret, $algo, $keys);
Expand Down

0 comments on commit 826aa52

Please sign in to comment.