1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SpomkyLabs\JoseBundle\DependencyInjection\Source; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
|
17
|
|
|
final class EasyJWTCreatorSource implements SourceInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function getName() |
23
|
|
|
{ |
24
|
|
|
return 'easy_jwt_creator'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function createService($name, array $config, ContainerBuilder $container) |
31
|
|
|
{ |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function getNodeDefinition(ArrayNodeDefinition $node) |
38
|
|
|
{ |
39
|
|
|
$node |
40
|
|
|
->children() |
41
|
|
|
->arrayNode('easy_jwt_creator') |
42
|
|
|
->useAttributeAsKey('name') |
43
|
|
|
->prototype('array') |
44
|
|
|
->children() |
45
|
|
|
->booleanNode('is_public') |
46
|
|
|
->info('If true, the service will be public, else private.') |
47
|
|
|
->defaultTrue() |
48
|
|
|
->end() |
49
|
|
|
->arrayNode('signature_algorithms') |
50
|
|
|
->useAttributeAsKey('name') |
51
|
|
|
->isRequired() |
52
|
|
|
->cannotBeEmpty() |
53
|
|
|
->prototype('scalar')->end() |
54
|
|
|
->end() |
55
|
|
|
->arrayNode('key_encryption_algorithms') |
56
|
|
|
->useAttributeAsKey('name') |
57
|
|
|
->defaultValue([]) |
58
|
|
|
->prototype('scalar')->end() |
59
|
|
|
->end() |
60
|
|
|
->arrayNode('content_encryption_algorithms') |
61
|
|
|
->useAttributeAsKey('name') |
62
|
|
|
->defaultValue([]) |
63
|
|
|
->prototype('scalar')->end() |
64
|
|
|
->end() |
65
|
|
|
->arrayNode('compression_methods') |
66
|
|
|
->useAttributeAsKey('name') |
67
|
|
|
->defaultValue(['DEF']) |
68
|
|
|
->prototype('scalar')->end() |
69
|
|
|
->end() |
70
|
|
|
->end() |
71
|
|
|
->end() |
72
|
|
|
->end() |
73
|
|
|
->end(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function prepend(ContainerBuilder $container, array $config) |
80
|
|
|
{ |
81
|
|
|
if (false === array_key_exists($this->getName(), $config)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
foreach ($config[$this->getName()] as $id => $section) { |
86
|
|
|
$config = $this->createServiceConfiguration($config, $id, $section); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $config; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $config |
94
|
|
|
* @param string $id |
95
|
|
|
* @param array $section |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
private function createServiceConfiguration(array $config, $id, array $section) |
100
|
|
|
{ |
101
|
|
|
$config = $this->createSignerServiceConfiguration($config, $id, $section); |
102
|
|
|
$config = $this->createEncrypterServiceConfiguration($config, $id, $section); |
103
|
|
|
$config = $this->createJWTCreatorServiceConfiguration($config, $id, $section); |
104
|
|
|
|
105
|
|
|
return $config; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $config |
110
|
|
|
* @param string $id |
111
|
|
|
* @param array $section |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private function createSignerServiceConfiguration(array $config, $id, array $section) |
116
|
|
|
{ |
117
|
|
|
$config['signers'] = array_merge( |
118
|
|
|
array_key_exists('signers', $config) ? $config['signers'] : [], |
119
|
|
|
[$id => [ |
120
|
|
|
'is_public' => $section['is_public'], |
121
|
|
|
'algorithms' => $section['signature_algorithms'], |
122
|
|
|
]] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $config; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $config |
130
|
|
|
* @param string $id |
131
|
|
|
* @param array $section |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
private function createEncrypterServiceConfiguration(array $config, $id, array $section) |
136
|
|
|
{ |
137
|
|
|
if (false === $this->isEncryptionSupportEnabled($section)) { |
138
|
|
|
return $config; |
139
|
|
|
} |
140
|
|
|
$config['encrypters'] = array_merge( |
141
|
|
|
array_key_exists('encrypters', $config) ? $config['encrypters'] : [], |
142
|
|
|
[$id => [ |
143
|
|
|
'is_public' => $section['is_public'], |
144
|
|
|
'key_encryption_algorithms' => $section['key_encryption_algorithms'], |
145
|
|
|
'content_encryption_algorithms' => $section['content_encryption_algorithms'], |
146
|
|
|
'compression_methods' => $section['compression_methods'], |
147
|
|
|
]] |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
return $config; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $config |
155
|
|
|
* @param string $id |
156
|
|
|
* @param array $section |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
private function createJWTCreatorServiceConfiguration(array $config, $id, array $section) |
161
|
|
|
{ |
162
|
|
|
$service = [ |
163
|
|
|
'is_public' => $section['is_public'], |
164
|
|
|
'signer' => sprintf('jose.signer.%s', $id), |
165
|
|
|
]; |
166
|
|
|
if (true === $this->isEncryptionSupportEnabled($section)) { |
167
|
|
|
$service['encrypter'] = sprintf('jose.encrypter.%s', $id); |
168
|
|
|
} |
169
|
|
|
$config['jwt_creators'] = array_merge( |
170
|
|
|
array_key_exists('jwt_creators', $config) ? $config['jwt_creators'] : [], |
171
|
|
|
[$id => $service] |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
return $config; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param array $section |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
private function isEncryptionSupportEnabled(array $section) |
183
|
|
|
{ |
184
|
|
|
if (true === empty($section['key_encryption_algorithms']) && true === empty($section['content_encryption_algorithms'])) { |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (true === empty($section['key_encryption_algorithms']) || true === empty($section['content_encryption_algorithms'])) { |
189
|
|
|
throw new \LogicException('Both key encryption algorithms and content encryption algorithms must be set to enable the encryption support.'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|