1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Bundle\JoseFramework\DataCollector; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Encryption\Compression\CompressionMethodManagerFactory; |
17
|
|
|
use Jose\Component\Encryption\JWEBuilder; |
18
|
|
|
use Jose\Component\Encryption\JWEDecrypter; |
19
|
|
|
use Jose\Component\Encryption\JWELoader; |
20
|
|
|
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
|
24
|
|
|
final class JWECollector implements Collector |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var JWESerializerManagerFactory|null |
28
|
|
|
*/ |
29
|
|
|
private $jweSerializerManagerFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CompressionMethodManagerFactory|null |
33
|
|
|
*/ |
34
|
|
|
private $compressionMethodManagerFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* JWECollector constructor. |
38
|
|
|
* |
39
|
|
|
* @param CompressionMethodManagerFactory|null $compressionMethodManagerFactory |
40
|
|
|
* @param JWESerializerManagerFactory|null $jweSerializerManagerFactory |
41
|
|
|
*/ |
42
|
|
|
public function __construct(?CompressionMethodManagerFactory $compressionMethodManagerFactory = null, ?JWESerializerManagerFactory $jweSerializerManagerFactory = null) |
43
|
|
|
{ |
44
|
|
|
$this->compressionMethodManagerFactory = $compressionMethodManagerFactory; |
45
|
|
|
$this->jweSerializerManagerFactory = $jweSerializerManagerFactory; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function collect(array &$data, Request $request, Response $response, \Exception $exception = null) |
52
|
|
|
{ |
53
|
|
|
$this->collectSupportedCompressionMethods($data); |
54
|
|
|
$this->collectSupportedJWESerializations($data); |
55
|
|
|
$this->collectSupportedJWEBuilders($data); |
56
|
|
|
$this->collectSupportedJWEDecrypters($data); |
57
|
|
|
$this->collectSupportedJWELoaders($data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $data |
62
|
|
|
*/ |
63
|
|
|
private function collectSupportedCompressionMethods(array &$data) |
64
|
|
|
{ |
65
|
|
|
$data['jwe']['compression_methods'] = []; |
66
|
|
|
if (null === $this->compressionMethodManagerFactory) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
$compressionMethods = $this->compressionMethodManagerFactory->all(); |
70
|
|
|
foreach ($compressionMethods as $alias => $compressionMethod) { |
71
|
|
|
$data['jwe']['compression_methods'][$alias] = $compressionMethod->name(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $data |
77
|
|
|
*/ |
78
|
|
|
private function collectSupportedJWESerializations(array &$data) |
79
|
|
|
{ |
80
|
|
|
$data['jwe']['jwe_serialization'] = []; |
81
|
|
|
if (null === $this->jweSerializerManagerFactory) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
$serializers = $this->jweSerializerManagerFactory->all(); |
85
|
|
|
foreach ($serializers as $serializer) { |
86
|
|
|
$data['jwe']['jwe_serialization'][$serializer->name()] = $serializer->displayName(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $data |
92
|
|
|
*/ |
93
|
|
|
private function collectSupportedJWEBuilders(array &$data) |
94
|
|
|
{ |
95
|
|
|
$data['jwe']['jwe_builders'] = []; |
96
|
|
|
foreach ($this->jweBuilders as $id => $jweBuilder) { |
97
|
|
|
$data['jwe']['jwe_builders'][$id] = [ |
98
|
|
|
'key_encryption_algorithms' => $jweBuilder->getKeyEncryptionAlgorithmManager()->list(), |
99
|
|
|
'content_encryption_algorithms' => $jweBuilder->getContentEncryptionAlgorithmManager()->list(), |
100
|
|
|
'compression_methods' => $jweBuilder->getCompressionMethodManager()->list(), |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $data |
107
|
|
|
*/ |
108
|
|
|
private function collectSupportedJWEDecrypters(array &$data) |
109
|
|
|
{ |
110
|
|
|
$data['jwe']['jwe_decrypters'] = []; |
111
|
|
|
foreach ($this->jweDecrypters as $id => $jweDecrypter) { |
112
|
|
|
$data['jwe']['jwe_decrypters'][$id] = [ |
113
|
|
|
'key_encryption_algorithms' => $jweDecrypter->getKeyEncryptionAlgorithmManager()->list(), |
114
|
|
|
'content_encryption_algorithms' => $jweDecrypter->getContentEncryptionAlgorithmManager()->list(), |
115
|
|
|
'compression_methods' => $jweDecrypter->getCompressionMethodManager()->list(), |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $data |
122
|
|
|
*/ |
123
|
|
|
private function collectSupportedJWELoaders(array &$data) |
124
|
|
|
{ |
125
|
|
|
$data['jwe']['jwe_loaders'] = []; |
126
|
|
|
foreach ($this->jweLoaders as $id => $jweLoader) { |
127
|
|
|
$data['jwe']['jwe_loaders'][$id] = [ |
128
|
|
|
'serializers' => $jweLoader->getSerializerManager()->list(), |
129
|
|
|
'key_encryption_algorithms' => $jweLoader->getJweDecrypter()->getKeyEncryptionAlgorithmManager()->list(), |
130
|
|
|
'content_encryption_algorithms' => $jweLoader->getJweDecrypter()->getContentEncryptionAlgorithmManager()->list(), |
131
|
|
|
'compression_methods' => $jweLoader->getJweDecrypter()->getCompressionMethodManager()->list(), |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var JWEBuilder[] |
138
|
|
|
*/ |
139
|
|
|
private $jweBuilders = []; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $id |
143
|
|
|
* @param JWEBuilder $jweBuilder |
144
|
|
|
*/ |
145
|
|
|
public function addJWEBuilder(string $id, JWEBuilder $jweBuilder) |
146
|
|
|
{ |
147
|
|
|
$this->jweBuilders[$id] = $jweBuilder; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @var JWEDecrypter[] |
152
|
|
|
*/ |
153
|
|
|
private $jweDecrypters = []; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $id |
157
|
|
|
* @param JWEDecrypter $jweDecrypter |
158
|
|
|
*/ |
159
|
|
|
public function addJWEDecrypter(string $id, JWEDecrypter $jweDecrypter) |
160
|
|
|
{ |
161
|
|
|
$this->jweDecrypters[$id] = $jweDecrypter; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @var JWELoader[] |
166
|
|
|
*/ |
167
|
|
|
private $jweLoaders = []; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $id |
171
|
|
|
* @param JWELoader $jweLoader |
172
|
|
|
*/ |
173
|
|
|
public function addJWELoader(string $id, JWELoader $jweLoader) |
174
|
|
|
{ |
175
|
|
|
$this->jweLoaders[$id] = $jweLoader; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|