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\KeyManagement\DependencyInjection\Source; |
15
|
|
|
|
16
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface; |
17
|
|
|
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSource\JWKSourceInterface; |
18
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
19
|
|
|
use Symfony\Component\Config\FileLocator; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class JWKSource. |
25
|
|
|
*/ |
26
|
|
|
final class JWKSource implements SourceInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var null|JWKSourceInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $jwkSources = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function name(): string |
37
|
|
|
{ |
38
|
|
|
return 'keys'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function load(array $configs, ContainerBuilder $container) |
45
|
|
|
{ |
46
|
|
|
$this->createService($configs[$this->name()], $container); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
private function createService(array $config, ContainerBuilder $container) |
53
|
|
|
{ |
54
|
|
|
$sources = $this->getJWKSources(); |
55
|
|
|
|
56
|
|
|
foreach ($config as $name => $itemConfig) { |
57
|
|
|
foreach ($itemConfig as $sourceName => $sourceConfig) { |
58
|
|
|
if (array_key_exists($sourceName, $sources)) { |
59
|
|
|
$source = $sources[$sourceName]; |
60
|
|
|
$source->create($container, 'key', $sourceName, $sourceConfig); |
61
|
|
|
} else { |
62
|
|
|
throw new \LogicException(sprintf('The JWK definition "%s" is not configured.', $name)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getNodeDefinition(ArrayNodeDefinition $node) |
72
|
|
|
{ |
73
|
|
|
$sourceNodeBuilder = $node |
|
|
|
|
74
|
|
|
->children() |
75
|
|
|
->arrayNode('keys') |
76
|
|
|
->useAttributeAsKey('name') |
77
|
|
|
->prototype('array') |
78
|
|
|
->performNoDeepMerging() |
79
|
|
|
->children(); |
80
|
|
|
foreach ($this->getJWKSources() as $name => $source) { |
81
|
|
|
$sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset(); |
82
|
|
|
$source->addConfiguration($sourceNode); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function prepend(ContainerBuilder $container, array $config): ?array |
90
|
|
|
{ |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return JWKSourceInterface[] |
96
|
|
|
*/ |
97
|
|
|
private function getJWKSources(): array |
98
|
|
|
{ |
99
|
|
|
if (null !== $this->jwkSources) { |
100
|
|
|
return $this->jwkSources; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// load bundled adapter factories |
104
|
|
|
$tempContainer = new ContainerBuilder(); |
105
|
|
|
$loader = new YamlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../Resources/config')); |
106
|
|
|
$loader->load('jwk_sources.yml'); |
107
|
|
|
$services = $tempContainer->findTaggedServiceIds('jose.jwk_source'); |
108
|
|
|
$jwkSources = []; |
109
|
|
|
foreach (array_keys($services) as $id) { |
110
|
|
|
$factory = $tempContainer->get($id); |
111
|
|
|
$jwkSources[str_replace('-', '_', $factory->getKey())] = $factory; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->jwkSources = $jwkSources; |
115
|
|
|
|
116
|
|
|
return $jwkSources; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: