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\Signature\JWSBuilder; |
17
|
|
|
use Jose\Component\Signature\JWSVerifier; |
18
|
|
|
use Jose\Component\Signature\JWSLoader; |
19
|
|
|
use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
|
23
|
|
|
final class JWSCollector implements Collector |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var JWSSerializerManagerFactory|null |
27
|
|
|
*/ |
28
|
|
|
private $jwsSerializerManagerFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* JWSCollector constructor. |
32
|
|
|
* |
33
|
|
|
* @param JWSSerializerManagerFactory|null $jwsSerializerManagerFactory |
34
|
|
|
*/ |
35
|
|
|
public function __construct(?JWSSerializerManagerFactory $jwsSerializerManagerFactory = null) |
36
|
|
|
{ |
37
|
|
|
$this->jwsSerializerManagerFactory = $jwsSerializerManagerFactory; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function collect(array &$data, Request $request, Response $response, \Exception $exception = null) |
44
|
|
|
{ |
45
|
|
|
$this->collectSupportedJWSSerializations($data); |
46
|
|
|
$this->collectSupportedJWSBuilders($data); |
47
|
|
|
$this->collectSupportedJWSVerifiers($data); |
48
|
|
|
$this->collectSupportedJWSLoaders($data); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $data |
53
|
|
|
*/ |
54
|
|
|
private function collectSupportedJWSSerializations(array &$data) |
55
|
|
|
{ |
56
|
|
|
$data['jws']['jws_serialization'] = []; |
57
|
|
|
if (null === $this->jwsSerializerManagerFactory) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
$serializers = $this->jwsSerializerManagerFactory->all(); |
61
|
|
|
foreach ($serializers as $serializer) { |
62
|
|
|
$data['jws']['jws_serialization'][$serializer->name()] = $serializer->displayName(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $data |
68
|
|
|
*/ |
69
|
|
|
private function collectSupportedJWSBuilders(array &$data) |
70
|
|
|
{ |
71
|
|
|
$data['jws']['jws_builders'] = []; |
72
|
|
|
foreach ($this->jwsBuilders as $id => $jwsBuilder) { |
73
|
|
|
$data['jws']['jws_builders'][$id] = [ |
74
|
|
|
'signature_algorithms' => $jwsBuilder->getSignatureAlgorithmManager()->list(), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $data |
81
|
|
|
*/ |
82
|
|
|
private function collectSupportedJWSVerifiers(array &$data) |
83
|
|
|
{ |
84
|
|
|
$data['jws']['jws_verifiers'] = []; |
85
|
|
|
foreach ($this->jwsVerifiers as $id => $jwsVerifier) { |
86
|
|
|
$data['jws']['jws_verifiers'][$id] = [ |
87
|
|
|
'signature_algorithms' => $jwsVerifier->getSignatureAlgorithmManager()->list(), |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $data |
94
|
|
|
*/ |
95
|
|
|
private function collectSupportedJWSLoaders(array &$data) |
96
|
|
|
{ |
97
|
|
|
$data['jws']['jws_loaders'] = []; |
98
|
|
|
foreach ($this->jwsLoaders as $id => $jwsLoader) { |
99
|
|
|
$data['jws']['jws_loaders'][$id] = [ |
100
|
|
|
'serializers' => $jwsLoader->getSerializerManager()->list(), |
101
|
|
|
'signature_algorithms' => $jwsLoader->getJwsVerifier()->getSignatureAlgorithmManager()->list(), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var JWSBuilder[] |
108
|
|
|
*/ |
109
|
|
|
private $jwsBuilders = []; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $id |
113
|
|
|
* @param JWSBuilder $jwsBuilder |
114
|
|
|
*/ |
115
|
|
|
public function addJWSBuilder(string $id, JWSBuilder $jwsBuilder) |
116
|
|
|
{ |
117
|
|
|
$this->jwsBuilders[$id] = $jwsBuilder; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var JWSVerifier[] |
122
|
|
|
*/ |
123
|
|
|
private $jwsVerifiers = []; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $id |
127
|
|
|
* @param JWSVerifier $jwsVerifier |
128
|
|
|
*/ |
129
|
|
|
public function addJWSVerifier(string $id, JWSVerifier $jwsVerifier) |
130
|
|
|
{ |
131
|
|
|
$this->jwsVerifiers[$id] = $jwsVerifier; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @var JWSLoader[] |
136
|
|
|
*/ |
137
|
|
|
private $jwsLoaders = []; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $id |
141
|
|
|
* @param JWSLoader $jwsLoader |
142
|
|
|
*/ |
143
|
|
|
public function addJWSLoader(string $id, JWSLoader $jwsLoader) |
144
|
|
|
{ |
145
|
|
|
$this->jwsLoaders[$id] = $jwsLoader; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|