1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 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\Core\AlgorithmInterface; |
17
|
|
|
use Jose\Component\Core\AlgorithmManagerFactory; |
18
|
|
|
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithmInterface; |
19
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryptionAlgorithmInterface; |
20
|
|
|
use Jose\Component\Encryption\Compression\CompressionMethodManagerFactory; |
21
|
|
|
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory; |
22
|
|
|
use Jose\Component\Signature\Algorithm\SignatureAlgorithmInterface; |
23
|
|
|
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
27
|
|
|
|
28
|
|
|
final class JoseCollector extends DataCollector |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var AlgorithmManagerFactory |
32
|
|
|
*/ |
33
|
|
|
private $algorithmManagerFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var CompressionMethodManagerFactory|null |
37
|
|
|
*/ |
38
|
|
|
private $compressionMethodManagerFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var JWSSerializerManagerFactory|null |
42
|
|
|
*/ |
43
|
|
|
private $jwsSerializerManagerFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var JWESerializerManagerFactory|null |
47
|
|
|
*/ |
48
|
|
|
private $jweSerializerManagerFactory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* JoseCollector constructor. |
52
|
|
|
* |
53
|
|
|
* @param AlgorithmManagerFactory $algorithmManagerFactory |
54
|
|
|
* @param CompressionMethodManagerFactory|null $compressionMethodManagerFactory |
55
|
|
|
* @param JWSSerializerManagerFactory|null $jwsSerializerManagerFactory |
56
|
|
|
* @param JWESerializerManagerFactory|null $jweSerializerManagerFactory |
57
|
|
|
*/ |
58
|
|
|
public function __construct(AlgorithmManagerFactory $algorithmManagerFactory, ?CompressionMethodManagerFactory $compressionMethodManagerFactory = null, ?JWSSerializerManagerFactory $jwsSerializerManagerFactory = null, ?JWESerializerManagerFactory $jweSerializerManagerFactory = null) |
59
|
|
|
{ |
60
|
|
|
$this->data = []; |
61
|
|
|
$this->algorithmManagerFactory = $algorithmManagerFactory; |
62
|
|
|
$this->compressionMethodManagerFactory = $compressionMethodManagerFactory; |
63
|
|
|
$this->jwsSerializerManagerFactory = $jwsSerializerManagerFactory; |
64
|
|
|
$this->jweSerializerManagerFactory = $jweSerializerManagerFactory; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
71
|
|
|
{ |
72
|
|
|
$this->collectSupportedAlgorithms(); |
73
|
|
|
$this->collectSupportedCompressionMethods(); |
74
|
|
|
$this->collectSupportedJWSSerializations(); |
75
|
|
|
$this->collectSupportedJWESerializations(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getAlgorithmDetails(): array |
82
|
|
|
{ |
83
|
|
|
return $this->data['algorithms']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function countSignatureAlgorithms(): int |
90
|
|
|
{ |
91
|
|
|
return $this->data['types']['signature']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function countKeyEncryptionAlgorithms(): int |
98
|
|
|
{ |
99
|
|
|
return $this->data['types']['key_encryption']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function countContentEncryptionAlgorithms(): int |
106
|
|
|
{ |
107
|
|
|
return $this->data['types']['content_encryption']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getCompressionMethodDetails(): array |
114
|
|
|
{ |
115
|
|
|
return $this->data['compression_methods']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getJWSSerializationDetails(): array |
122
|
|
|
{ |
123
|
|
|
return $this->data['jws_serialization']; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getJWESerializationDetails(): array |
130
|
|
|
{ |
131
|
|
|
return $this->data['jwe_serialization']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function getName() |
138
|
|
|
{ |
139
|
|
|
return 'jose_collector'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
*/ |
145
|
|
|
private function collectSupportedAlgorithms() |
146
|
|
|
{ |
147
|
|
|
$algorithms = $this->algorithmManagerFactory->all(); |
148
|
|
|
$this->data['algorithms'] = []; |
149
|
|
|
$signatureAlgorithms = 0; |
150
|
|
|
$keyEncryptionAlgorithms = 0; |
151
|
|
|
$contentEncryptionAlgorithms = 0; |
152
|
|
|
foreach ($algorithms as $alias => $algorithm) { |
153
|
|
|
$type = $this->getAlgorithmType($algorithm, $signatureAlgorithms, $keyEncryptionAlgorithms, $contentEncryptionAlgorithms); |
154
|
|
|
if (!array_key_exists($type, $this->data['algorithms'])) { |
155
|
|
|
$this->data['algorithms'][$type] = []; |
156
|
|
|
} |
157
|
|
|
$this->data['algorithms'][$type][$alias] = [ |
158
|
|
|
'name' => $algorithm->name(), |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->data['types'] = [ |
163
|
|
|
'signature' => $signatureAlgorithms, |
164
|
|
|
'key_encryption' => $keyEncryptionAlgorithms, |
165
|
|
|
'content_encryption' => $contentEncryptionAlgorithms, |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param AlgorithmInterface $algorithm |
171
|
|
|
* @param int $signatureAlgorithms |
172
|
|
|
* @param int $keyEncryptionAlgorithms |
173
|
|
|
* @param int $contentEncryptionAlgorithms |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
private function getAlgorithmType(AlgorithmInterface $algorithm, int &$signatureAlgorithms, int &$keyEncryptionAlgorithms, int &$contentEncryptionAlgorithms): string |
178
|
|
|
{ |
179
|
|
|
switch (true) { |
180
|
|
|
case $algorithm instanceof SignatureAlgorithmInterface: |
181
|
|
|
$signatureAlgorithms++; |
182
|
|
|
|
183
|
|
|
return 'Signature'; |
184
|
|
|
case $algorithm instanceof KeyEncryptionAlgorithmInterface: |
185
|
|
|
$keyEncryptionAlgorithms++; |
186
|
|
|
|
187
|
|
|
return 'Key Encryption'; |
188
|
|
|
case $algorithm instanceof ContentEncryptionAlgorithmInterface: |
189
|
|
|
$contentEncryptionAlgorithms++; |
190
|
|
|
|
191
|
|
|
return 'Content Encryption'; |
192
|
|
|
default: |
193
|
|
|
return 'Unknown'; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function collectSupportedCompressionMethods() |
198
|
|
|
{ |
199
|
|
|
$this->data['compression_methods'] = []; |
200
|
|
|
if (null === $this->compressionMethodManagerFactory) { |
201
|
|
|
return; |
202
|
|
|
} |
203
|
|
|
$compressionMethods = $this->compressionMethodManagerFactory->all(); |
204
|
|
|
foreach ($compressionMethods as $alias => $compressionMethod) { |
205
|
|
|
$this->data['compression_methods'][$alias] = $compressionMethod->name(); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
private function collectSupportedJWSSerializations() |
210
|
|
|
{ |
211
|
|
|
$this->data['jws_serialization'] = []; |
212
|
|
|
if (null === $this->jwsSerializerManagerFactory) { |
213
|
|
|
return; |
214
|
|
|
} |
215
|
|
|
$serializers = $this->jwsSerializerManagerFactory->all(); |
216
|
|
|
foreach ($serializers as $serializer) { |
217
|
|
|
$this->data['jws_serialization'][$serializer->name()] = $serializer->displayName(); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
private function collectSupportedJWESerializations() |
222
|
|
|
{ |
223
|
|
|
$this->data['jwe_serialization'] = []; |
224
|
|
|
if (null === $this->jweSerializerManagerFactory) { |
225
|
|
|
return; |
226
|
|
|
} |
227
|
|
|
$serializers = $this->jweSerializerManagerFactory->all(); |
228
|
|
|
foreach ($serializers as $serializer) { |
229
|
|
|
$this->data['jwe_serialization'][$serializer->name()] = $serializer->displayName(); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|