Skip to content

Commit

Permalink
Add deprecation messages on Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Nov 25, 2020
1 parent 055722c commit 5ce37ae
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/Builder.php
Expand Up @@ -21,6 +21,8 @@
use function implode;
use function in_array;
use function is_array;
use function trigger_error;
use const E_USER_DEPRECATED;

/**
* This class makes easier the token creation process
Expand Down Expand Up @@ -148,6 +150,8 @@ public function expiresAt($expiration, $replicateAsHeader = false)
private function convertToDate($value)
{
if (! $value instanceof DateTimeImmutable) {
trigger_error('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.', E_USER_DEPRECATED);

return new DateTimeImmutable('@' . $value);
}

Expand Down Expand Up @@ -329,6 +333,8 @@ protected function setRegisteredClaim($name, $value, $replicate)
$this->configureClaim($name, $value);

if ($replicate) {
trigger_error('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.', E_USER_DEPRECATED);

$this->headers[$name] = $value;
}

Expand Down Expand Up @@ -444,6 +450,8 @@ public function set($name, $value)
public function sign(Signer $signer, $key)
{
if (! $key instanceof Key) {
trigger_error('Implicit conversion of keys from strings is deprecated. Please use InMemory or LocalFileReference classes.', E_USER_DEPRECATED);

$key = new Key($key);
}

Expand Down Expand Up @@ -476,6 +484,10 @@ public function unsign()
*/
public function getToken(Signer $signer = null, Key $key = null)
{
if ($signer === null || $key === null) {
trigger_error('Not specifying the signer and key to Builder#getToken() is deprecated. Please move the arguments from Builder#sign() to Builder#getToken().', E_USER_DEPRECATED);
}

$signer = $signer ?: $this->signer;
$key = $key ?: $this->key;

Expand Down
6 changes: 6 additions & 0 deletions test/functional/CompatibilityLayerTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Lcobucci\JWT\FunctionalTests;

use DateTimeImmutable;
use Lcobucci\JWT\CheckForDeprecations;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Keys;
use Lcobucci\JWT\Signer\Hmac\Sha256 as HmacSha256;
Expand Down Expand Up @@ -43,6 +44,7 @@
final class CompatibilityLayerTest extends TestCase
{
use Keys;
use CheckForDeprecations;

/** @test */
public function tokenCanBeInstantiatedInTheNewNamespace()
Expand All @@ -59,6 +61,10 @@ public function tokenCanBeInstantiatedInTheNewNamespace()
/** @test */
public function registeredDateClaimsShouldBeConvertedToDateObjects()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'); // iat
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'); // nbf
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.'); // exp

$now = time();

$config = Configuration::forSymmetricSigner(new HmacSha256(), Key\InMemory::plainText('testing'));
Expand Down
6 changes: 6 additions & 0 deletions test/functional/EcdsaTokenTest.php
Expand Up @@ -8,6 +8,7 @@
namespace Lcobucci\JWT\FunctionalTests;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\CheckForDeprecations;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Token;
Expand All @@ -26,6 +27,7 @@
*/
class EcdsaTokenTest extends \PHPUnit\Framework\TestCase
{
use CheckForDeprecations;
use Keys;

/**
Expand Down Expand Up @@ -117,6 +119,8 @@ public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible()
*/
public function builderCanGenerateAToken()
{
$this->expectDeprecation('Not specifying the signer and key to Builder#getToken() is deprecated. Please move the arguments from Builder#sign() to Builder#getToken().');

$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];

$token = (new Builder())->setId(1)
Expand Down Expand Up @@ -276,6 +280,8 @@ public function verifyShouldReturnTrueWhenKeyIsRight(Token $token)
*/
public function everythingShouldWorkWithAKeyWithParams()
{
$this->expectDeprecation('Not specifying the signer and key to Builder#getToken() is deprecated. Please move the arguments from Builder#sign() to Builder#getToken().');

$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];

$token = (new Builder())->setId(1)
Expand Down
6 changes: 6 additions & 0 deletions test/functional/HmacTokenTest.php
Expand Up @@ -8,6 +8,7 @@
namespace Lcobucci\JWT\FunctionalTests;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\CheckForDeprecations;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Signature;
Expand All @@ -22,6 +23,8 @@
*/
class HmacTokenTest extends \PHPUnit\Framework\TestCase
{
use CheckForDeprecations;

/**
* @var Sha256
*/
Expand Down Expand Up @@ -51,6 +54,9 @@ public function createSigner()
*/
public function builderCanGenerateAToken()
{
$this->expectDeprecation('Implicit conversion of keys from strings is deprecated. Please use InMemory or LocalFileReference classes.');
$this->expectDeprecation('Not specifying the signer and key to Builder#getToken() is deprecated. Please move the arguments from Builder#sign() to Builder#getToken().');

$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];

$token = (new Builder())->setId(1)
Expand Down
4 changes: 4 additions & 0 deletions test/functional/RsaTokenTest.php
Expand Up @@ -8,6 +8,7 @@
namespace Lcobucci\JWT\FunctionalTests;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\CheckForDeprecations;
use Lcobucci\JWT\Keys;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key;
Expand All @@ -25,6 +26,7 @@
*/
class RsaTokenTest extends \PHPUnit\Framework\TestCase
{
use CheckForDeprecations;
use Keys;

/**
Expand Down Expand Up @@ -113,6 +115,8 @@ public function builderShouldRaiseExceptionWhenKeyIsNotRsaCompatible()
*/
public function builderCanGenerateAToken()
{
$this->expectDeprecation('Not specifying the signer and key to Builder#getToken() is deprecated. Please move the arguments from Builder#sign() to Builder#getToken().');

$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];

$token = (new Builder())->setId(1)
Expand Down
6 changes: 6 additions & 0 deletions test/functional/UnsignedTokenTest.php
Expand Up @@ -8,6 +8,7 @@
namespace Lcobucci\JWT\FunctionalTests;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\CheckForDeprecations;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signature;
use Lcobucci\JWT\Token;
Expand All @@ -22,6 +23,8 @@
*/
class UnsignedTokenTest extends \PHPUnit\Framework\TestCase
{
use CheckForDeprecations;

const CURRENT_TIME = 100000;

/**
Expand All @@ -35,6 +38,9 @@ class UnsignedTokenTest extends \PHPUnit\Framework\TestCase
*/
public function builderCanGenerateAToken()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.');
$this->expectDeprecation('Not specifying the signer and key to Builder#getToken() is deprecated. Please move the arguments from Builder#sign() to Builder#getToken().');

$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];

$token = (new Builder())->setId(1)
Expand Down
43 changes: 38 additions & 5 deletions test/unit/BuilderTest.php
Expand Up @@ -7,6 +7,7 @@

namespace Lcobucci\JWT;

use DateTimeImmutable;
use Lcobucci\JWT\Claim\Basic;
use Lcobucci\JWT\Claim\EqualsTo;
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
Expand Down Expand Up @@ -38,6 +39,8 @@
*/
class BuilderTest extends TestCase
{
use CheckForDeprecations;

/**
* @var Encoder|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -96,6 +99,8 @@ public function permittedForMustChangeTheAudClaim()
*/
public function permittedForCanReplicateItemOnHeader()
{
$this->expectDeprecation('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.');

$builder = $this->createBuilder();
$builder->permittedFor('test', true);

Expand Down Expand Up @@ -135,6 +140,8 @@ public function permittedForMustKeepAFluentInterface()
*/
public function expiresAtMustChangeTheExpClaim()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.');

$builder = $this->createBuilder();
$builder->expiresAt('2');

Expand All @@ -160,8 +167,10 @@ public function expiresAtMustChangeTheExpClaim()
*/
public function expiresAtCanReplicateItemOnHeader()
{
$this->expectDeprecation('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.');

$builder = $this->createBuilder();
$builder->expiresAt('2', true);
$builder->expiresAt(new DateTimeImmutable('@2'), true);

$token = $builder->getToken(new None(), Key\InMemory::plainText(''));

Expand All @@ -180,6 +189,8 @@ public function expiresAtCanReplicateItemOnHeader()
*/
public function expiresAtMustKeepAFluentInterface()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.');

$builder = $this->createBuilder();

self::assertSame($builder, $builder->expiresAt('2'));
Expand Down Expand Up @@ -222,6 +233,8 @@ public function identifiedByMustChangeTheJtiClaim()
*/
public function identifiedByCanReplicateItemOnHeader()
{
$this->expectDeprecation('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.');

$builder = $this->createBuilder();
$builder->identifiedBy('2', true);

Expand Down Expand Up @@ -261,6 +274,8 @@ public function identifiedByMustKeepAFluentInterface()
*/
public function issuedAtMustChangeTheIatClaim()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.');

$builder = $this->createBuilder();
$builder->issuedAt('2');

Expand All @@ -285,8 +300,10 @@ public function issuedAtMustChangeTheIatClaim()
*/
public function issuedAtCanReplicateItemOnHeader()
{
$this->expectDeprecation('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.');

$builder = $this->createBuilder();
$builder->issuedAt('2', true);
$builder->issuedAt(new DateTimeImmutable('@2'), true);

$token = $builder->getToken(new None(), Key\InMemory::plainText(''));

Expand All @@ -305,6 +322,8 @@ public function issuedAtCanReplicateItemOnHeader()
*/
public function issuedAtMustKeepAFluentInterface()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.');

$builder = $this->createBuilder();

self::assertSame($builder, $builder->issuedAt('2'));
Expand Down Expand Up @@ -347,6 +366,8 @@ public function issuedByMustChangeTheIssClaim()
*/
public function issuedByCanReplicateItemOnHeader()
{
$this->expectDeprecation('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.');

$builder = $this->createBuilder();
$builder->issuedBy('2', true);

Expand Down Expand Up @@ -386,6 +407,8 @@ public function issuedByMustKeepAFluentInterface()
*/
public function canOnlyBeUsedAfterMustChangeTheNbfClaim()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.');

$builder = $this->createBuilder();
$builder->canOnlyBeUsedAfter('2');

Expand All @@ -410,8 +433,10 @@ public function canOnlyBeUsedAfterMustChangeTheNbfClaim()
*/
public function canOnlyBeUsedAfterCanReplicateItemOnHeader()
{
$this->expectDeprecation('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.');

$builder = $this->createBuilder();
$builder->canOnlyBeUsedAfter('2', true);
$builder->canOnlyBeUsedAfter(new DateTimeImmutable('@2'), true);

$token = $builder->getToken(new None(), Key\InMemory::plainText(''));

Expand All @@ -430,6 +455,8 @@ public function canOnlyBeUsedAfterCanReplicateItemOnHeader()
*/
public function canOnlyBeUsedAfterMustKeepAFluentInterface()
{
$this->expectDeprecation('Using integers for registered date claims is deprecated, please use DateTimeImmutable objects instead.');

$builder = $this->createBuilder();

self::assertSame($builder, $builder->canOnlyBeUsedAfter('2'));
Expand Down Expand Up @@ -472,6 +499,8 @@ public function relatedToMustChangeTheSubClaim()
*/
public function relatedToCanReplicateItemOnHeader()
{
$this->expectDeprecation('Replicating claims as headers is deprecated and will removed from v4.0. Please manually set the header if you need it replicated.');

$builder = $this->createBuilder();
$builder->relatedTo('2', true);

Expand Down Expand Up @@ -512,7 +541,7 @@ public function withClaimMustConfigureTheGivenClaim()
$builder = $this->createBuilder();
$builder->withClaim('userId', 2);

$token = $builder->getToken();
$token = $builder->getToken(new None(), Key\InMemory::plainText(''));

self::assertEquals(['typ' => 'JWT', 'alg' => 'none'], $token->getHeaders());
self::assertEquals(['userId' => new Basic('userId', 2)], $token->getClaims());
Expand Down Expand Up @@ -561,7 +590,7 @@ public function withHeaderMustConfigureTheGivenClaim()
$builder = $this->createBuilder();
$builder->withHeader('userId', 2);

$token = $builder->getToken();
$token = $builder->getToken(new None(), Key\InMemory::plainText(''));

self::assertEquals(['typ' => 'JWT', 'alg' => 'none', 'userId' => 2], $token->getHeaders());
self::assertEquals([], $token->getClaims());
Expand Down Expand Up @@ -592,6 +621,8 @@ public function withHeaderMustKeepAFluentInterface()
*/
public function signMustConfigureSignerAndKey()
{
$this->expectDeprecation('Implicit conversion of keys from strings is deprecated. Please use InMemory or LocalFileReference classes.');

$signer = $this->createMock(Signer::class);

$builder = $this->createBuilder();
Expand All @@ -609,6 +640,8 @@ public function signMustConfigureSignerAndKey()
*/
public function signMustKeepAFluentInterface()
{
$this->expectDeprecation('Implicit conversion of keys from strings is deprecated. Please use InMemory or LocalFileReference classes.');

$signer = $this->createMock(Signer::class);
$builder = $this->createBuilder();

Expand Down

0 comments on commit 5ce37ae

Please sign in to comment.