Skip to content

Commit 9179323

Browse files
committed
Remove RegisteredClaimGiven exception in favour of deprecation message for withClaim
This PR is proposed as per the discussion in #559.
1 parent 320b9f0 commit 9179323

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-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: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,15 +564,33 @@ public function withClaimMustKeepAFluentInterface()
564564
/**
565565
* @test
566566
*
567+
* @param string $claim
568+
* @param array $value
569+
*
567570
* @covers ::__construct
568571
* @covers ::withClaim
569572
* @covers \Lcobucci\JWT\Token\RegisteredClaimGiven
573+
*
574+
* @dataProvider dataWithClaimDeprecationNotice
570575
*/
571-
public function withClaimShouldThrowExceptionWhenTryingToConfigureARegisteredClaim()
576+
public function withClaimShouldSendDeprecationNoticeWhenTryingToConfigureARegisteredClaim($claim, $value)
572577
{
573-
$this->expectException(RegisteredClaimGiven::class);
578+
$this->expectDeprecation('The use of the method "withClaim" is deprecated for registered claims. Please use dedicated method instead.');
574579

575-
$this->createBuilder()->withClaim('sub', 'me');
580+
$this->createBuilder()->withClaim($claim, $value);
581+
}
582+
583+
public function dataWithClaimDeprecationNotice()
584+
{
585+
return [
586+
['sub', 'me'],
587+
['aud', 'him'],
588+
['jti', '0123456789ABCDEF'],
589+
['iss', 'you'],
590+
['exp', time()+3600],
591+
['iat', time()],
592+
['nbf', time()],
593+
];
576594
}
577595

578596
/**

0 commit comments

Comments
 (0)