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\StepupGateway\GatewayBundle\Entity; |
20
|
|
|
|
21
|
|
|
use Doctrine\ORM\Mapping as ORM; |
22
|
|
|
use GuzzleHttp; |
23
|
|
|
use Surfnet\SamlBundle\Entity\IdentityProvider; |
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @ORM\Entity(repositoryClass="Surfnet\StepupGateway\GatewayBundle\Entity\DoctrineSamlEntityRepository") |
28
|
|
|
* @ORM\Table() |
29
|
|
|
* |
30
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateField) |
31
|
|
|
*/ |
32
|
|
|
class SamlEntity |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Constants denoting the type of SamlEntity. Also used in the middleware to make that distinction |
36
|
|
|
*/ |
37
|
|
|
const TYPE_IDP = 'idp'; |
38
|
|
|
const TYPE_SP = 'sp'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
* |
43
|
|
|
* @ORM\Id |
44
|
|
|
* @ORM\Column(length=36) |
45
|
|
|
*/ |
46
|
|
|
private $id; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @ORM\Column |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $entityId; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @ORM\Column |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $type; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @ORM\Column(type="text") |
64
|
|
|
* |
65
|
|
|
* @var string the configuration as json string |
66
|
|
|
*/ |
67
|
|
|
private $configuration; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return IdentityProvider |
71
|
|
|
*/ |
72
|
|
|
public function toIdentityProvider() |
73
|
|
|
{ |
74
|
|
|
if (!$this->type === self::TYPE_IDP) { |
75
|
|
|
throw new RuntimeException(sprintf( |
76
|
|
|
'Cannot cast a SAMLEntity to an IdentityProvider if it is not of the type "%s", current type: "%s"', |
77
|
|
|
self::TYPE_IDP, |
78
|
|
|
$this->type |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$decodedConfiguration = $this->decodeConfiguration(); |
83
|
|
|
|
84
|
|
|
// index based will be supported later on |
85
|
|
|
$configuration['entityId'] = $this->entityId; |
|
|
|
|
86
|
|
|
$configuration['configuredLoas'] = $decodedConfiguration['loa']; |
87
|
|
|
|
88
|
|
|
return new IdentityProvider($configuration); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return ServiceProvider |
93
|
|
|
*/ |
94
|
|
|
public function toServiceProvider() |
95
|
|
|
{ |
96
|
|
|
if (!$this->type === self::TYPE_SP) { |
97
|
|
|
throw new RuntimeException(sprintf( |
98
|
|
|
'Cannot cast a SAMLEntity to a ServiceProvider if it is not of the type "%s", current type: "%s"', |
99
|
|
|
self::TYPE_SP, |
100
|
|
|
$this->type |
101
|
|
|
)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$decodedConfiguration = $this->decodeConfiguration(); |
105
|
|
|
|
106
|
|
|
// index based will be supported later on |
107
|
|
|
$configuration['assertionConsumerUrl'] = reset($decodedConfiguration['acs']); |
|
|
|
|
108
|
|
|
$configuration['certificateData'] = $decodedConfiguration['public_key']; |
109
|
|
|
$configuration['entityId'] = $this->entityId; |
110
|
|
|
$configuration['configuredLoas'] = $decodedConfiguration['loa']; |
111
|
|
|
|
112
|
|
|
$configuration['secondFactorOnly'] = false; |
113
|
|
|
if (isset($decodedConfiguration['second_factor_only'])) { |
114
|
|
|
$configuration['secondFactorOnly'] = $decodedConfiguration['second_factor_only']; |
115
|
|
|
} |
116
|
|
|
$configuration['secondFactorOnlyNameIdPatterns'] = []; |
117
|
|
|
if (isset($decodedConfiguration['second_factor_only_nameid_patterns'])) { |
118
|
|
|
$configuration['secondFactorOnlyNameIdPatterns'] = |
119
|
|
|
$decodedConfiguration['second_factor_only_nameid_patterns']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return new ServiceProvider($configuration); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the decoded configuration |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
private function decodeConfiguration() |
131
|
|
|
{ |
132
|
|
|
return GuzzleHttp\json_decode($this->configuration, true); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.