diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d81f91840..c6a60b87d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1245,11 +1245,6 @@ parameters: count: 1 path: src/Library/Console/X5ULoaderCommand.php - - - message: "#^Property Jose\\\\Component\\\\Core\\\\AlgorithmManager\\:\\:\\$algorithms type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Library/Core/AlgorithmManager.php - - message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#" count: 1 diff --git a/src/Bundle/Services/JWEBuilder.php b/src/Bundle/Services/JWEBuilder.php index aef12e2ac..03e386d96 100644 --- a/src/Bundle/Services/JWEBuilder.php +++ b/src/Bundle/Services/JWEBuilder.php @@ -16,12 +16,12 @@ final class JWEBuilder extends BaseJWEBuilder { public function __construct( - AlgorithmManager $keyEncryptionKeyEncryptionAlgorithmManager, - AlgorithmManager $contentEncryptionAlgorithmManager, + AlgorithmManager $algorithmManager, + null|AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager, private readonly EventDispatcherInterface $eventDispatcher ) { - parent::__construct($keyEncryptionKeyEncryptionAlgorithmManager, $contentEncryptionAlgorithmManager, $compressionManager); + parent::__construct($algorithmManager, $contentEncryptionAlgorithmManager, $compressionManager); } public function build(): JWE diff --git a/src/Bundle/Services/JWEBuilderFactory.php b/src/Bundle/Services/JWEBuilderFactory.php index ce71f7634..b956499b4 100644 --- a/src/Bundle/Services/JWEBuilderFactory.php +++ b/src/Bundle/Services/JWEBuilderFactory.php @@ -10,7 +10,7 @@ final class JWEBuilderFactory { - public function __construct( + public function __construct(ush private readonly AlgorithmManagerFactory $algorithmManagerFactory, private readonly CompressionMethodManagerFactory $compressionMethodManagerFactory, private readonly EventDispatcherInterface $eventDispatcher @@ -21,23 +21,19 @@ public function __construct( * This method creates a JWEBuilder using the given algorithm aliases. * * @param string[] $keyEncryptionAlgorithms - * @param string[] $contentEncryptionAlgorithm + * @param string[] $contentEncryptionAlgorithms * @param string[] $compressionMethods */ public function create( array $keyEncryptionAlgorithms, - array $contentEncryptionAlgorithm, + array $contentEncryptionAlgorithms, array $compressionMethods ): JWEBuilder { - $keyEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($keyEncryptionAlgorithms); - $contentEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($contentEncryptionAlgorithm); + $algorithmManager = $this->algorithmManagerFactory->create( + array_merge($keyEncryptionAlgorithms, $contentEncryptionAlgorithms) + ); $compressionMethodManager = $this->compressionMethodManagerFactory->create($compressionMethods); - return new JWEBuilder( - $keyEncryptionAlgorithmManager, - $contentEncryptionAlgorithmManager, - $compressionMethodManager, - $this->eventDispatcher - ); + return new JWEBuilder($algorithmManager, null, $compressionMethodManager, $this->eventDispatcher); } } diff --git a/src/Library/Core/AlgorithmManager.php b/src/Library/Core/AlgorithmManager.php index 97bb9a7db..147921b5e 100644 --- a/src/Library/Core/AlgorithmManager.php +++ b/src/Library/Core/AlgorithmManager.php @@ -9,6 +9,9 @@ class AlgorithmManager { + /** + * @var array + */ private array $algorithms = []; /** @@ -31,6 +34,14 @@ public function has(string $algorithm): bool return array_key_exists($algorithm, $this->algorithms); } + /** + * @return array + */ + public function all(): array + { + return $this->algorithms; + } + /** * Returns the list of names of supported algorithms. * diff --git a/src/Library/Encryption/JWEBuilder.php b/src/Library/Encryption/JWEBuilder.php index 5a0504ec0..73827c9c5 100644 --- a/src/Library/Encryption/JWEBuilder.php +++ b/src/Library/Encryption/JWEBuilder.php @@ -45,11 +45,37 @@ class JWEBuilder private ?ContentEncryptionAlgorithm $contentEncryptionAlgorithm = null; + private readonly AlgorithmManager $keyEncryptionAlgorithmManager; + + private readonly AlgorithmManager $contentEncryptionAlgorithmManager; + public function __construct( - private readonly AlgorithmManager $keyEncryptionAlgorithmManager, - private readonly AlgorithmManager $contentEncryptionAlgorithmManager, + AlgorithmManager $algorithmManager, + null|AlgorithmManager $contentEncryptionAlgorithmManager, private readonly CompressionMethodManager $compressionManager ) { + if ($contentEncryptionAlgorithmManager !== null) { + trigger_deprecation( + 'web-token/jwt-library', + '3.3.0', + 'The parameter "$contentEncryptionAlgorithmManager" is deprecated and will be removed in 4.0.0. Please set all algorithms in the first argument and set "null" instead.' + ); + $this->keyEncryptionAlgorithmManager = $algorithmManager; + $this->contentEncryptionAlgorithmManager = $contentEncryptionAlgorithmManager; + } else { + $keyEncryptionAlgorithms = []; + $contentEncryptionAlgorithms = []; + foreach ($algorithmManager->all() as $algorithm) { + if ($algorithm instanceof KeyEncryptionAlgorithm) { + $keyEncryptionAlgorithms[] = $algorithm; + } + if ($algorithm instanceof ContentEncryptionAlgorithm) { + $contentEncryptionAlgorithms[] = $algorithm; + } + } + $this->keyEncryptionAlgorithmManager = new AlgorithmManager($keyEncryptionAlgorithms); + $this->contentEncryptionAlgorithmManager = new AlgorithmManager($contentEncryptionAlgorithms); + } } /** diff --git a/src/Library/Encryption/JWEDecrypter.php b/src/Library/Encryption/JWEDecrypter.php index 5dd5e4b7f..e688112ef 100644 --- a/src/Library/Encryption/JWEDecrypter.php +++ b/src/Library/Encryption/JWEDecrypter.php @@ -24,11 +24,37 @@ class JWEDecrypter { + private readonly AlgorithmManager $keyEncryptionAlgorithmManager; + + private readonly AlgorithmManager $contentEncryptionAlgorithmManager; + public function __construct( - private readonly AlgorithmManager $keyEncryptionAlgorithmManager, - private readonly AlgorithmManager $contentEncryptionAlgorithmManager, + AlgorithmManager $algorithmManager, + null|AlgorithmManager $contentEncryptionAlgorithmManager, private readonly CompressionMethodManager $compressionMethodManager ) { + if ($contentEncryptionAlgorithmManager !== null) { + trigger_deprecation( + 'web-token/jwt-library', + '3.3.0', + 'The parameter "$contentEncryptionAlgorithmManager" is deprecated and will be removed in 4.0.0. Please set all algorithms in the first argument and set "null" instead.' + ); + $this->keyEncryptionAlgorithmManager = $algorithmManager; + $this->contentEncryptionAlgorithmManager = $contentEncryptionAlgorithmManager; + } else { + $keyEncryptionAlgorithms = []; + $contentEncryptionAlgorithms = []; + foreach ($algorithmManager->all() as $key => $algorithm) { + if ($algorithm instanceof KeyEncryptionAlgorithm) { + $keyEncryptionAlgorithms[$key] = $algorithm; + } + if ($algorithm instanceof ContentEncryptionAlgorithm) { + $contentEncryptionAlgorithms[$key] = $algorithm; + } + } + $this->keyEncryptionAlgorithmManager = new AlgorithmManager($keyEncryptionAlgorithms); + $this->contentEncryptionAlgorithmManager = new AlgorithmManager($contentEncryptionAlgorithms); + } } /** diff --git a/src/Library/Encryption/JWEDecrypterFactory.php b/src/Library/Encryption/JWEDecrypterFactory.php index 074b756f0..a61ef3bae 100644 --- a/src/Library/Encryption/JWEDecrypterFactory.php +++ b/src/Library/Encryption/JWEDecrypterFactory.php @@ -28,14 +28,11 @@ public function create( array $contentEncryptionAlgorithms, array $compressionMethods ): JWEDecrypter { - $keyEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($keyEncryptionAlgorithms); - $contentEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($contentEncryptionAlgorithms); + $algorithmManager = $this->algorithmManagerFactory->create( + array_merge($keyEncryptionAlgorithms, $contentEncryptionAlgorithms) + ); $compressionMethodManager = $this->compressionMethodManagerFactory->create($compressionMethods); - return new JWEDecrypter( - $keyEncryptionAlgorithmManager, - $contentEncryptionAlgorithmManager, - $compressionMethodManager - ); + return new JWEDecrypter($algorithmManager, null, $compressionMethodManager); } }