|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\JwtBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\JwtBundle\DependencyInjection; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface; |
|
12
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author John Kleijn <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class JwtAuthenticationFactory implements SecurityFactoryInterface |
|
21
|
|
|
{ |
|
22
|
|
|
public function getPosition() |
|
23
|
|
|
{ |
|
24
|
|
|
return 'pre_auth'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getKey() |
|
28
|
|
|
{ |
|
29
|
|
|
return 'jwt'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function addConfiguration(NodeDefinition $node) |
|
33
|
|
|
{ |
|
34
|
|
|
$node |
|
|
|
|
|
|
35
|
|
|
->children() |
|
36
|
|
|
->scalarNode('header')->defaultValue('Authorization')->end() |
|
37
|
|
|
->end(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint) |
|
41
|
|
|
{ |
|
42
|
|
|
$providerId = 'security.authentication.provider.jwt.' . $id; |
|
43
|
|
|
$container |
|
44
|
|
|
->setDefinition($providerId, new DefinitionDecorator('jwt.security.authentication.provider')) |
|
45
|
|
|
->replaceArgument(0, new Reference($userProvider)); |
|
46
|
|
|
|
|
47
|
|
|
$listenerId = 'security.authentication.listener.jwt.' . $id; |
|
48
|
|
|
$container->setDefinition($listenerId, new DefinitionDecorator('jwt.security.authentication.listener')); |
|
49
|
|
|
|
|
50
|
|
|
return [$providerId, $listenerId, $defaultEntryPoint]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
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: