|
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 Jose\Bundle\JoseFramework\DependencyInjection\Source\JWKSetSource; |
|
13
|
|
|
|
|
14
|
|
|
use Jose\Bundle\JoseFramework\Controller\JWKSetController; |
|
15
|
|
|
use Jose\Bundle\JoseFramework\Controller\JWKSetControllerFactory; |
|
16
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\AbstractSource; |
|
17
|
|
|
use Jose\Bundle\JoseFramework\Routing\JWKSetLoader; |
|
18
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
22
|
|
|
|
|
23
|
|
|
abstract class AbstractJWKSetSource extends AbstractSource implements JWKSetSourceInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public function create(ContainerBuilder $container, string $type, string $name, array $config) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::create($container, $type, $name, $config); |
|
31
|
|
|
|
|
32
|
|
|
if (null !== $config['path']) { |
|
33
|
|
|
$jwkset_id = sprintf('jose.key_set.%s', $name); |
|
34
|
|
|
$controller_definition = new Definition(JWKSetController::class); |
|
35
|
|
|
$controller_definition->setFactory([new Reference(JWKSetControllerFactory::class), 'create']); |
|
36
|
|
|
$controller_definition->setArguments([new Reference($jwkset_id), $config['max_age']]); |
|
37
|
|
|
$controller_id = sprintf('jose.controller.%s', $name); |
|
38
|
|
|
$container->setDefinition($controller_id, $controller_definition); |
|
39
|
|
|
|
|
40
|
|
|
$jwkset_loader_definition = $container->getDefinition(JWKSetLoader::class); |
|
41
|
|
|
$jwkset_loader_definition->addMethodCall('add', [$config['path'], $config['max_age'], $name]); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param NodeDefinition $node |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addConfiguration(NodeDefinition $node) |
|
49
|
|
|
{ |
|
50
|
|
|
parent::addConfiguration($node); |
|
51
|
|
|
$node |
|
|
|
|
|
|
52
|
|
|
->children() |
|
53
|
|
|
->scalarNode('path') |
|
54
|
|
|
->info('To share the JWKSet, then set a valid path (e.g. "/jwkset.json").') |
|
55
|
|
|
->defaultNull() |
|
56
|
|
|
->end() |
|
57
|
|
|
->integerNode('max_age') |
|
58
|
|
|
->info('When share, this value indicates when the HTTP client should keep the key in cache. Default is 21600 = 6 hours.') |
|
59
|
|
|
->defaultValue(21600) |
|
60
|
|
|
->end() |
|
61
|
|
|
->end(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
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: