8000 Remove RegisteredClaimGiven exception in favour of deprecation messag… · lcobucci/jwt@a37e7ec · GitHub
[go: up one dir, main page]

Skip to content

Commit a37e7ec

Browse files
Spomkylcobucci
authored andcommitted
Remove RegisteredClaimGiven exception in favour of deprecation message for withClaim
This PR is proposed as per the discussion in #559.
1 parent 484d9a6 commit a37e7ec

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/Builder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,11 @@ private function configureClaim($name, $value)
412412
public function withClaim($name, $value)
413413
{
414414
if (in_array($name, RegisteredClaims::ALL, true)) {
415-
throw RegisteredClaimGiven::forClaim($name);
415+
trigger_error('The use of the method "withClaim" is deprecated for registered claims. Please use dedicated method instead.', E_USER_DEPRECATED);
416+
}
417+
418+
if ($name === RegisteredClaims::AUDIENCE) {
419+
return $this->permittedFor($value);
416420
}
417421

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

test/unit/BuilderTest.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,15 +564,61 @@ public function withClaimMustKeepAFluentInterface()
564564
/**
565565
* @test
566566
*
567+
* @param string $name
568+
* @param mixed $value
569+
* @param mixed $expected
570+
*
567571
* @covers ::__construct
568572
* @covers ::withClaim
569573
* @covers \Lcobucci\JWT\Token\RegisteredClaimGiven
574+
*
575+
* @dataProvider dataWithClaimDeprecationNotice
570576
*/
571-
public function withClaimShouldThrowExceptionWhenTryingToConfigureARegisteredClaim()
577+
public function withClaimShouldSendDeprecationNoticeWhenTryingToConfigureARegisteredClaim($name, $value, $expected)
578+
{
579+
$key = $this->createMock(Key::class);
580+
$signature = $this->createMock(Signature::class);
581+
$signature
582+
->expects(static::once())
583+
->method('hash')
584+
->willReturn('--hash--')
585+
;
586+
587+
$signer = $this->createMock(Signer::class);
588+
$signer
589+
->expects(static::once())
590+
->method('sign')
591+
->willReturn($signature)
592+
;
593+
594+
595+
596+
$this->expectDeprecation('The use of the method "withClaim" is deprecated for registered claims. Please use dedicated method instead.');
597+
598+
$token = $this
599+
->createBuilder()
600+
->withClaim($name, $value)
601+
->getToken($signer, $key)
602+
;
603+
604+
self::assertEquals($expected, $token->claims()->get($name));
605+
}
606+
607+
public function dataWithClaimDeprecationNotice()
572608
{
573-
$this->expectException(RegisteredClaimGiven::class);
609+
$now = time();
610+
$nowAsDate = new DateTimeImmutable('@' . $now);
611+
$nowPlus1HourAsDate = $nowAsDate->modify('+1 hour');
574612

575-
$this->createBuilder()->withClaim('sub', 'me');
613+
return [
614+
['sub', 'me', 'me'],
615+
['aud', 'him', ['him']],
616+
['jti', '0123456789ABCDEF', '0123456789ABCDEF'],
617+
['iss', 'you', 'you'],
618+
['exp', $nowPlus1HourAsDate->getTimestamp(), $nowPlus1HourAsDate->getTimestamp()],
619+
['iat', $now, $nowAsDate->getTimestamp()],
620+
['nbf', $now, $nowAsDate->getTimestamp()],
621+
];
576622
}
577623

578624
/**

0 commit comments

Comments
 (0)
0