Skip to content

Commit

Permalink
Simplified Algorithm Manager for Encryption/Decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Feb 22, 2024
1 parent 4e2678a commit de4ac90
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 30 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Bundle/Services/JWEBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 7 additions & 11 deletions src/Bundle/Services/JWEBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
11 changes: 11 additions & 0 deletions src/Library/Core/AlgorithmManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class AlgorithmManager
{
/**
* @var array<string, Algorithm>
*/
private array $algorithms = [];

/**
Expand All @@ -31,6 +34,14 @@ public function has(string $algorithm): bool
return array_key_exists($algorithm, $this->algorithms);
}

/**
* @return array<string, Algorithm>
*/
public function all(): array
{
return $this->algorithms;
}

/**
* Returns the list of names of supported algorithms.
*
Expand Down
30 changes: 28 additions & 2 deletions src/Library/Encryption/JWEBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
30 changes: 28 additions & 2 deletions src/Library/Encryption/JWEDecrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Library/Encryption/JWEDecrypterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit de4ac90

Please sign in to comment.