Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Bundle/Services/JWELoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Jose\Bundle\JoseFramework\Services;

use Jose\Component\Core\Exception\InvalidArgumentException;
use Jose\Component\Encryption\JWETokenSupport;
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
use Psr\EventDispatcher\EventDispatcherInterface;
use function func_get_arg;
use function func_num_args;
use function trigger_deprecation;

final readonly class JWELoaderFactory
{
Expand All @@ -18,6 +20,16 @@ public function __construct(
private ?HeaderCheckerManagerFactory $headerCheckerManagerFactory,
private EventDispatcherInterface $eventDispatcher
) {
if ($headerCheckerManagerFactory === null) {
trigger_deprecation(
'web-token/jwt-framework',
'4.3.0',
'Creating a "%s" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "%s" that has the "%s" token type support.',
self::class,
HeaderCheckerManagerFactory::class,
JWETokenSupport::class
);
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Bundle/Services/JWSLoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Jose\Bundle\JoseFramework\Services;

use Jose\Component\Core\Exception\InvalidArgumentException;
use Jose\Component\Signature\JWSTokenSupport;
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory;
use Psr\EventDispatcher\EventDispatcherInterface;
use function func_get_arg;
use function func_num_args;
use function trigger_deprecation;

final readonly class JWSLoaderFactory
{
Expand All @@ -18,6 +20,16 @@ public function __construct(
private ?HeaderCheckerManagerFactory $headerCheckerManagerFactory,
private EventDispatcherInterface $eventDispatcher
) {
if ($headerCheckerManagerFactory === null) {
trigger_deprecation(
'web-token/jwt-framework',
'4.3.0',
'Creating a "%s" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "%s" that has the "%s" token type support.',
self::class,
HeaderCheckerManagerFactory::class,
JWSTokenSupport::class
);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Library/Encryption/JWELoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
use function func_get_arg;
use function func_num_args;
use function trigger_deprecation;

readonly class JWELoaderFactory
{
Expand All @@ -17,6 +18,16 @@ public function __construct(
private JWEDecrypterFactory $jweDecrypterFactory,
private ?HeaderCheckerManagerFactory $headerCheckerManagerFactory
) {
if ($headerCheckerManagerFactory === null) {
trigger_deprecation(
'web-token/jwt-library',
'4.3.0',
'Creating a "%s" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "%s" that has the "%s" token type support.',
static::class,
HeaderCheckerManagerFactory::class,
JWETokenSupport::class
);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Library/Signature/JWSLoaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory;
use function func_get_arg;
use function func_num_args;
use function trigger_deprecation;

class JWSLoaderFactory
{
Expand All @@ -17,6 +18,16 @@ public function __construct(
private readonly JWSVerifierFactory $jwsVerifierFactory,
private readonly ?HeaderCheckerManagerFactory $headerCheckerManagerFactory
) {
if ($headerCheckerManagerFactory === null) {
trigger_deprecation(
'web-token/jwt-library',
'4.3.0',
'Creating a "%s" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "%s" that has the "%s" token type support.',
static::class,
HeaderCheckerManagerFactory::class,
JWSTokenSupport::class
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Bundle\JoseFramework\Functional;

use Jose\Bundle\JoseFramework\Services\HeaderCheckerManagerFactory;
use Jose\Bundle\JoseFramework\Services\JWEDecrypterFactory;
use Jose\Bundle\JoseFramework\Services\JWELoaderFactory;
use Jose\Bundle\JoseFramework\Services\JWSLoaderFactory;
use Jose\Bundle\JoseFramework\Services\JWSVerifierFactory;
use Jose\Component\Core\AlgorithmManagerFactory;
use Jose\Component\Encryption\JWETokenSupport;
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
use Jose\Component\Signature\JWSTokenSupport;
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory;
use Jose\Tests\Bundle\JoseFramework\WebTestCase;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\EventDispatcher\EventDispatcher;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use const E_USER_DEPRECATED;

/**
* @internal
*/
final class LoaderFactoriesWithoutHeaderCheckerManagerFactoryTest extends WebTestCase
{
private const MESSAGE = 'Since web-token/jwt-framework 4.3.0: Creating a "%s" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "%s" that has the "%s" token type support.';

#[Test]
public function theLoaderFactoriesWithoutHeaderCheckerManagerFactoryAreDeprecated(): void
{
$eventDispatcher = new EventDispatcher();
$algorithmManagerFactory = new AlgorithmManagerFactory([]);
$jweSerializerManagerFactory = new JWESerializerManagerFactory();
$jweDecrypterFactory = new JWEDecrypterFactory($algorithmManagerFactory, $eventDispatcher);
$jwsSerializerManagerFactory = new JWSSerializerManagerFactory();
$jwsVerifierFactory = new JWSVerifierFactory($algorithmManagerFactory, $eventDispatcher);

$deprecations = $this->collectDeprecations(static function () use (
$eventDispatcher,
$jweSerializerManagerFactory,
$jweDecrypterFactory,
$jwsSerializerManagerFactory,
$jwsVerifierFactory
): void {
new JWELoaderFactory($jweSerializerManagerFactory, $jweDecrypterFactory, null, $eventDispatcher);
new JWSLoaderFactory($jwsSerializerManagerFactory, $jwsVerifierFactory, null, $eventDispatcher);
});

static::assertSame([
sprintf(self::MESSAGE, JWELoaderFactory::class, HeaderCheckerManagerFactory::class, JWETokenSupport::class),
sprintf(self::MESSAGE, JWSLoaderFactory::class, HeaderCheckerManagerFactory::class, JWSTokenSupport::class),
], $deprecations);
}

#[Test]
public function theLoaderFactoriesOfTheContainerAreNotDeprecated(): void
{
static::ensureKernelShutdown();
$client = static::createClient();
$container = $client->getContainer();

$deprecations = $this->collectDeprecations(static function () use ($container): void {
$container->get(JWELoaderFactory::class);
$container->get(JWSLoaderFactory::class);
});

static::assertSame([], $deprecations);
}

/**
* @return list<string>
*/
private function collectDeprecations(callable $callback): array
{
$deprecations = [];
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
$deprecations[] = $errstr;

return true;
}, E_USER_DEPRECATED);

try {
$callback();
} finally {
restore_error_handler();
}

return $deprecations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Component\Encryption;

use Jose\Component\Checker\HeaderCheckerManagerFactory;
use Jose\Component\Encryption\JWELoaderFactory;
use PHPUnit\Framework\Attributes\Test;
use function restore_error_handler;
use function set_error_handler;
use const E_USER_DEPRECATED;

/**
* @internal
*/
final class JWELoaderFactoryWithoutHeaderCheckerManagerFactoryTest extends EncryptionTestCase
{
#[Test]
public function aFactoryWithoutHeaderCheckerManagerFactoryIsDeprecated(): void
{
$deprecations = $this->collectDeprecations(function (): void {
new JWELoaderFactory($this->getJWESerializerManagerFactory(), $this->getJWEDecrypterFactory(), null);
});

static::assertSame([
'Since web-token/jwt-library 4.3.0: Creating a "Jose\\Component\\Encryption\\JWELoaderFactory" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "Jose\\Component\\Checker\\HeaderCheckerManagerFactory" that has the "Jose\\Component\\Encryption\\JWETokenSupport" token type support.',
], $deprecations);
}

#[Test]
public function aFactoryWithAHeaderCheckerManagerFactoryIsNotDeprecated(): void
{
$deprecations = $this->collectDeprecations(function (): void {
new JWELoaderFactory($this->getJWESerializerManagerFactory(), $this->getJWEDecrypterFactory(), new HeaderCheckerManagerFactory());
});

static::assertSame([], $deprecations);
}

/**
* @return list<string>
*/
private function collectDeprecations(callable $callback): array
{
$deprecations = [];
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
$deprecations[] = $errstr;

return true;
}, E_USER_DEPRECATED);

try {
$callback();
} finally {
restore_error_handler();
}

return $deprecations;
}
}
9 changes: 4 additions & 5 deletions tests/Component/Encryption/ResultObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ public function theDeprecatedLoaderMethodsStillPopulateTheRecipientIndex(): void
$token = $this->createToken();
$withKey = null;
$withKeySet = null;
$deprecations = $this->collectDeprecations(function () use ($token, &$withKey, &$withKeySet): void {
$this->getLoader()
->loadAndDecryptWithKey($token, $this->getKey(), $withKey);
$this->getLoader()
->loadAndDecryptWithKeySet($token, $this->getKeySet(), $withKeySet);
$loader = $this->getLoader();
$deprecations = $this->collectDeprecations(function () use ($loader, $token, &$withKey, &$withKeySet): void {
$loader->loadAndDecryptWithKey($token, $this->getKey(), $withKey);
$loader->loadAndDecryptWithKeySet($token, $this->getKeySet(), $withKeySet);
});

static::assertSame(0, $withKey);
Expand Down
10 changes: 5 additions & 5 deletions tests/Component/NestedToken/ResultObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ public function onlyThePassedSignatureArgumentIsDeprecated(): void
$encryptionKeySet = new JWKSet([$this->getEncryptionKey()]);
$signatureKeySet = new JWKSet([$this->getSignatureKey()]);
$signature = null;
$deprecations = $this->collectDeprecations(function () use (
$loader = $this->getNestedTokenLoader();
$deprecations = $this->collectDeprecations(static function () use (
$loader,
$token,
$encryptionKeySet,
$signatureKeySet,
&$signature
): void {
$this->getNestedTokenLoader()
->load($token, $encryptionKeySet, $signatureKeySet);
$this->getNestedTokenLoader()
->load($token, $encryptionKeySet, $signatureKeySet, $signature);
$loader->load($token, $encryptionKeySet, $signatureKeySet);
$loader->load($token, $encryptionKeySet, $signatureKeySet, $signature);
});

static::assertSame(0, $signature);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Jose\Tests\Component\Signature;

use Jose\Component\Checker\HeaderCheckerManagerFactory;
use Jose\Component\Signature\JWSLoaderFactory;
use PHPUnit\Framework\Attributes\Test;
use function restore_error_handler;
use function set_error_handler;
use const E_USER_DEPRECATED;

/**
* @internal
*/
final class JWSLoaderFactoryWithoutHeaderCheckerManagerFactoryTest extends SignatureTestCase
{
#[Test]
public function aFactoryWithoutHeaderCheckerManagerFactoryIsDeprecated(): void
{
$deprecations = $this->collectDeprecations(function (): void {
new JWSLoaderFactory($this->getJWSSerializerManagerFactory(), $this->getJWSVerifierFactory(), null);
});

static::assertSame([
'Since web-token/jwt-library 4.3.0: Creating a "Jose\\Component\\Signature\\JWSLoaderFactory" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "Jose\\Component\\Checker\\HeaderCheckerManagerFactory" that has the "Jose\\Component\\Signature\\JWSTokenSupport" token type support.',
], $deprecations);
}

#[Test]
public function aFactoryWithAHeaderCheckerManagerFactoryIsNotDeprecated(): void
{
$deprecations = $this->collectDeprecations(function (): void {
new JWSLoaderFactory($this->getJWSSerializerManagerFactory(), $this->getJWSVerifierFactory(), new HeaderCheckerManagerFactory());
});

static::assertSame([], $deprecations);
}

/**
* @return list<string>
*/
private function collectDeprecations(callable $callback): array
{
$deprecations = [];
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
$deprecations[] = $errstr;

return true;
}, E_USER_DEPRECATED);

try {
$callback();
} finally {
restore_error_handler();
}

return $deprecations;
}
}
9 changes: 4 additions & 5 deletions tests/Component/Signature/ResultObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,10 @@ public function theDeprecatedLoaderMethodsStillPopulateTheSignatureIndex(): void
$token = $this->createToken();
$withKey = null;
$withKeySet = null;
$deprecations = $this->collectDeprecations(function () use ($token, &$withKey, &$withKeySet): void {
$this->getLoader()
->loadAndVerifyWithKey($token, $this->getKey(), $withKey);
$this->getLoader()
->loadAndVerifyWithKeySet($token, $this->getKeySet(), $withKeySet);
$loader = $this->getLoader();
$deprecations = $this->collectDeprecations(function () use ($loader, $token, &$withKey, &$withKeySet): void {
$loader->loadAndVerifyWithKey($token, $this->getKey(), $withKey);
$loader->loadAndVerifyWithKeySet($token, $this->getKeySet(), $withKeySet);
});

static::assertSame(0, $withKey);
Expand Down
Loading