Skip to content

Commit

Permalink
Prevent the creation of tokens with duplicated audiences
Browse files Browse the repository at this point in the history
And remove things that are not useful now that we have scalar type
hints.

Fixes #131
  • Loading branch information
lcobucci committed Oct 28, 2016
1 parent 72a260f commit 2373e57
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/Storage/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Lcobucci\JWT\Storage;

use BadMethodCallException;
use Lcobucci\Jose\Parsing;
use Lcobucci\JWT\Builder as BuilderInterface;
use Lcobucci\JWT\Signer;
Expand Down Expand Up @@ -59,13 +58,12 @@ public function __construct(Parsing\Encoder $encoder)
public function canOnlyBeUsedBy(string $audience, bool $addHeader = false): BuilderInterface
{
$audiences = $this->claims['aud'] ?? [];
$audiences[] = $audience;

return $this->setRegisteredClaim(
'aud',
array_values(array_map('strval', $audiences)),
$addHeader
);
if (!in_array($audience, $audiences)) {
$audiences[] = $audience;
}

return $this->setRegisteredClaim('aud', $audiences, $addHeader);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/unit/Storage/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ public function canOnlyBeUsedByMustAppendToTheAudClaim()
self::assertAttributeEquals(['aud' => ['test', 'test2']], 'claims', $builder);
}

/**
* @test
*
* @uses \Lcobucci\JWT\Storage\Builder::__construct
* @uses \Lcobucci\JWT\Storage\Builder::with
*
* @covers \Lcobucci\JWT\Storage\Builder::canOnlyBeUsedBy
* @covers \Lcobucci\JWT\Storage\Builder::setRegisteredClaim
*/
public function canOnlyBeUsedByShouldPreventDuplicatedEntries()
{
$builder = $this->createBuilder();
$builder->canOnlyBeUsedBy('test');
$builder->canOnlyBeUsedBy('test');

self::assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
self::assertAttributeEquals(['aud' => ['test']], 'claims', $builder);
}

/**
* @test
*
Expand Down

0 comments on commit 2373e57

Please sign in to comment.