|
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\SecurityBundle\DependencyInjection; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\BearerTokenType\BearerToken; |
|
17
|
|
|
use OAuth2Framework\Component\MacTokenType\MacToken; |
|
18
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
19
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
20
|
|
|
|
|
21
|
|
|
final class Configuration implements ConfigurationInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $alias; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Configuration constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $alias |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(string $alias) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->alias = $alias; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getConfigTreeBuilder() |
|
42
|
|
|
{ |
|
43
|
|
|
$treeBuilder = new TreeBuilder(); |
|
44
|
|
|
$rootNode = $treeBuilder->root($this->alias); |
|
45
|
|
|
|
|
46
|
|
|
$rootNode |
|
47
|
|
|
->addDefaultsIfNotSet() |
|
48
|
|
|
->children() |
|
49
|
|
|
->scalarNode('psr7_message_factory') |
|
50
|
|
|
->cannotBeEmpty() |
|
51
|
|
|
->defaultValue('oauth2_security.psr7_message_factory.default') |
|
52
|
|
|
->info('PSR7 requests and responses factory') |
|
53
|
|
|
->end() |
|
54
|
|
|
->end(); |
|
55
|
|
|
if (class_exists(BearerToken::class)) { |
|
56
|
|
|
$rootNode |
|
57
|
|
|
->children() |
|
58
|
|
|
->arrayNode('bearer_token') |
|
59
|
|
|
->addDefaultsIfNotSet() |
|
60
|
|
|
->canBeDisabled() |
|
61
|
|
|
->children() |
|
62
|
|
|
->scalarNode('realm') |
|
63
|
|
|
->isRequired() |
|
64
|
|
|
->info('The realm displayed in the authentication header') |
|
65
|
|
|
->end() |
|
66
|
|
|
->booleanNode('authorization_header') |
|
67
|
|
|
->defaultTrue() |
|
68
|
|
|
->info('Allow the access token to be sent in the authorization header (recommended).') |
|
69
|
|
|
->end() |
|
70
|
|
|
->booleanNode('request_body') |
|
71
|
|
|
->defaultFalse() |
|
72
|
|
|
->info('Allow the access token to be sent in the request body (not recommended).') |
|
73
|
|
|
->end() |
|
74
|
|
|
->booleanNode('query_string') |
|
75
|
|
|
->defaultFalse() |
|
76
|
|
|
->info('Allow the access token to be sent in the query string (not recommended).') |
|
77
|
|
|
->end() |
|
78
|
|
|
->end() |
|
79
|
|
|
->end() |
|
80
|
|
|
->end(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (class_exists(MacToken::class)) { |
|
84
|
|
|
$rootNode |
|
85
|
|
|
->children() |
|
86
|
|
|
->arrayNode('mac_token') |
|
87
|
|
|
->addDefaultsIfNotSet() |
|
88
|
|
|
->canBeDisabled() |
|
89
|
|
|
->validate() |
|
90
|
|
|
->ifTrue(function ($config) { |
|
91
|
|
|
return $config['min_length'] > $config['max_length']; |
|
92
|
|
|
}) |
|
93
|
|
|
->thenInvalid('The option "min_length" must not be greater than "max_length".') |
|
94
|
|
|
->end() |
|
95
|
|
|
->validate() |
|
96
|
|
|
->ifTrue(function ($config) { |
|
97
|
|
|
return !in_array($config['algorithm'], ['hmac-sha-256', 'hmac-sha-1']); |
|
98
|
|
|
}) |
|
99
|
|
|
->thenInvalid('The algorithm is not supported. Please use one of the following one: "hmac-sha-1", "hmac-sha-256".') |
|
100
|
|
|
->end() |
|
101
|
|
|
->children() |
|
102
|
|
|
->integerNode('min_length') |
|
103
|
|
|
->defaultValue(50) |
|
104
|
|
|
->min(1) |
|
105
|
|
|
->info('Minimum length for the generated MAC key') |
|
106
|
|
|
->end() |
|
107
|
|
|
->integerNode('max_length') |
|
108
|
|
|
->defaultValue(100) |
|
109
|
|
|
->min(2) |
|
110
|
|
|
->info('Maximum length for the generated MAC key') |
|
111
|
|
|
->end() |
|
112
|
|
|
->scalarNode('algorithm') |
|
113
|
|
|
->defaultValue('hmac-sha-256') |
|
114
|
|
|
->info('Hashing algorithm. Must be either "hmac-sha-1" or "hmac-sha-256"') |
|
115
|
|
|
->end() |
|
116
|
|
|
->integerNode('timestamp_lifetime') |
|
117
|
|
|
->defaultValue(10) |
|
118
|
|
|
->min(1) |
|
119
|
|
|
->info('Default lifetime of the MAC') |
|
120
|
|
|
->end() |
|
121
|
|
|
->end() |
|
122
|
|
|
->end() |
|
123
|
|
|
->end(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $treeBuilder; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|