Skip to content

Commit 0204496

Browse files
committed
Use of the new method for all registered claims
1 parent 9654355 commit 0204496

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/Builder.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,22 @@ public function withClaim($name, $value)
415415
trigger_error('The use of the method "withClaim" is deprecated for registered claims. Please use dedicated method instead.', E_USER_DEPRECATED);
416416
}
417417

418-
if ($name === RegisteredClaims::AUDIENCE) {
419-
return $this->permittedFor($value);
418+
switch ($name) {
419+
case RegisteredClaims::ID:
420+
return $this->identifiedBy($value);
421+
case RegisteredClaims::EXPIRATION_TIME:
422+
return $this->expiresAt($value);
423+
case RegisteredClaims::NOT_BEFORE:
424+
return $this->canOnlyBeUsedAfter($value);
425+
case RegisteredClaims::ISSUED_AT:
426+
return $this->issuedAt($value);
427+
case RegisteredClaims::ISSUER:
428+
return $this->issuedBy($value);
429+
case RegisteredClaims::AUDIENCE:
430+
return $this->permittedFor($value);
431+
default:
432+
return $this->configureClaim($name, $value);
420433
}
421-
422-
return $this->configureClaim($name, $value);
423434
}
424435

425436
/**

test/unit/BuilderTest.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -567,38 +567,25 @@ public function withClaimMustKeepAFluentInterface()
567567
* @param string $name
568568
* @param mixed $value
569569
* @param mixed $expected
570+
* @param null|string $otherMessage
570571
*
571572
* @covers ::__construct
572573
* @covers ::withClaim
573574
* @covers \Lcobucci\JWT\Token\RegisteredClaimGiven
574575
*
575576
* @dataProvider dataWithClaimDeprecationNotice
576577
*/
577-
public function withClaimShouldSendDeprecationNoticeWhenTryingToConfigureARegisteredClaim($name, $value, $expected)
578+
public function withClaimShouldSendDeprecationNoticeWhenTryingToConfigureARegisteredClaim($name, $value, $expected, $otherMessage = null)
578579
{
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-
596580
$this->expectDeprecation('The use of the method "withClaim" is deprecated for registered claims. Please use dedicated method instead.');
581+
if ($otherMessage) {
582+
$this->expectDeprecation($otherMessage);
583+
}
597584

598585
$token = $this
599586
->createBuilder()
600587
->withClaim($name, $value)
601-
->getToken($signer, $key)
588+
->getToken(new None(), Key\InMemory::plainText(''))
602589
;
603590

604591
self::assertEquals($expected, $token->claims()->get($name));
@@ -615,9 +602,9 @@ public function dataWithClaimDeprecationNotice()
615602
['aud', 'him', ['him']],
616603
['jti', '0123456789ABCDEF', '0123456789ABCDEF'],
617604
['iss', 'you', 'you'],
618-
['exp', $nowPlus1HourAsDate->getTimestamp(), $nowPlus1HourAsDate->getTimestamp()],
619-
['iat', $now, $nowAsDate->getTimestamp()],
620-
['nbf', $now, $nowAsDate->getTimestamp()],
605+
['exp', $nowPlus1HourAsDate->getTimestamp(), $nowPlus1HourAsDate, 'Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'],
606+
['iat', $now, $nowAsDate, 'Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'],
607+
['nbf', $now, $nowAsDate, 'Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'],
621608
];
622609
}
623610

0 commit comments

Comments
 (0)