Skip to content

Commit 5d3230b

Browse files
authored
Merge pull request #788 from web-token/fix/loader-factories-require-header-checker-manager-factory
fix: deprecate loader factories without a header checker manager factory
2 parents 72191a5 + 71278ff commit 5d3230b

10 files changed

Lines changed: 275 additions & 15 deletions

‎src/Bundle/Services/JWELoaderFactory.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace Jose\Bundle\JoseFramework\Services;
66

77
use Jose\Component\Core\Exception\InvalidArgumentException;
8+
use Jose\Component\Encryption\JWETokenSupport;
89
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
910
use Psr\EventDispatcher\EventDispatcherInterface;
1011
use function func_get_arg;
1112
use function func_num_args;
13+
use function trigger_deprecation;
1214

1315
final readonly class JWELoaderFactory
1416
{
@@ -18,6 +20,16 @@ public function __construct(
1820
private ?HeaderCheckerManagerFactory $headerCheckerManagerFactory,
1921
private EventDispatcherInterface $eventDispatcher
2022
) {
23+
if ($headerCheckerManagerFactory === null) {
24+
trigger_deprecation(
25+
'web-token/jwt-framework',
26+
'4.3.0',
27+
'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.',
28+
self::class,
29+
HeaderCheckerManagerFactory::class,
30+
JWETokenSupport::class
31+
);
32+
}
2133
}
2234

2335
/**

‎src/Bundle/Services/JWSLoaderFactory.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace Jose\Bundle\JoseFramework\Services;
66

77
use Jose\Component\Core\Exception\InvalidArgumentException;
8+
use Jose\Component\Signature\JWSTokenSupport;
89
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory;
910
use Psr\EventDispatcher\EventDispatcherInterface;
1011
use function func_get_arg;
1112
use function func_num_args;
13+
use function trigger_deprecation;
1214

1315
final readonly class JWSLoaderFactory
1416
{
@@ -18,6 +20,16 @@ public function __construct(
1820
private ?HeaderCheckerManagerFactory $headerCheckerManagerFactory,
1921
private EventDispatcherInterface $eventDispatcher
2022
) {
23+
if ($headerCheckerManagerFactory === null) {
24+
trigger_deprecation(
25+
'web-token/jwt-framework',
26+
'4.3.0',
27+
'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.',
28+
self::class,
29+
HeaderCheckerManagerFactory::class,
30+
JWSTokenSupport::class
31+
);
32+
}
2133
}
2234

2335
/**

‎src/Library/Encryption/JWELoaderFactory.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
1010
use function func_get_arg;
1111
use function func_num_args;
12+
use function trigger_deprecation;
1213

1314
readonly class JWELoaderFactory
1415
{
@@ -17,6 +18,16 @@ public function __construct(
1718
private JWEDecrypterFactory $jweDecrypterFactory,
1819
private ?HeaderCheckerManagerFactory $headerCheckerManagerFactory
1920
) {
21+
if ($headerCheckerManagerFactory === null) {
22+
trigger_deprecation(
23+
'web-token/jwt-library',
24+
'4.3.0',
25+
'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.',
26+
static::class,
27+
HeaderCheckerManagerFactory::class,
28+
JWETokenSupport::class
29+
);
30+
}
2031
}
2132

2233
/**

‎src/Library/Signature/JWSLoaderFactory.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory;
1010
use function func_get_arg;
1111
use function func_num_args;
12+
use function trigger_deprecation;
1213

1314
class JWSLoaderFactory
1415
{
@@ -17,6 +18,16 @@ public function __construct(
1718
private readonly JWSVerifierFactory $jwsVerifierFactory,
1819
private readonly ?HeaderCheckerManagerFactory $headerCheckerManagerFactory
1920
) {
21+
if ($headerCheckerManagerFactory === null) {
22+
trigger_deprecation(
23+
'web-token/jwt-library',
24+
'4.3.0',
25+
'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.',
26+
static::class,
27+
HeaderCheckerManagerFactory::class,
28+
JWSTokenSupport::class
29+
);
30+
}
2031
}
2132

2233
/**
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Tests\Bundle\JoseFramework\Functional;
6+
7+
use Jose\Bundle\JoseFramework\Services\HeaderCheckerManagerFactory;
8+
use Jose\Bundle\JoseFramework\Services\JWEDecrypterFactory;
9+
use Jose\Bundle\JoseFramework\Services\JWELoaderFactory;
10+
use Jose\Bundle\JoseFramework\Services\JWSLoaderFactory;
11+
use Jose\Bundle\JoseFramework\Services\JWSVerifierFactory;
12+
use Jose\Component\Core\AlgorithmManagerFactory;
13+
use Jose\Component\Encryption\JWETokenSupport;
14+
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
15+
use Jose\Component\Signature\JWSTokenSupport;
16+
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory;
17+
use Jose\Tests\Bundle\JoseFramework\WebTestCase;
18+
use PHPUnit\Framework\Attributes\Test;
19+
use Symfony\Component\EventDispatcher\EventDispatcher;
20+
use function restore_error_handler;
21+
use function set_error_handler;
22+
use function sprintf;
23+
use const E_USER_DEPRECATED;
24+
25+
/**
26+
* @internal
27+
*/
28+
final class LoaderFactoriesWithoutHeaderCheckerManagerFactoryTest extends WebTestCase
29+
{
30+
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.';
31+
32+
#[Test]
33+
public function theLoaderFactoriesWithoutHeaderCheckerManagerFactoryAreDeprecated(): void
34+
{
35+
$eventDispatcher = new EventDispatcher();
36+
$algorithmManagerFactory = new AlgorithmManagerFactory([]);
37+
$jweSerializerManagerFactory = new JWESerializerManagerFactory();
38+
$jweDecrypterFactory = new JWEDecrypterFactory($algorithmManagerFactory, $eventDispatcher);
39+
$jwsSerializerManagerFactory = new JWSSerializerManagerFactory();
40+
$jwsVerifierFactory = new JWSVerifierFactory($algorithmManagerFactory, $eventDispatcher);
41+
42+
$deprecations = $this->collectDeprecations(static function () use (
43+
$eventDispatcher,
44+
$jweSerializerManagerFactory,
45+
$jweDecrypterFactory,
46+
$jwsSerializerManagerFactory,
47+
$jwsVerifierFactory
48+
): void {
49+
new JWELoaderFactory($jweSerializerManagerFactory, $jweDecrypterFactory, null, $eventDispatcher);
50+
new JWSLoaderFactory($jwsSerializerManagerFactory, $jwsVerifierFactory, null, $eventDispatcher);
51+
});
52+
53+
static::assertSame([
54+
sprintf(self::MESSAGE, JWELoaderFactory::class, HeaderCheckerManagerFactory::class, JWETokenSupport::class),
55+
sprintf(self::MESSAGE, JWSLoaderFactory::class, HeaderCheckerManagerFactory::class, JWSTokenSupport::class),
56+
], $deprecations);
57+
}
58+
59+
#[Test]
60+
public function theLoaderFactoriesOfTheContainerAreNotDeprecated(): void
61+
{
62+
static::ensureKernelShutdown();
63+
$client = static::createClient();
64+
$container = $client->getContainer();
65+
66+
$deprecations = $this->collectDeprecations(static function () use ($container): void {
67+
$container->get(JWELoaderFactory::class);
68+
$container->get(JWSLoaderFactory::class);
69+
});
70+
71+
static::assertSame([], $deprecations);
72+
}
73+
74+
/**
75+
* @return list<string>
76+
*/
77+
private function collectDeprecations(callable $callback): array
78+
{
79+
$deprecations = [];
80+
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
81+
$deprecations[] = $errstr;
82+
83+
return true;
84+
}, E_USER_DEPRECATED);
85+
86+
try {
87+
$callback();
88+
} finally {
89+
restore_error_handler();
90+
}
91+
92+
return $deprecations;
93+
}
94+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Tests\Component\Encryption;
6+
7+
use Jose\Component\Checker\HeaderCheckerManagerFactory;
8+
use Jose\Component\Encryption\JWELoaderFactory;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use function restore_error_handler;
11+
use function set_error_handler;
12+
use const E_USER_DEPRECATED;
13+
14+
/**
15+
* @internal
16+
*/
17+
final class JWELoaderFactoryWithoutHeaderCheckerManagerFactoryTest extends EncryptionTestCase
18+
{
19+
#[Test]
20+
public function aFactoryWithoutHeaderCheckerManagerFactoryIsDeprecated(): void
21+
{
22+
$deprecations = $this->collectDeprecations(function (): void {
23+
new JWELoaderFactory($this->getJWESerializerManagerFactory(), $this->getJWEDecrypterFactory(), null);
24+
});
25+
26+
static::assertSame([
27+
'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.',
28+
], $deprecations);
29+
}
30+
31+
#[Test]
32+
public function aFactoryWithAHeaderCheckerManagerFactoryIsNotDeprecated(): void
33+
{
34+
$deprecations = $this->collectDeprecations(function (): void {
35+
new JWELoaderFactory($this->getJWESerializerManagerFactory(), $this->getJWEDecrypterFactory(), new HeaderCheckerManagerFactory());
36+
});
37+
38+
static::assertSame([], $deprecations);
39+
}
40+
41+
/**
42+
* @return list<string>
43+
*/
44+
private function collectDeprecations(callable $callback): array
45+
{
46+
$deprecations = [];
47+
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
48+
$deprecations[] = $errstr;
49+
50+
return true;
51+
}, E_USER_DEPRECATED);
52+
53+
try {
54+
$callback();
55+
} finally {
56+
restore_error_handler();
57+
}
58+
59+
return $deprecations;
60+
}
61+
}

