|
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\Helper; |
|
13
|
|
|
|
|
14
|
|
|
use Assert\Assertion; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This helper will help you to create services configuration. |
|
18
|
|
|
*/ |
|
19
|
|
|
final class ConfigurationHelper |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $name |
|
23
|
|
|
* @param string[] $header_checkers |
|
24
|
|
|
* @param string[] $claim_checkers |
|
25
|
|
|
* @param bool $is_public |
|
26
|
|
|
* |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function getCheckerConfiguration($name, array $header_checkers, array $claim_checkers, $is_public = true) |
|
30
|
|
|
{ |
|
31
|
|
|
self::checkParameters($name, $is_public); |
|
32
|
|
|
Assertion::allString($header_checkers); |
|
33
|
|
|
Assertion::allString($claim_checkers); |
|
34
|
|
|
|
|
35
|
|
|
return [ |
|
36
|
|
|
'jose' => [ |
|
37
|
|
|
'checkers' => [ |
|
38
|
|
|
$name => [ |
|
39
|
|
|
'is_public' => $is_public, |
|
40
|
|
|
'claims' => $claim_checkers, |
|
41
|
|
|
'headers' => $header_checkers, |
|
42
|
|
|
], |
|
43
|
|
|
], |
|
44
|
|
|
], |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @param string[] $signature_algorithms |
|
51
|
|
|
* @param bool $create_verifier |
|
52
|
|
|
* @param bool $is_public |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getSignerConfiguration($name, array $signature_algorithms, $create_verifier = false, $is_public = true) |
|
57
|
|
|
{ |
|
58
|
|
|
self::checkParameters($name, $is_public); |
|
59
|
|
|
Assertion::allString($signature_algorithms); |
|
60
|
|
|
Assertion::notEmpty($signature_algorithms); |
|
61
|
|
|
Assertion::boolean($create_verifier); |
|
62
|
|
|
|
|
63
|
|
|
return [ |
|
64
|
|
|
'jose' => [ |
|
65
|
|
|
'signers' => [ |
|
66
|
|
|
$name => [ |
|
67
|
|
|
'is_public' => $is_public, |
|
68
|
|
|
'algorithms' => $signature_algorithms, |
|
69
|
|
|
'create_verifier' => $create_verifier, |
|
70
|
|
|
], |
|
71
|
|
|
], |
|
72
|
|
|
], |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $name |
|
78
|
|
|
* @param string[] $signature_algorithms |
|
79
|
|
|
* @param bool $is_public |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function getVerifierConfiguration($name, array $signature_algorithms, $is_public = true) |
|
84
|
|
|
{ |
|
85
|
|
|
self::checkParameters($name, $is_public); |
|
86
|
|
|
Assertion::allString($signature_algorithms); |
|
87
|
|
|
Assertion::notEmpty($signature_algorithms); |
|
88
|
|
|
|
|
89
|
|
|
return [ |
|
90
|
|
|
'jose' => [ |
|
91
|
|
|
'verifiers' => [ |
|
92
|
|
|
$name => [ |
|
93
|
|
|
'is_public' => $is_public, |
|
94
|
|
|
'algorithms' => $signature_algorithms, |
|
95
|
|
|
], |
|
96
|
|
|
], |
|
97
|
|
|
], |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $name |
|
103
|
|
|
* @param string[] $key_encryption_algorithms |
|
104
|
|
|
* @param string[] $content_encryption_algorithms |
|
105
|
|
|
* @param string[] $compression_methods |
|
106
|
|
|
* @param bool $create_decrypter |
|
107
|
|
|
* @param bool $is_public |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function getEncrypterConfiguration($name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'], $create_decrypter = false, $is_public = true) |
|
112
|
|
|
{ |
|
113
|
|
|
self::checkParameters($name, $is_public); |
|
114
|
|
|
Assertion::allString($key_encryption_algorithms); |
|
115
|
|
|
Assertion::notEmpty($key_encryption_algorithms); |
|
116
|
|
|
Assertion::allString($content_encryption_algorithms); |
|
117
|
|
|
Assertion::notEmpty($content_encryption_algorithms); |
|
118
|
|
|
Assertion::boolean($create_decrypter); |
|
119
|
|
|
|
|
120
|
|
|
return [ |
|
121
|
|
|
'jose' => [ |
|
122
|
|
|
'encrypters' => [ |
|
123
|
|
|
$name => [ |
|
124
|
|
|
'is_public' => $is_public, |
|
125
|
|
|
'key_encryption_algorithms' => $key_encryption_algorithms, |
|
126
|
|
|
'content_encryption_algorithms' => $content_encryption_algorithms, |
|
127
|
|
|
'compression_methods' => $compression_methods, |
|
128
|
|
|
'create_decrypter' => $create_decrypter, |
|
129
|
|
|
], |
|
130
|
|
|
], |
|
131
|
|
|
], |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $name |
|
137
|
|
|
* @param string[] $key_encryption_algorithms |
|
138
|
|
|
* @param string[] $content_encryption_algorithms |
|
139
|
|
|
* @param string[] $compression_methods |
|
140
|
|
|
* @param bool $is_public |
|
141
|
|
|
* |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function getDecrypterConfiguration($name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'], $is_public = true) |
|
145
|
|
|
{ |
|
146
|
|
|
self::checkParameters($name, $is_public); |
|
147
|
|
|
Assertion::allString($key_encryption_algorithms); |
|
148
|
|
|
Assertion::notEmpty($key_encryption_algorithms); |
|
149
|
|
|
Assertion::allString($content_encryption_algorithms); |
|
150
|
|
|
Assertion::notEmpty($content_encryption_algorithms); |
|
151
|
|
|
|
|
152
|
|
|
return [ |
|
153
|
|
|
'jose' => [ |
|
154
|
|
|
'decrypters' => [ |
|
155
|
|
|
$name => [ |
|
156
|
|
|
'is_public' => $is_public, |
|
157
|
|
|
'key_encryption_algorithms' => $key_encryption_algorithms, |
|
158
|
|
|
'content_encryption_algorithms' => $content_encryption_algorithms, |
|
159
|
|
|
'compression_methods' => $compression_methods, |
|
160
|
|
|
], |
|
161
|
|
|
], |
|
162
|
|
|
], |
|
163
|
|
|
]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $name |
|
168
|
|
|
* @param string $signer |
|
169
|
|
|
* @param string|null $encrypter |
|
170
|
|
|
* @param bool $is_public |
|
171
|
|
|
* |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
public static function getJWTCreatorConfiguration($name, $signer, $encrypter = null, $is_public = true) |
|
175
|
|
|
{ |
|
176
|
|
|
self::checkParameters($name, $is_public); |
|
177
|
|
|
Assertion::string($signer); |
|
178
|
|
|
Assertion::notEmpty($signer); |
|
179
|
|
|
Assertion::nullOrString($encrypter); |
|
180
|
|
|
|
|
181
|
|
|
return [ |
|
182
|
|
|
'jose' => [ |
|
183
|
|
|
'jwt_creators' => [ |
|
184
|
|
|
$name => [ |
|
185
|
|
|
'is_public' => $is_public, |
|
186
|
|
|
'signer' => $signer, |
|
187
|
|
|
'encrypter' => $encrypter, |
|
188
|
|
|
], |
|
189
|
|
|
], |
|
190
|
|
|
], |
|
191
|
|
|
]; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string $name |
|
196
|
|
|
* @param string $verifier |
|
197
|
|
|
* @param string $checker |
|
198
|
|
|
* @param string|null $decrypter |
|
199
|
|
|
* @param bool $is_public |
|
200
|
|
|
* |
|
201
|
|
|
* @return array |
|
202
|
|
|
*/ |
|
203
|
|
|
public static function getJWTLoaderConfiguration($name, $verifier, $checker, $decrypter = null, $is_public = true) |
|
204
|
|
|
{ |
|
205
|
|
|
self::checkParameters($name, $is_public); |
|
206
|
|
|
Assertion::string($verifier); |
|
207
|
|
|
Assertion::notEmpty($verifier); |
|
208
|
|
|
Assertion::string($checker); |
|
209
|
|
|
Assertion::notEmpty($checker); |
|
210
|
|
|
Assertion::nullOrString($decrypter); |
|
211
|
|
|
|
|
212
|
|
|
return [ |
|
213
|
|
|
'jose' => [ |
|
214
|
|
|
'jwt_loaders' => [ |
|
215
|
|
|
$name => [ |
|
216
|
|
|
'is_public' => $is_public, |
|
217
|
|
|
'verifier' => $verifier, |
|
218
|
|
|
'checker' => $checker, |
|
219
|
|
|
'decrypter' => $decrypter, |
|
220
|
|
|
], |
|
221
|
|
|
], |
|
222
|
|
|
], |
|
223
|
|
|
]; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param string $name |
|
228
|
|
|
* @param int $nb_keys |
|
229
|
|
|
* @param array $key_configuration |
|
230
|
|
|
* @param bool $is_rotatable |
|
231
|
|
|
* @param bool $is_public |
|
232
|
|
|
* |
|
233
|
|
|
* @return array |
|
234
|
|
|
*/ |
|
235
|
|
|
public static function getRandomJWKSetConfiguration($name, $nb_keys, array $key_configuration, $is_rotatable = false, $is_public = true) |
|
236
|
|
|
{ |
|
237
|
|
|
self::checkParameters($name, $is_public); |
|
238
|
|
|
Assertion::integer($nb_keys); |
|
239
|
|
|
Assertion::greaterThan($nb_keys, 0); |
|
240
|
|
|
Assertion::boolean($is_rotatable); |
|
241
|
|
|
|
|
242
|
|
|
return [ |
|
243
|
|
|
'jose' => [ |
|
244
|
|
|
'key_sets' => [ |
|
245
|
|
|
$name => [ |
|
246
|
|
|
'auto' => [ |
|
247
|
|
|
'is_rotatable' => $is_rotatable, |
|
248
|
|
|
'is_public' => $is_public, |
|
249
|
|
|
'nb_keys' => $nb_keys, |
|
250
|
|
|
'key_configuration' => $key_configuration, |
|
251
|
|
|
], |
|
252
|
|
|
], |
|
253
|
|
|
], |
|
254
|
|
|
], |
|
255
|
|
|
]; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @param string $name |
|
260
|
|
|
* @param string $jwkset |
|
261
|
|
|
* @param bool $is_public |
|
262
|
|
|
* |
|
263
|
|
|
* @return array |
|
264
|
|
|
*/ |
|
265
|
|
|
public static function getPublicJWKSetConfiguration($name, $jwkset, $is_public = true) |
|
266
|
|
|
{ |
|
267
|
|
|
self::checkParameters($name, $is_public); |
|
268
|
|
|
Assertion::string($jwkset); |
|
269
|
|
|
Assertion::notEmpty($jwkset); |
|
270
|
|
|
|
|
271
|
|
|
return [ |
|
272
|
|
|
'jose' => [ |
|
273
|
|
|
'key_sets' => [ |
|
274
|
|
|
$name => [ |
|
275
|
|
|
'public_jwkset' => [ |
|
276
|
|
|
'is_public' => $is_public, |
|
277
|
|
|
'id' => $jwkset, |
|
278
|
|
|
], |
|
279
|
|
|
], |
|
280
|
|
|
], |
|
281
|
|
|
], |
|
282
|
|
|
]; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param string $name |
|
287
|
|
|
* @param string[] $jwksets |
|
288
|
|
|
* @param bool $is_public |
|
289
|
|
|
* |
|
290
|
|
|
* @return array |
|
291
|
|
|
*/ |
|
292
|
|
|
public static function getJWKSetsConfiguration($name, array $jwksets, $is_public = true) |
|
293
|
|
|
{ |
|
294
|
|
|
self::checkParameters($name, $is_public); |
|
295
|
|
|
Assertion::isArray($jwksets); |
|
296
|
|
|
Assertion::allString($jwksets); |
|
297
|
|
|
Assertion::allNotEmpty($jwksets); |
|
298
|
|
|
|
|
299
|
|
|
return [ |
|
300
|
|
|
'jose' => [ |
|
301
|
|
|
'key_sets' => [ |
|
302
|
|
|
$name => [ |
|
303
|
|
|
'jwksets' => [ |
|
304
|
|
|
'is_public' => $is_public, |
|
305
|
|
|
'id' => $jwksets, |
|
306
|
|
|
], |
|
307
|
|
|
], |
|
308
|
|
|
], |
|
309
|
|
|
], |
|
310
|
|
|
]; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @param string $name |
|
315
|
|
|
* @param bool $is_public |
|
316
|
|
|
*/ |
|
317
|
|
|
private static function checkParameters($name, $is_public) |
|
318
|
|
|
{ |
|
319
|
|
|
Assertion::string($name); |
|
320
|
|
|
Assertion::notEmpty($name); |
|
321
|
|
|
Assertion::boolean($is_public); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|