1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\DependencyInjection; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupBundle\Exception\DomainException; |
22
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
23
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
24
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeBuilder; |
25
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
26
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
27
|
|
|
|
28
|
|
|
class Configuration implements ConfigurationInterface |
29
|
|
|
{ |
30
|
|
|
public function getConfigTreeBuilder() |
31
|
|
|
{ |
32
|
|
|
$treeBuilder = new TreeBuilder(); |
33
|
|
|
$rootNode = $treeBuilder->root('surfnet_stepup_self_service_self_service'); |
34
|
|
|
|
35
|
|
|
$childNodes = $rootNode->children(); |
36
|
|
|
$this->appendEnabledSecondFactorTypesConfiguration($childNodes); |
37
|
|
|
$this->appendSessionConfiguration($childNodes); |
38
|
|
|
|
39
|
|
|
return $treeBuilder; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param NodeBuilder $childNodes |
44
|
|
|
*/ |
45
|
|
|
private function appendSessionConfiguration(NodeBuilder $childNodes) |
46
|
|
|
{ |
47
|
|
|
$childNodes |
|
|
|
|
48
|
|
|
->arrayNode('session_lifetimes') |
49
|
|
|
->isRequired() |
50
|
|
|
->children() |
51
|
|
|
->integerNode('max_absolute_lifetime') |
52
|
|
|
->isRequired() |
53
|
|
|
->defaultValue(3600) |
54
|
|
|
->info('The maximum lifetime of a session regardless of interaction by the user, in seconds.') |
55
|
|
|
->example('3600 -> 1 hour * 60 minutes * 60 seconds') |
56
|
|
|
->validate() |
57
|
|
|
->ifTrue( |
58
|
|
|
function ($lifetime) { |
59
|
|
|
return !is_int($lifetime); |
60
|
|
|
} |
61
|
|
|
) |
62
|
|
|
->thenInvalid('max_absolute_lifetime must be an integer') |
63
|
|
|
->end() |
64
|
|
|
->end() |
65
|
|
|
->integerNode('max_relative_lifetime') |
66
|
|
|
->isRequired() |
67
|
|
|
->defaultValue(600) |
68
|
|
|
->info( |
69
|
|
|
'The maximum relative lifetime of a session; the maximum allowed time between two ' |
70
|
|
|
. 'interactions by the user' |
71
|
|
|
) |
72
|
|
|
->example('600 -> 10 minutes * 60 seconds') |
73
|
|
|
->validate() |
74
|
|
|
->ifTrue( |
75
|
|
|
function ($lifetime) { |
76
|
|
|
return !is_int($lifetime); |
77
|
|
|
} |
78
|
|
|
) |
79
|
|
|
->thenInvalid('max_relative_lifetime must be an integer') |
80
|
|
|
->end() |
81
|
|
|
->end() |
82
|
|
|
->end() |
83
|
|
|
->end(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param NodeBuilder $childNodes |
88
|
|
|
*/ |
89
|
|
|
private function appendEnabledSecondFactorTypesConfiguration(NodeBuilder $childNodes) |
90
|
|
|
{ |
91
|
|
|
$childNodes |
|
|
|
|
92
|
|
|
->arrayNode('enabled_second_factors') |
93
|
|
|
->isRequired() |
94
|
|
|
->prototype('scalar') |
95
|
|
|
->validate() |
96
|
|
|
->ifTrue( |
97
|
|
|
function ($type) { |
98
|
|
|
try { |
99
|
|
|
new SecondFactorType($type); |
100
|
|
|
} catch (InvalidArgumentException $e) { |
101
|
|
|
return true; |
102
|
|
|
} catch (DomainException $e) { |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
) |
107
|
|
|
->thenInvalid( |
108
|
|
|
'Enabled second factor type "%s" is not one of the valid types. See SecondFactorType' |
109
|
|
|
) |
110
|
|
|
->end() |
111
|
|
|
->end() |
112
|
|
|
->end(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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: