Skip to content

Commit

Permalink
Merge pull request #561 from Spomky/patch-1
Browse files Browse the repository at this point in the history
Remove RegisteredClaimGiven exception in favour of deprecation message for withClaim
  • Loading branch information
lcobucci authored Nov 27, 2020
2 parents 484d9a6 + 0542295 commit 958a987
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 7 deletions.
26 changes: 23 additions & 3 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,30 @@ private function configureClaim($name, $value)
public function withClaim($name, $value)
{
if (in_array($name, RegisteredClaims::ALL, true)) {
throw RegisteredClaimGiven::forClaim($name);
trigger_error('The use of the method "withClaim" is deprecated for registered claims. Please use dedicated method instead.', E_USER_DEPRECATED);
}

return $this->configureClaim($name, $value);
return $this->forwardCallToCorrectClaimMethod($name, $value);
}

private function forwardCallToCorrectClaimMethod($name, $value)
{
switch ($name) {
case RegisteredClaims::ID:
return $this->identifiedBy($value);
case RegisteredClaims::EXPIRATION_TIME:
return $this->expiresAt($value);
case RegisteredClaims::NOT_BEFORE:
return $this->canOnlyBeUsedAfter($value);
case RegisteredClaims::ISSUED_AT:
return $this->issuedAt($value);
case RegisteredClaims::ISSUER:
return $this->issuedBy($value);
case RegisteredClaims::AUDIENCE:
return $this->permittedFor($value);
default:
return $this->configureClaim($name, $value);
}
}

/**
Expand All @@ -431,7 +451,7 @@ public function withClaim($name, $value)
*/
public function set($name, $value)
{
return $this->configureClaim($name, $value);
return $this->forwardCallToCorrectClaimMethod($name, $value);
}

/**
Expand Down
95 changes: 91 additions & 4 deletions test/unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ public function relatedToMustKeepAFluentInterface()
* @covers ::configureClaim
* @covers ::createSignature
* @covers ::convertItems
* @covers ::forwardCallToCorrectClaimMethod
*
* @uses \Lcobucci\JWT\Builder::getToken
*/
Expand All @@ -553,6 +554,7 @@ public function withClaimMustConfigureTheGivenClaim()
* @covers ::__construct
* @covers ::withClaim
* @covers ::configureClaim
* @covers ::forwardCallToCorrectClaimMethod
*/
public function withClaimMustKeepAFluentInterface()
{
Expand All @@ -564,15 +566,99 @@ public function withClaimMustKeepAFluentInterface()
/**
* @test
*
* @param string $name
* @param mixed $value
* @param mixed $expected
* @param null|string $otherMessage
*
* @covers ::__construct
* @covers ::withClaim
* @covers \Lcobucci\JWT\Token\RegisteredClaimGiven
* @covers ::canOnlyBeUsedAfter
* @covers ::configureClaim
* @covers ::convertItems
* @covers ::convertToDate
* @covers ::getToken
* @covers ::setRegisteredClaim
* @covers ::createSignature
* @covers ::expiresAt
* @covers ::issuedBy
* @covers ::identifiedBy
* @covers ::permittedFor
* @covers ::forwardCallToCorrectClaimMethod
* @covers ::issuedAt
*
* @dataProvider dataWithClaimDeprecationNotice
*/
public function withClaimShouldThrowExceptionWhenTryingToConfigureARegisteredClaim()
public function withClaimShouldSendDeprecationNoticeWhenTryingToConfigureARegisteredClaim($name, $value, $expected, $otherMessage = null)
{
$this->expectDeprecation('The use of the method "withClaim" is deprecated for registered claims. Please use dedicated method instead.');

if ($otherMessage) {
$this->expectDeprecation($otherMessage);
}

$token = $this->createBuilder()
->withClaim($name, $value)
->getToken(new None(), Key\InMemory::plainText(''));

self::assertEquals($expected, $token->claims()->get($name));
}


/**
* @test
*
* @param string $name
* @param mixed $value
* @param mixed $expected
* @param null|string $otherMessage
*
* @covers ::__construct
* @covers ::set
* @covers ::canOnlyBeUsedAfter
* @covers ::configureClaim
* @covers ::convertItems
* @covers ::convertToDate
* @covers ::getToken
* @covers ::setRegisteredClaim
* @covers ::createSignature
* @covers ::expiresAt
* @covers ::issuedBy
* @covers ::identifiedBy
* @covers ::permittedFor
* @covers ::forwardCallToCorrectClaimMethod
* @covers ::issuedAt
*
* @dataProvider dataWithClaimDeprecationNotice
*/
public function setShouldSendDeprecationNoticeWhenTryingToConfigureARegisteredClaim($name, $value, $expected, $otherMessage = null)
{
if ($otherMessage) {
$this->expectDeprecation($otherMessage);
}

$token = $this->createBuilder()
->set($name, $value)
->getToken(new None(), Key\InMemory::plainText(''));

self::assertEquals($expected, $token->claims()->get($name));
}

public function dataWithClaimDeprecationNotice()
{
$this->expectException(RegisteredClaimGiven::class);
$now = time();
$nowAsDate = new DateTimeImmutable('@' . $now);
$nowPlus1HourAsDate = $nowAsDate->modify('+1 hour');

$this->createBuilder()->withClaim('sub', 'me');
return [
['sub', 'me', 'me'],
['aud', 'him', ['him']],
['jti', '0123456789ABCDEF', '0123456789ABCDEF'],
['iss', 'you', 'you'],
['exp', $nowPlus1HourAsDate->getTimestamp(), $nowPlus1HourAsDate, 'Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'],
['iat', $now, $nowAsDate, 'Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'],
['nbf', $now, $nowAsDate, 'Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'],
];
}

/**
Expand Down Expand Up @@ -687,6 +773,7 @@ public function unsignMustKeepAFluentInterface(Builder $builder)
* @uses \Lcobucci\JWT\Builder::__construct
* @uses \Lcobucci\JWT\Builder::configureClaim
* @uses \Lcobucci\JWT\Builder::withClaim
* @uses \Lcobucci\JWT\Builder::forwardCallToCorrectClaimMethod
*/
public function getTokenMustReturnANewTokenWithCurrentConfiguration()
{
Expand Down

0 comments on commit 958a987

Please sign in to comment.