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; |
15
|
|
|
|
16
|
|
|
use Fluent\PhpConfigFileLoader; |
17
|
|
|
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper; |
18
|
|
|
use OAuth2Framework\Bundle\DependencyInjection\Component\Component; |
19
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
20
|
|
|
use Symfony\Component\Config\FileLocator; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
22
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
23
|
|
|
|
24
|
|
|
final class JwtBearerSource implements Component |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* JwtBearerSource constructor. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->addSubSource(new JwtBearerEncryptionSource()); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected function continueLoading(string $path, ContainerBuilder $container, array $config) |
38
|
|
|
{ |
39
|
|
|
foreach ($config as $k => $v) { |
40
|
|
|
$container->setParameter($path.'.'.$k, $config[$k]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$loader = new PhpConfigFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/grant')); |
44
|
|
|
$loader->load('jwt_bearer.php'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function name(): string |
51
|
|
|
{ |
52
|
|
|
return 'jwt_bearer'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getNodeDefinition(NodeDefinition $node) |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
$node |
|
|
|
|
59
|
|
|
->validate() |
60
|
|
|
->ifTrue(function ($config) { |
61
|
|
|
return true === $config['enabled'] && empty($config['signature_algorithms']); |
62
|
|
|
}) |
63
|
|
|
->thenInvalid('The option "signature_algorithms" must contain at least one signature algorithm.') |
64
|
|
|
->end() |
65
|
|
|
->children() |
66
|
|
|
->booleanNode('issue_refresh_token') |
67
|
|
|
->info('If true, a refresh token will be issued with the access token (the refresh token grant type must be enabled).') |
68
|
|
|
->end() |
69
|
|
|
->arrayNode('signature_algorithms') |
70
|
|
|
->info('Signature algorithms supported by this grant type.') |
71
|
|
|
->useAttributeAsKey('name') |
72
|
|
|
->prototype('scalar')->end() |
73
|
|
|
->treatNullLike([]) |
74
|
|
|
->end() |
75
|
|
|
->arrayNode('claim_checkers') |
76
|
|
|
->info('Checkers will verify the JWT claims.') |
77
|
|
|
->useAttributeAsKey('name') |
78
|
|
|
->prototype('scalar')->end() |
79
|
|
|
->treatNullLike(['exp', 'iat', 'nbf']) |
80
|
|
|
->end() |
81
|
|
|
->arrayNode('header_checkers') |
82
|
|
|
->info('Checkers will verify the JWT headers.') |
83
|
|
|
->useAttributeAsKey('name') |
84
|
|
|
->prototype('scalar')->end() |
85
|
|
|
->treatNullLike(['crit']) |
86
|
|
|
->end() |
87
|
|
|
->end(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function prepend(array $bundleConfig, string $path, ContainerBuilder $container) |
94
|
|
|
{ |
95
|
|
|
parent::prepend($bundleConfig, $path, $container); |
96
|
|
|
$currentPath = $path.'['.$this->name().']'; |
97
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
98
|
|
|
$sourceConfig = $accessor->getValue($bundleConfig, $currentPath); |
99
|
|
|
|
100
|
|
|
if (true === $sourceConfig['enabled']) { |
101
|
|
|
$this->updateJoseBundleConfigurationForVerifier($container, $sourceConfig); |
102
|
|
|
$this->updateJoseBundleConfigurationForDecrypter($container, $sourceConfig); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ContainerBuilder $container |
108
|
|
|
* @param array $sourceConfig |
109
|
|
|
*/ |
110
|
|
|
private function updateJoseBundleConfigurationForVerifier(ContainerBuilder $container, array $sourceConfig) |
111
|
|
|
{ |
112
|
|
|
ConfigurationHelper::addJWSLoader($container, $this->name(), $sourceConfig['signature_algorithms'], [], ['jws_compact'], false); |
|
|
|
|
113
|
|
|
ConfigurationHelper::addClaimChecker($container, $this->name(), [], false); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param ContainerBuilder $container |
118
|
|
|
* @param array $sourceConfig |
119
|
|
|
*/ |
120
|
|
|
private function updateJoseBundleConfigurationForDecrypter(ContainerBuilder $container, array $sourceConfig) |
121
|
|
|
{ |
122
|
|
|
if (true === $sourceConfig['encryption']['enabled']) { |
123
|
|
|
ConfigurationHelper::addJWELoader($container, $this->name(), $sourceConfig['encryption']['key_encryption_algorithms'], $sourceConfig['encryption']['content_encryption_algorithms'], ['DEF'], [], ['jwe_compact'], false); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|