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