‎tests/Component/Encryption/ResultObjectsTest.php‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,10 @@ public function theDeprecatedLoaderMethodsStillPopulateTheRecipientIndex(): void
170170
$token = $this->createToken();
171171
$withKey = null;
172172
$withKeySet = null;
173-
$deprecations = $this->collectDeprecations(function () use ($token, &$withKey, &$withKeySet): void {
174-
$this->getLoader()
175-
->loadAndDecryptWithKey($token, $this->getKey(), $withKey);
176-
$this->getLoader()
177-
->loadAndDecryptWithKeySet($token, $this->getKeySet(), $withKeySet);
173+
$loader = $this->getLoader();
174+
$deprecations = $this->collectDeprecations(function () use ($loader, $token, &$withKey, &$withKeySet): void {
175+
$loader->loadAndDecryptWithKey($token, $this->getKey(), $withKey);
176+
$loader->loadAndDecryptWithKeySet($token, $this->getKeySet(), $withKeySet);
178177
});
179178

180179
static::assertSame(0, $withKey);

‎tests/Component/NestedToken/ResultObjectsTest.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ public function onlyThePassedSignatureArgumentIsDeprecated(): void
5454
$encryptionKeySet = new JWKSet([$this->getEncryptionKey()]);
5555
$signatureKeySet = new JWKSet([$this->getSignatureKey()]);
5656
$signature = null;
57-
$deprecations = $this->collectDeprecations(function () use (
57+
$loader = $this->getNestedTokenLoader();
58+
$deprecations = $this->collectDeprecations(static function () use (
59+
$loader,
5860
$token,
5961
$encryptionKeySet,
6062
$signatureKeySet,
6163
&$signature
6264
): void {
63-
$this->getNestedTokenLoader()
64-
->load($token, $encryptionKeySet, $signatureKeySet);
65-
$this->getNestedTokenLoader()
66-
->load($token, $encryptionKeySet, $signatureKeySet, $signature);
65+
$loader->load($token, $encryptionKeySet, $signatureKeySet);
66+
$loader->load($token, $encryptionKeySet, $signatureKeySet, $signature);
6767
});
6868

6969
static::assertSame(0, $signature);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jose\Tests\Component\Signature;
6+
7+
use Jose\Component\Checker\HeaderCheckerManagerFactory;
8+
use Jose\Component\Signature\JWSLoaderFactory;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use function restore_error_handler;
11+
use function set_error_handler;
12+
use const E_USER_DEPRECATED;
13+
14+
/**
15+
* @internal
16+
*/
17+
final class JWSLoaderFactoryWithoutHeaderCheckerManagerFactoryTest extends SignatureTestCase
18+
{
19+
#[Test]
20+
public function aFactoryWithoutHeaderCheckerManagerFactoryIsDeprecated(): void
21+
{
22+
$deprecations = $this->collectDeprecations(function (): void {
23+
new JWSLoaderFactory($this->getJWSSerializerManagerFactory(), $this->getJWSVerifierFactory(), null);
24+
});
25+
26+
static::assertSame([
27+
'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.',
28+
], $deprecations);
29+
}
30+
31+
#[Test]
32+
public function aFactoryWithAHeaderCheckerManagerFactoryIsNotDeprecated(): void
33+
{
34+
$deprecations = $this->collectDeprecations(function (): void {
35+
new JWSLoaderFactory($this->getJWSSerializerManagerFactory(), $this->getJWSVerifierFactory(), new HeaderCheckerManagerFactory());
36+
});
37+
38+
static::assertSame([], $deprecations);
39+
}
40+
41+
/**
42+
* @return list<string>
43+
*/
44+
private function collectDeprecations(callable $callback): array
45+
{
46+
$deprecations = [];
47+
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
48+
$deprecations[] = $errstr;
49+
50+
return true;
51+
}, E_USER_DEPRECATED);
52+
53+
try {
54+
$callback();
55+
} finally {
56+
restore_error_handler();
57+
}
58+
59+
return $deprecations;
60+
}
61+
}

‎tests/Component/Signature/ResultObjectsTest.php‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,10 @@ public function theDeprecatedLoaderMethodsStillPopulateTheSignatureIndex(): void
142142
$token = $this->createToken();
143143
$withKey = null;
144144
$withKeySet = null;
145-
$deprecations = $this->collectDeprecations(function () use ($token, &$withKey, &$withKeySet): void {
146-
$this->getLoader()
147-
->loadAndVerifyWithKey($token, $this->getKey(), $withKey);
148-
$this->getLoader()
149-
->loadAndVerifyWithKeySet($token, $this->getKeySet(), $withKeySet);
145+
$loader = $this->getLoader();
146+
$deprecations = $this->collectDeprecations(function () use ($loader, $token, &$withKey, &$withKeySet): void {
147+
$loader->loadAndVerifyWithKey($token, $this->getKey(), $withKey);
148+
$loader->loadAndVerifyWithKeySet($token, $this->getKeySet(), $withKeySet);
150149
});
151150

152151
static::assertSame(0, $withKey);

0 commit comments

Comments
 (0)