1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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 OAuth2Framework\Bundle\DependencyInjection\Component\Grant\JwtBearer; |
15
|
|
|
|
16
|
|
|
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper; |
17
|
|
|
use OAuth2Framework\Bundle\DependencyInjection\Component\Component; |
18
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
19
|
|
|
use Symfony\Component\Config\FileLocator; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
22
|
|
|
|
23
|
|
|
class JwtBearerSource implements Component |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function name(): string |
29
|
|
|
{ |
30
|
|
|
return 'jwt_bearer'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function load(array $configs, ContainerBuilder $container) |
37
|
|
|
{ |
38
|
|
|
if ($configs['grant']['jwt_bearer']['enabled']) { |
39
|
|
|
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../../Resources/config/grant')); |
40
|
|
|
$loader->load('jwt_bearer.php'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getNodeDefinition(NodeDefinition $node) |
48
|
|
|
{ |
49
|
|
|
$node |
|
|
|
|
50
|
|
|
->validate() |
51
|
|
|
->ifTrue(function ($config) { |
52
|
|
|
return true === $config['enabled'] && empty($config['signature_algorithms']); |
53
|
|
|
}) |
54
|
|
|
->thenInvalid('The option "signature_algorithms" must contain at least one signature algorithm.') |
55
|
|
|
->end() |
56
|
|
|
->children() |
57
|
|
|
->booleanNode('issue_refresh_token') |
58
|
|
|
->info('If true, a refresh token will be issued with the access token (the refresh token grant type must be enabled).') |
59
|
|
|
->end() |
60
|
|
|
->arrayNode('signature_algorithms') |
61
|
|
|
->info('Signature algorithms supported by this grant type.') |
62
|
|
|
->useAttributeAsKey('name') |
63
|
|
|
->prototype('scalar')->end() |
64
|
|
|
->treatNullLike([]) |
65
|
|
|
->end() |
66
|
|
|
->arrayNode('claim_checkers') |
67
|
|
|
->info('Checkers will verify the JWT claims.') |
68
|
|
|
->useAttributeAsKey('name') |
69
|
|
|
->prototype('scalar')->end() |
70
|
|
|
->treatNullLike(['exp', 'iat', 'nbf']) |
71
|
|
|
->end() |
72
|
|
|
->arrayNode('header_checkers') |
73
|
|
|
->info('Checkers will verify the JWT headers.') |
74
|
|
|
->useAttributeAsKey('name') |
75
|
|
|
->prototype('scalar')->end() |
76
|
|
|
->treatNullLike(['crit']) |
77
|
|
|
->end() |
78
|
|
|
->arrayNode('encryption') |
79
|
|
|
->children() |
80
|
|
|
->booleanNode('required') |
81
|
|
|
->info('If set to true, all ID Token sent to the server must be encrypted.') |
82
|
|
|
->defaultFalse() |
83
|
|
|
->end() |
84
|
|
|
->arrayNode('key_encryption_algorithms') |
85
|
|
|
->info('Supported key encryption algorithms.') |
86
|
|
|
->useAttributeAsKey('name') |
87
|
|
|
->prototype('scalar')->end() |
88
|
|
|
->treatNullLike([]) |
89
|
|
|
->end() |
90
|
|
|
->arrayNode('content_encryption_algorithms') |
91
|
|
|
->info('Supported content encryption algorithms.') |
92
|
|
|
->useAttributeAsKey('name') |
93
|
|
|
->prototype('scalar')->end() |
94
|
|
|
->treatNullLike([]) |
95
|
|
|
->end() |
96
|
|
|
->end() |
97
|
|
|
->end() |
98
|
|
|
->end(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function prepend(ContainerBuilder $container, array $config): array |
105
|
|
|
{ |
106
|
|
|
//Nothing to do |
107
|
|
|
return []; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ContainerBuilder $container |
112
|
|
|
* @param array $sourceConfig |
113
|
|
|
*/ |
114
|
|
|
private function updateJoseBundleConfigurationForVerifier(ContainerBuilder $container, array $sourceConfig) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
ConfigurationHelper::addJWSLoader($container, $this->name(), $sourceConfig['signature_algorithms'], [], ['jws_compact'], false); |
|
|
|
|
117
|
|
|
ConfigurationHelper::addClaimChecker($container, $this->name(), [], false); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ContainerBuilder $container |
122
|
|
|
* @param array $sourceConfig |
123
|
|
|
*/ |
124
|
|
|
private function updateJoseBundleConfigurationForDecrypter(ContainerBuilder $container, array $sourceConfig) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
if (true === $sourceConfig['encryption']['enabled']) { |
127
|
|
|
//ConfigurationHelper::addKeyset($container, 'jwt_bearer.key_set.encryption', 'jwkset', ['value' => $bundleConfig['key_set']['encryption']]); |
|
|
|
|
128
|
|
|
ConfigurationHelper::addJWELoader($container, $this->name(), $sourceConfig['encryption']['key_encryption_algorithms'], $sourceConfig['encryption']['content_encryption_algorithms'], ['DEF'], [], ['jwe_compact'], false); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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: