1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 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\StepupMiddleware\ManagementBundle\Validator; |
20
|
|
|
|
21
|
|
|
use Assert\Assertion; |
22
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert; |
23
|
|
|
|
24
|
|
|
class IdentityProviderConfigurationValidator implements ConfigurationValidatorInterface |
25
|
|
|
{ |
26
|
|
|
public function validate(array $configuration, $propertyPath) |
27
|
|
|
{ |
28
|
|
|
Assertion::isArray($configuration, 'invalid configuration format, must be an object', $propertyPath); |
29
|
|
|
|
30
|
|
|
$requiredProperties = [ |
31
|
|
|
'entity_id', |
32
|
|
|
'loa', |
33
|
|
|
'use_pdp' |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
if (empty($configuration['use_pdp'])) { |
37
|
|
|
$configuration['use_pdp'] = false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
StepupAssert::keysMatch( |
41
|
|
|
$configuration, |
42
|
|
|
$requiredProperties, |
43
|
|
|
sprintf( |
44
|
|
|
"The following properties must be present: '%s'; other properties are not supported", |
45
|
|
|
join("', '", $requiredProperties) |
46
|
|
|
), |
47
|
|
|
$propertyPath |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$this->validateStringValue($configuration, 'entity_id', $propertyPath); |
51
|
|
|
$this->validateLoaDefinition($configuration, $propertyPath); |
52
|
|
|
$this->validateBooleanValue($configuration, 'use_pdp', $propertyPath); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $configuration |
57
|
|
|
* @param string $name |
58
|
|
|
* @param string $propertyPath |
59
|
|
|
*/ |
60
|
|
|
private function validateStringValue($configuration, $name, $propertyPath) |
61
|
|
|
{ |
62
|
|
|
Assertion::string($configuration[$name], 'value must be a string', $propertyPath . '.' . $name); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $configuration |
67
|
|
|
* @param string $name |
68
|
|
|
* @param string $propertyPath |
69
|
|
|
*/ |
70
|
|
|
private function validateBooleanValue($configuration, $name, $propertyPath) |
71
|
|
|
{ |
72
|
|
|
Assertion::boolean($configuration[$name], 'value must be a boolean', $propertyPath . '.' . $name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $configuration |
77
|
|
|
* @param string $propertyPath |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
private function validateLoaDefinition($configuration, $propertyPath) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$value = $configuration['loa']; |
82
|
|
|
$path = $propertyPath . '.loa'; |
83
|
|
|
|
84
|
|
|
Assertion::isArray($value, 'must be an object', $path); |
85
|
|
|
Assertion::keyExists($value, '__default__', "must have the default loa set on the '__default__' property", $path); |
|
|
|
|
86
|
|
|
Assertion::allString($value, 'all properties must contain strings as values', $path); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.