Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arrays for audience #547

Merged
merged 3 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

use function array_key_exists;
use function assert;
use function current;
use function implode;
use function in_array;
use function is_array;

/**
* This class makes easier the token creation process
Expand Down Expand Up @@ -106,7 +108,7 @@ public function canOnlyBeUsedBy($audience, $replicateAsHeader = false)
*/
public function permittedFor($audience, $replicateAsHeader = false)
{
return $this->setRegisteredClaim('aud', (string) $audience, $replicateAsHeader);
return $this->setRegisteredClaim('aud', [(string) $audience], $replicateAsHeader);
}

/**
Expand All @@ -122,7 +124,7 @@ public function permittedFor($audience, $replicateAsHeader = false)
*/
public function setAudience($audience, $replicateAsHeader = false)
{
return $this->setRegisteredClaim('aud', (string) $audience, $replicateAsHeader);
return $this->permittedFor($audience, $replicateAsHeader);
}

/**
Expand Down Expand Up @@ -482,8 +484,8 @@ public function getToken(Signer $signer = null, Key $key = null)
}

$payload = [
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertDatesToInt($this->headers))),
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertDatesToInt($this->claims)))
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertItems($this->headers))),
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->convertItems($this->claims)))
];

$signature = $this->createSignature($payload, $signer, $key);
Expand All @@ -503,7 +505,7 @@ public function getToken(Signer $signer = null, Key $key = null)
*
* @return array<string, mixed>
*/
private function convertDatesToInt(array $items)
private function convertItems(array $items)
{
foreach (RegisteredClaims::DATE_CLAIMS as $name) {
if (! array_key_exists($name, $items) || ! $items[$name] instanceof DateTimeImmutable) {
Expand All @@ -513,6 +515,10 @@ private function convertDatesToInt(array $items)
$items[$name] = $items[$name]->getTimestamp();
}

if (array_key_exists(RegisteredClaims::AUDIENCE, $items) && is_array($items[RegisteredClaims::AUDIENCE])) {
$items[RegisteredClaims::AUDIENCE] = current($items[RegisteredClaims::AUDIENCE]);
}

return $items;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Claim/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use DateTimeImmutable;
use Lcobucci\JWT\Claim;
use Lcobucci\JWT\Token\RegisteredClaims;
use function current;
use function in_array;
use function is_array;

/**
* Class that create claims
Expand Down Expand Up @@ -64,6 +66,10 @@ public function create($name, $value)
$value = $value->getTimestamp();
}

if ($name === RegisteredClaims::AUDIENCE && is_array($value)) {
$value = current($value);
}

if (!empty($this->callbacks[$name])) {
return call_user_func($this->callbacks[$name], $name, $value);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Lcobucci\JWT\Token\UnsupportedHeaderFound;
use RuntimeException;
use function array_key_exists;
use function is_array;

/**
* This class parses the JWT strings and convert them into tokens
Expand Down Expand Up @@ -119,7 +120,7 @@ protected function parseHeader($data)
throw UnsupportedHeaderFound::encryption();
}

return $this->convertToDateObjects($header);
return $this->convertItems($header);
}

/**
Expand All @@ -133,15 +134,15 @@ protected function parseClaims($data)
{
$claims = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data));

return $this->convertToDateObjects($claims);
return $this->convertItems($claims);
}

/**
* @param array<string, mixed> $items
*
* @return array<string, mixed>
*/
private function convertToDateObjects(array $items)
private function convertItems(array $items)
{
foreach (RegisteredClaims::DATE_CLAIMS as $name) {
if (! array_key_exists($name, $items)) {
Expand All @@ -151,6 +152,10 @@ private function convertToDateObjects(array $items)
$items[$name] = new DateTimeImmutable('@' . ((int) $items[$name]));
}

if (array_key_exists(RegisteredClaims::AUDIENCE, $items) && ! is_array($items[RegisteredClaims::AUDIENCE])) {
$items[RegisteredClaims::AUDIENCE] = [$items[RegisteredClaims::AUDIENCE]];
}

return $items;
}

Expand Down
8 changes: 7 additions & 1 deletion src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Lcobucci\JWT\Token\DataSet;
use Lcobucci\JWT\Token\RegisteredClaims;
use OutOfBoundsException;
use function current;
use function func_num_args;
use function in_array;
use function is_array;
use function sprintf;

/**
Expand Down Expand Up @@ -237,7 +239,11 @@ public function getClaim($name, $default = null)
}

if ($value instanceof DateTimeImmutable && in_array($name, RegisteredClaims::DATE_CLAIMS, true)) {
$value = $value->getTimestamp();
return $value->getTimestamp();
}

if ($name === RegisteredClaims::AUDIENCE && is_array($value)) {
return current($value);
}

return $value;
Expand Down
3 changes: 3 additions & 0 deletions test/functional/CompatibilityLayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ public function registeredDateClaimsShouldBeConvertedToDateObjects()

$token = $config->builder()
->issuedAt($now)
->permittedFor('test')
->canOnlyBeUsedAfter($now + 5)
->expiresAt($now + 3600)
->getToken($config->signer(), $config->signingKey());

$expectedNow = new DateTimeImmutable('@' . $now);

self::assertSame(['test'], $token->claims()->get('aud'));
self::assertEquals($expectedNow, $token->claims()->get('iat'));
self::assertEquals($expectedNow->modify('+5 seconds'), $token->claims()->get('nbf'));
self::assertEquals($expectedNow->modify('+1 hour'), $token->claims()->get('exp'));

$token2 = $config->parser()->parse($token->toString());

self::assertSame(['test'], $token2->claims()->get('aud'));
self::assertEquals($expectedNow, $token2->claims()->get('iat'));
self::assertEquals($expectedNow->modify('+5 seconds'), $token2->claims()->get('nbf'));
self::assertEquals($expectedNow->modify('+1 hour'), $token2->claims()->get('exp'));
Expand Down
36 changes: 18 additions & 18 deletions test/unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function createBuilder()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -85,7 +85,7 @@ public function permittedForMustChangeTheAudClaim()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -124,7 +124,7 @@ public function permittedForMustKeepAFluentInterface()
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertToDate
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -149,7 +149,7 @@ public function expiresAtMustChangeTheExpClaim()
* @covers ::createSignature
* @covers ::convertToDate
* @covers ::convertToDate
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -188,7 +188,7 @@ public function expiresAtMustKeepAFluentInterface()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -211,7 +211,7 @@ public function identifiedByMustChangeTheJtiClaim()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -250,7 +250,7 @@ public function identifiedByMustKeepAFluentInterface()
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertToDate
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -274,7 +274,7 @@ public function issuedAtMustChangeTheIatClaim()
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertToDate
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -313,7 +313,7 @@ public function issuedAtMustKeepAFluentInterface()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -336,7 +336,7 @@ public function issuedByMustChangeTheIssClaim()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -375,7 +375,7 @@ public function issuedByMustKeepAFluentInterface()
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertToDate
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -399,7 +399,7 @@ public function canOnlyBeUsedAfterMustChangeTheNbfClaim()
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertToDate
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -438,7 +438,7 @@ public function canOnlyBeUsedAfterMustKeepAFluentInterface()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -461,7 +461,7 @@ public function relatedToMustChangeTheSubClaim()
* @covers ::setRegisteredClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -498,7 +498,7 @@ public function relatedToMustKeepAFluentInterface()
* @covers ::withClaim
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -547,7 +547,7 @@ public function withClaimShouldThrowExceptionWhenTryingToConfigureARegisteredCla
* @covers ::__construct
* @covers ::withHeader
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -581,7 +581,7 @@ public function withHeaderMustKeepAFluentInterface()
* @covers ::__construct
* @covers ::sign
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand Down Expand Up @@ -644,7 +644,7 @@ public function unsignMustKeepAFluentInterface(Builder $builder)
*
* @covers ::getToken
* @covers ::createSignature
* @covers ::convertDatesToInt
* @covers ::convertItems
*
* @uses \Lcobucci\JWT\Builder::__construct
* @uses \Lcobucci\JWT\Builder::configureClaim
Expand Down
6 changes: 3 additions & 3 deletions test/unit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken()
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
* @covers Lcobucci\JWT\Parser::convertToDateObjects
* @covers Lcobucci\JWT\Parser::convertItems
* @covers \Lcobucci\JWT\Claim\Factory
* @covers \Lcobucci\JWT\Claim\Basic
* @covers \Lcobucci\JWT\Claim\EqualsTo
Expand Down Expand Up @@ -175,7 +175,7 @@ public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed()
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
* @covers Lcobucci\JWT\Parser::convertToDateObjects
* @covers Lcobucci\JWT\Parser::convertItems
* @covers \Lcobucci\JWT\Claim\Factory
* @covers \Lcobucci\JWT\Claim\Basic
* @covers \Lcobucci\JWT\Claim\EqualsTo
Expand Down Expand Up @@ -214,7 +214,7 @@ public function parseShouldReplicateClaimValueOnHeaderWhenNeeded()
* @covers Lcobucci\JWT\Parser::parseHeader
* @covers Lcobucci\JWT\Parser::parseClaims
* @covers Lcobucci\JWT\Parser::parseSignature
* @covers Lcobucci\JWT\Parser::convertToDateObjects
* @covers Lcobucci\JWT\Parser::convertItems
* @covers \Lcobucci\JWT\Claim\Factory
* @covers \Lcobucci\JWT\Claim\Basic
* @covers \Lcobucci\JWT\Claim\EqualsTo
Expand Down