Skip to content

Commit ec0a071

Browse files
committed
Simplified Algorithm Manager for Encryption/Decryption
1 parent 4e2678a commit ec0a071

File tree

7 files changed

+80
-29
lines changed

7 files changed

+80
-29
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,11 +1245,6 @@ parameters:
12451245
count: 1
12461246
path: src/Library/Console/X5ULoaderCommand.php
12471247

1248-
-
1249-
message: "#^Property Jose\\\\Component\\\\Core\\\\AlgorithmManager\\:\\:\\$algorithms type has no value type specified in iterable type array\\.$#"
1250-
count: 1
1251-
path: src/Library/Core/AlgorithmManager.php
1252-
12531248
-
12541249
message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#"
12551250
count: 1

src/Bundle/Services/JWEBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
final class JWEBuilder extends BaseJWEBuilder
1717
{
1818
public function __construct(
19-
AlgorithmManager $keyEncryptionKeyEncryptionAlgorithmManager,
20-
AlgorithmManager $contentEncryptionAlgorithmManager,
19+
AlgorithmManager $algorithmManager,
20+
null|AlgorithmManager $contentEncryptionAlgorithmManager,
2121
CompressionMethodManager $compressionManager,
2222
private readonly EventDispatcherInterface $eventDispatcher
2323
) {
24-
parent::__construct($keyEncryptionKeyEncryptionAlgorithmManager, $contentEncryptionAlgorithmManager, $compressionManager);
24+
parent::__construct($algorithmManager, $contentEncryptionAlgorithmManager, $compressionManager);
2525
}
2626

2727
public function build(): JWE

src/Bundle/Services/JWEBuilderFactory.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,19 @@ public function __construct(
2121
* This method creates a JWEBuilder using the given algorithm aliases.
2222
*
2323
* @param string[] $keyEncryptionAlgorithms
24-
* @param string[] $contentEncryptionAlgorithm
24+
* @param string[] $contentEncryptionAlgorithms
2525
* @param string[] $compressionMethods
2626
*/
2727
public function create(
2828
array $keyEncryptionAlgorithms,
29-
array $contentEncryptionAlgorithm,
29+
array $contentEncryptionAlgorithms,
3030
array $compressionMethods
3131
): JWEBuilder {
32-
$keyEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($keyEncryptionAlgorithms);
33-
$contentEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($contentEncryptionAlgorithm);
32+
$algorithmManager = $this->algorithmManagerFactory->create(
33+
array_merge($keyEncryptionAlgorithms, $contentEncryptionAlgorithms)
34+
);
3435
$compressionMethodManager = $this->compressionMethodManagerFactory->create($compressionMethods);
3536

36-
return new JWEBuilder(
37-
$keyEncryptionAlgorithmManager,
38-
$contentEncryptionAlgorithmManager,
39-
$compressionMethodManager,
40-
$this->eventDispatcher
41-
);
37+
return new JWEBuilder($algorithmManager, null, $compressionMethodManager, $this->eventDispatcher);
4238
}
4339
}

src/Library/Core/AlgorithmManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
class AlgorithmManager
1111
{
12+
/**
13+
* @var array<string, Algorithm>
14+
*/
1215
private array $algorithms = [];
1316

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

37+
/**
38+
* @return array<string, Algorithm>
39+
*/
40+
public function all(): array
41+
{
42+
return $this->algorithms;
43+
}
44+
3445
/**
3546
* Returns the list of names of supported algorithms.
3647
*

src/Library/Encryption/JWEBuilder.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,37 @@ class JWEBuilder
4545

4646
private ?ContentEncryptionAlgorithm $contentEncryptionAlgorithm = null;
4747

48+
private readonly AlgorithmManager $keyEncryptionAlgorithmManager;
49+
50+
private readonly AlgorithmManager $contentEncryptionAlgorithmManager;
51+
4852
public function __construct(
49-
private readonly AlgorithmManager $keyEncryptionAlgorithmManager,
50-
private readonly AlgorithmManager $contentEncryptionAlgorithmManager,
53+
AlgorithmManager $algorithmManager,
54+
null|AlgorithmManager $contentEncryptionAlgorithmManager,
5155
private readonly CompressionMethodManager $compressionManager
5256
) {
57+
if ($contentEncryptionAlgorithmManager !== null) {
58+
trigger_deprecation(
59+
'web-token/jwt-library',
60+
'3.3.0',
61+
'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.'
62+
);
63+
$this->keyEncryptionAlgorithmManager = $algorithmManager;
64+
$this->contentEncryptionAlgorithmManager = $contentEncryptionAlgorithmManager;
65+
} else {
66+
$keyEncryptionAlgorithms = [];
67+
$contentEncryptionAlgorithms = [];
68+
foreach ($algorithmManager->all() as $algorithm) {
69+
if ($algorithm instanceof KeyEncryptionAlgorithm) {
70+
$keyEncryptionAlgorithms[] = $algorithm;
71+
}
72+
if ($algorithm instanceof ContentEncryptionAlgorithm) {
73+
$contentEncryptionAlgorithms[] = $algorithm;
74+
}
75+
}
76+
$this->keyEncryptionAlgorithmManager = new AlgorithmManager($keyEncryptionAlgorithms);
77+
$this->contentEncryptionAlgorithmManager = new AlgorithmManager($contentEncryptionAlgorithms);
78+
}
5379
}
5480

5581
/**

src/Library/Encryption/JWEDecrypter.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,37 @@
2424

2525
class JWEDecrypter
2626
{
27+
private readonly AlgorithmManager $keyEncryptionAlgorithmManager;
28+
29+
private readonly AlgorithmManager $contentEncryptionAlgorithmManager;
30+
2731
public function __construct(
28-
private readonly AlgorithmManager $keyEncryptionAlgorithmManager,
29-
private readonly AlgorithmManager $contentEncryptionAlgorithmManager,
32+
AlgorithmManager $algorithmManager,
33+
null|AlgorithmManager $contentEncryptionAlgorithmManager,
3034
private readonly CompressionMethodManager $compressionMethodManager
3135
) {
36+
if ($contentEncryptionAlgorithmManager !== null) {
37+
trigger_deprecation(
38+
'web-token/jwt-library',
39+
'3.3.0',
40+
'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.'
41+
);
42+
$this->keyEncryptionAlgorithmManager = $algorithmManager;
43+
$this->contentEncryptionAlgorithmManager = $contentEncryptionAlgorithmManager;
44+
} else {
45+
$keyEncryptionAlgorithms = [];
46+
$contentEncryptionAlgorithms = [];
47+
foreach ($algorithmManager->all() as $key => $algorithm) {
48+
if ($algorithm instanceof KeyEncryptionAlgorithm) {
49+
$keyEncryptionAlgorithms[$key] = $algorithm;
50+
}
51+
if ($algorithm instanceof ContentEncryptionAlgorithm) {
52+
$contentEncryptionAlgorithms[$key] = $algorithm;
53+
}
54+
}
55+
$this->keyEncryptionAlgorithmManager = new AlgorithmManager($keyEncryptionAlgorithms);
56+
$this->contentEncryptionAlgorithmManager = new AlgorithmManager($contentEncryptionAlgorithms);
57+
}
3258
}
3359

3460
/**

src/Library/Encryption/JWEDecrypterFactory.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ public function create(
2828
array $contentEncryptionAlgorithms,
2929
array $compressionMethods
3030
): JWEDecrypter {
31-
$keyEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($keyEncryptionAlgorithms);
32-
$contentEncryptionAlgorithmManager = $this->algorithmManagerFactory->create($contentEncryptionAlgorithms);
31+
$algorithmManager = $this->algorithmManagerFactory->create(
32+
array_merge($keyEncryptionAlgorithms, $contentEncryptionAlgorithms)
33+
);
3334
$compressionMethodManager = $this->compressionMethodManagerFactory->create($compressionMethods);
3435

35-
return new JWEDecrypter(
36-
$keyEncryptionAlgorithmManager,
37-
$contentEncryptionAlgorithmManager,
38-
$compressionMethodManager
39-
);
36+
return new JWEDecrypter($algorithmManager, null, $compressionMethodManager);
4037
}
4138
}

0 commit comments

Comments
 (0)