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 Jan 7, 2017
1 parent 3ad8661 commit 4fb4d79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/Token/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Lcobucci\JWT\Token;

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/Token/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\Token\Builder::__construct
* @uses \Lcobucci\JWT\Token\Builder::with
*
* @covers \Lcobucci\JWT\Token\Builder::canOnlyBeUsedBy
* @covers \Lcobucci\JWT\Token\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 4fb4d79

Please sign in to comment.