|
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\DependencyInjection\Source\KeyManagement; |
|
15
|
|
|
|
|
16
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\KeyManagement\JWKSetSource\JWKSetSource as JWKSetSourceInterface; |
|
17
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source; |
|
18
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
|
19
|
|
|
use Symfony\Component\Config\FileLocator; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
22
|
|
|
|
|
23
|
|
|
class JWKSetSource implements Source |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var null|JWKSetSourceInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $jwkset_sources = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function name(): string |
|
34
|
|
|
{ |
|
35
|
|
|
return 'key_sets'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
42
|
|
|
{ |
|
43
|
|
|
$sources = $this->getJWKSetSources(); |
|
44
|
|
|
foreach ($configs[$this->name()] as $name => $itemConfig) { |
|
45
|
|
|
foreach ($itemConfig as $sourceName => $sourceConfig) { |
|
46
|
|
|
if (array_key_exists($sourceName, $sources)) { |
|
47
|
|
|
$source = $sources[$sourceName]; |
|
48
|
|
|
$source->create($container, 'key_set', $name, $sourceConfig); |
|
49
|
|
|
} else { |
|
50
|
|
|
throw new \LogicException(sprintf('The JWKSet definition "%s" is not configured.', $name)); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getNodeDefinition(NodeDefinition $node) |
|
60
|
|
|
{ |
|
61
|
|
|
$sourceNodeBuilder = $node |
|
62
|
|
|
->children() |
|
63
|
|
|
->arrayNode('key_sets') |
|
64
|
|
|
->treatFalseLike([]) |
|
65
|
|
|
->treatNullLike([]) |
|
66
|
|
|
->useAttributeAsKey('name') |
|
67
|
|
|
->arrayPrototype() |
|
68
|
|
|
->validate() |
|
69
|
|
|
->ifTrue(function ($config) { |
|
70
|
|
|
return count($config) !== 1; |
|
71
|
|
|
}) |
|
72
|
|
|
->thenInvalid('One key set type must be set.') |
|
73
|
|
|
->end() |
|
74
|
|
|
->children(); |
|
75
|
|
|
foreach ($this->getJWKSetSources() as $name => $source) { |
|
76
|
|
|
$sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset(); |
|
77
|
|
|
$source->addConfiguration($sourceNode); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function prepend(ContainerBuilder $container, array $config): array |
|
85
|
|
|
{ |
|
86
|
|
|
return []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws \Exception |
|
91
|
|
|
* |
|
92
|
|
|
* @return JWKSetSourceInterface[] |
|
93
|
|
|
*/ |
|
94
|
|
|
private function getJWKSetSources(): array |
|
95
|
|
|
{ |
|
96
|
|
|
if (null !== $this->jwkset_sources) { |
|
97
|
|
|
return $this->jwkset_sources; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// load bundled adapter factories |
|
101
|
|
|
$tempContainer = new ContainerBuilder(); |
|
102
|
|
|
$loader = new YamlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../../Resources/config')); |
|
103
|
|
|
$loader->load('jwkset_sources.yml'); |
|
104
|
|
|
|
|
105
|
|
|
$services = $tempContainer->findTaggedServiceIds('jose.jwkset_source'); |
|
106
|
|
|
$jwkset_sources = []; |
|
107
|
|
|
foreach (array_keys($services) as $id) { |
|
108
|
|
|
$factory = $tempContainer->get($id); |
|
109
|
|
|
if (!$factory instanceof JWKSetSourceInterface) { |
|
110
|
|
|
throw new \InvalidArgumentException(); |
|
111
|
|
|
} |
|
112
|
|
|
$jwkset_sources[str_replace('-', '_', $factory->getKeySet())] = $factory; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this->jwkset_sources = $jwkset_sources; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|