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\JoseFramework\DependencyInjection\Source\KeyManagement; |
15
|
|
|
|
16
|
|
|
use Jose\Bundle\JoseFramework\Controller\JWKSetController; |
17
|
|
|
use Jose\Bundle\JoseFramework\Controller\JWKSetControllerFactory; |
18
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source; |
19
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
21
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
22
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class JKUriSource. |
26
|
|
|
*/ |
27
|
|
|
final class JWKUriSource implements Source |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function name(): string |
33
|
|
|
{ |
34
|
|
|
return 'jwk_uris'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function load(array $configs, ContainerBuilder $container) |
41
|
|
|
{ |
42
|
|
|
foreach ($configs[$this->name()] as $name => $itemConfig) { |
43
|
|
|
$service_id = sprintf('jose.controller.%s', $name); |
44
|
|
|
$definition = new Definition(JWKSetController::class); |
45
|
|
|
$definition->setFactory([new Reference(JWKSetControllerFactory::class), 'create']); |
46
|
|
|
$definition->setArguments([new Reference($itemConfig['id']), $itemConfig['max_age']]); |
47
|
|
|
$definition->addTag('jose.jwk_uri.controller', ['path' => $itemConfig['path']]); |
48
|
|
|
$container->setDefinition($service_id, $definition); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function getNodeDefinition(ArrayNodeDefinition $node) |
56
|
|
|
{ |
57
|
|
|
$node |
|
|
|
|
58
|
|
|
->children() |
59
|
|
|
->arrayNode('jwk_uris') |
60
|
|
|
->useAttributeAsKey('name') |
61
|
|
|
->prototype('array') |
62
|
|
|
->performNoDeepMerging() |
63
|
|
|
->children() |
64
|
|
|
->scalarNode('id') |
65
|
|
|
->info('The service ID of the Key Set to share.') |
66
|
|
|
->defaultNull() |
67
|
|
|
->end() |
68
|
|
|
->scalarNode('path') |
69
|
|
|
->info('To share the JWKSet, then set a valid path (e.g. "/jwkset.json").') |
70
|
|
|
->defaultNull() |
71
|
|
|
->end() |
72
|
|
|
->integerNode('max_age') |
73
|
|
|
->info('When share, this value indicates how many seconds the HTTP client should keep the key in cache. Default is 21600 = 6 hours.') |
74
|
|
|
->defaultValue(21600) |
75
|
|
|
->end() |
76
|
|
|
->end() |
77
|
|
|
->end() |
78
|
|
|
->end() |
79
|
|
|
->end(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function prepend(ContainerBuilder $container, array $config): array |
86
|
|
|
{ |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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: