diff --git a/src/Builder.php b/src/Builder.php index 379ac308..9f0a5bd9 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -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); + } } /** @@ -431,7 +451,7 @@ public function withClaim($name, $value) */ public function set($name, $value) { - return $this->configureClaim($name, $value); + return $this->forwardCallToCorrectClaimMethod($name, $value); } /** diff --git a/test/unit/BuilderTest.php b/test/unit/BuilderTest.php index 8c501fa2..54789634 100644 --- a/test/unit/BuilderTest.php +++ b/test/unit/BuilderTest.php @@ -533,6 +533,7 @@ public function relatedToMustKeepAFluentInterface() * @covers ::configureClaim * @covers ::createSignature * @covers ::convertItems + * @covers ::forwardCallToCorrectClaimMethod * * @uses \Lcobucci\JWT\Builder::getToken */ @@ -553,6 +554,7 @@ public function withClaimMustConfigureTheGivenClaim() * @covers ::__construct * @covers ::withClaim * @covers ::configureClaim + * @covers ::forwardCallToCorrectClaimMethod */ public function withClaimMustKeepAFluentInterface() { @@ -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.'], + ]; } /** @@ -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() {