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\StepupBundle\Value; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupBundle\Exception\DomainException; |
22
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Value object representing the different LOAs that can be configured |
26
|
|
|
*/ |
27
|
|
|
class Loa |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The different levels |
31
|
|
|
*/ |
32
|
|
|
const LOA_1 = 1; |
33
|
|
|
const LOA_2 = 2; |
34
|
|
|
const LOA_3 = 3; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $level; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $authnContextClasses; |
45
|
|
|
|
46
|
|
|
public static function getLevels() |
47
|
|
|
{ |
48
|
|
|
return [self::LOA_1, self::LOA_2, self::LOA_3]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Loa constructor. |
53
|
|
|
* @param $level |
54
|
|
|
* @param AuthnContextClass[] $authnContextClasses |
55
|
|
|
*/ |
56
|
|
|
public function __construct($level, array $authnContextClasses) |
57
|
|
|
{ |
58
|
|
|
$possibleLevels = static::getLevels(); |
59
|
|
|
if (!in_array($level, $possibleLevels, true)) { |
60
|
|
|
throw new DomainException(sprintf( |
61
|
|
|
'Unknown loa level "%s", known levels: "%s"', |
62
|
|
|
is_object($level) ? get_class($level) : $level, |
63
|
|
|
implode('", "', $possibleLevels) |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($authnContextClasses as $authnContextClass) { |
68
|
|
|
if (!$authnContextClass instanceof AuthnContextClass) { |
69
|
|
|
throw InvalidArgumentException::invalidType( |
70
|
|
|
'\Surfnet\StepupBundle\Value', |
71
|
|
|
'authnContextClassRefs', |
72
|
|
|
$authnContextClass |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->level = $level; |
78
|
|
|
$this->authnContextClasses = $authnContextClasses; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $identifier |
|
|
|
|
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function isIdentifiedBy($authnContextClassRef) |
86
|
|
|
{ |
87
|
|
|
foreach ($this->authnContextClasses as $authnContextClass) { |
|
|
|
|
88
|
|
|
if ($authnContextClass->isIdentifiedBy($authnContextClassRef)) { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $type |
97
|
|
|
* @return AuthnContextClass |
98
|
|
|
*/ |
99
|
|
|
public function fetchAuthnContextClassOfType($type) |
100
|
|
|
{ |
101
|
|
|
foreach ($this->authnContextClasses as $authnContextClass) { |
|
|
|
|
102
|
|
|
if ($authnContextClass->isOfType($type)) { |
103
|
|
|
return $authnContextClass; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new \RuntimeException( |
108
|
|
|
sprintf('No AuthnContextClass of type "%s"', $type) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param int $level |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function levelIsLowerOrEqualTo($level) |
117
|
|
|
{ |
118
|
|
|
return $this->level <= $level; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param int $level |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function levelIsHigherOrEqualTo($level) |
126
|
|
|
{ |
127
|
|
|
return $this->level >= $level; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Loa $loa |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function canSatisfyLoa(Loa $loa) |
135
|
|
|
{ |
136
|
|
|
return $loa->levelIsLowerOrEqualTo($this->level); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Loa $loa |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function equals(Loa $loa) |
144
|
|
|
{ |
145
|
|
|
$myClassRefs = []; |
146
|
|
|
foreach ($this->authnContextClasses as $class) { |
|
|
|
|
147
|
|
|
$myClassRefs[] = (string) $class; |
148
|
|
|
} |
149
|
|
|
$theirClassRefs = []; |
150
|
|
|
foreach ($loa->authnContextClasses as $class) { |
|
|
|
|
151
|
|
|
$theirClassRefs[] = (string) $class; |
152
|
|
|
} |
153
|
|
|
return $this->level === $loa->level |
154
|
|
|
&& empty(array_diff($myClassRefs, $theirClassRefs)) |
155
|
|
|
&& empty(array_diff($theirClassRefs, $myClassRefs)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $loaLevel |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isOfLevel($loaLevel) |
163
|
|
|
{ |
164
|
|
|
return $this->level === $loaLevel; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function __toString() |
168
|
|
|
{ |
169
|
|
|
return (string) $this->level; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..