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 createService(array $config, ContainerBuilder $container) |
45
|
|
|
{ |
46
|
|
|
$sources = $this->getJWKSources(); |
47
|
|
|
|
48
|
|
|
foreach ($config as $name => $itemConfig) { |
49
|
|
|
foreach ($itemConfig as $sourceName => $sourceConfig) { |
50
|
|
|
if (array_key_exists($sourceName, $sources)) { |
51
|
|
|
$source = $sources[$sourceName]; |
52
|
|
|
$source->create($container, 'key', $sourceName, $sourceConfig); |
53
|
|
|
} else { |
54
|
|
|
throw new \LogicException(sprintf('The JWK definition "%s" is not configured.', $name)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getNodeDefinition(ArrayNodeDefinition $node) |
64
|
|
|
{ |
65
|
|
|
$sourceNodeBuilder = $node |
|
|
|
|
66
|
|
|
->children() |
67
|
|
|
->arrayNode('keys') |
68
|
|
|
->useAttributeAsKey('name') |
69
|
|
|
->prototype('array') |
70
|
|
|
->performNoDeepMerging() |
71
|
|
|
->children(); |
72
|
|
|
foreach ($this->getJWKSources() as $name => $source) { |
73
|
|
|
$sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset(); |
74
|
|
|
$source->addConfiguration($sourceNode); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function prepend(ContainerBuilder $container, array $config): ?array |
82
|
|
|
{ |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return JWKSourceInterface[] |
88
|
|
|
*/ |
89
|
|
|
private function getJWKSources(): array |
90
|
|
|
{ |
91
|
|
|
if (null !== $this->jwkSources) { |
92
|
|
|
return $this->jwkSources; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// load bundled adapter factories |
96
|
|
|
$tempContainer = new ContainerBuilder(); |
97
|
|
|
$loader = new YamlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../Resources/config')); |
98
|
|
|
$loader->load('jwk_sources.yml'); |
99
|
|
|
$services = $tempContainer->findTaggedServiceIds('jose.jwk_source'); |
100
|
|
|
$jwkSources = []; |
101
|
|
|
foreach (array_keys($services) as $id) { |
102
|
|
|
$factory = $tempContainer->get($id); |
103
|
|
|
$jwkSources[str_replace('-', '_', $factory->getKey())] = $factory; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->jwkSources = $jwkSources; |
107
|
|
|
|
108
|
|
|
return $jwkSources; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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: