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\StepupMiddlewareClientBundle\Identity\Service; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupMiddlewareClient\Exception\AccessDeniedToResourceException; |
22
|
|
|
use Surfnet\StepupMiddlewareClient\Exception\MalformedResponseException; |
23
|
|
|
use Surfnet\StepupMiddlewareClient\Exception\ResourceReadException; |
24
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\UnverifiedSecondFactorSearchQuery; |
25
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\VerifiedSecondFactorSearchQuery; |
26
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\VettedSecondFactorSearchQuery; |
27
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Service\SecondFactorService as LibrarySecondFactorService; |
28
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException; |
29
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactor; |
30
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactorCollection; |
31
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactor; |
32
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactorCollection; |
33
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactor; |
34
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactorCollection; |
35
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
39
|
|
|
*/ |
40
|
|
|
class SecondFactorService |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var LibrarySecondFactorService |
44
|
|
|
*/ |
45
|
|
|
private $service; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ValidatorInterface |
49
|
|
|
*/ |
50
|
|
|
private $validator; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LibrarySecondFactorService $service |
54
|
|
|
* @param ValidatorInterface $validator |
55
|
|
|
*/ |
56
|
|
|
public function __construct(LibrarySecondFactorService $service, ValidatorInterface $validator) |
57
|
|
|
{ |
58
|
|
|
$this->service = $service; |
59
|
|
|
$this->validator = $validator; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $secondFactorId |
64
|
|
|
* @return UnverifiedSecondFactor|null |
65
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
66
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
67
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
68
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
69
|
|
|
*/ |
70
|
|
|
public function getUnverified($secondFactorId) |
71
|
|
|
{ |
72
|
|
|
$data = $this->service->getUnverified($secondFactorId); |
73
|
|
|
|
74
|
|
|
if ($data === null) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$secondFactor = UnverifiedSecondFactor::fromData($data); |
79
|
|
|
$violations = $this->validator->validate($secondFactor); |
80
|
|
|
|
81
|
|
|
if (count($violations) > 0) { |
82
|
|
|
throw InvalidResponseException::withViolations( |
83
|
|
|
"Unverified second factor retrieved from the Middleware was invalid", |
|
|
|
|
84
|
|
|
$violations |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $secondFactor; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $secondFactorId |
93
|
|
|
* @return VerifiedSecondFactor|null |
94
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
95
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
96
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
97
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
98
|
|
|
*/ |
99
|
|
|
public function getVerified($secondFactorId) |
100
|
|
|
{ |
101
|
|
|
$data = $this->service->getVerified($secondFactorId); |
102
|
|
|
|
103
|
|
|
if ($data === null) { |
104
|
|
|
return null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$secondFactor = VerifiedSecondFactor::fromData($data); |
108
|
|
|
$violations = $this->validator->validate($secondFactor); |
109
|
|
|
|
110
|
|
|
if (count($violations) > 0) { |
111
|
|
|
throw InvalidResponseException::withViolations( |
112
|
|
|
"Verified second factor retrieved from the Middleware was invalid", |
|
|
|
|
113
|
|
|
$violations |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $secondFactor; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $secondFactorId |
122
|
|
|
* @return VettedSecondFactor|null |
123
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
124
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
125
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
126
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
127
|
|
|
*/ |
128
|
|
|
public function getVetted($secondFactorId) |
129
|
|
|
{ |
130
|
|
|
$data = $this->service->getVetted($secondFactorId); |
131
|
|
|
|
132
|
|
|
if ($data === null) { |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$secondFactor = VettedSecondFactor::fromData($data); |
137
|
|
|
$violations = $this->validator->validate($secondFactor); |
138
|
|
|
|
139
|
|
|
if (count($violations) > 0) { |
140
|
|
|
throw InvalidResponseException::withViolations( |
141
|
|
|
"Vetted second factor retrieved from the Middleware was invalid", |
|
|
|
|
142
|
|
|
$violations |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $secondFactor; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param UnverifiedSecondFactorSearchQuery $query |
151
|
|
|
* @return UnverifiedSecondFactorCollection |
|
|
|
|
152
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
153
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
154
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
155
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
156
|
|
|
*/ |
157
|
|
|
public function searchUnverified(UnverifiedSecondFactorSearchQuery $query) |
158
|
|
|
{ |
159
|
|
|
$data = $this->service->searchUnverified($query); |
160
|
|
|
|
161
|
|
|
if ($data === null) { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$secondFactors = UnverifiedSecondFactorCollection::fromData($data); |
166
|
|
|
$violations = $this->validator->validate($secondFactors); |
167
|
|
|
|
168
|
|
|
if (count($violations) > 0) { |
169
|
|
|
throw InvalidResponseException::withViolations( |
170
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
|
|
|
|
171
|
|
|
$violations |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $secondFactors; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param VerifiedSecondFactorSearchQuery $query |
180
|
|
|
* @return VerifiedSecondFactorCollection |
|
|
|
|
181
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
182
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
183
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
184
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
185
|
|
|
*/ |
186
|
|
|
public function searchVerified(VerifiedSecondFactorSearchQuery $query) |
187
|
|
|
{ |
188
|
|
|
$data = $this->service->searchVerified($query); |
189
|
|
|
|
190
|
|
|
if ($data === null) { |
191
|
|
|
return null; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$secondFactors = VerifiedSecondFactorCollection::fromData($data); |
195
|
|
|
$violations = $this->validator->validate($secondFactors); |
196
|
|
|
|
197
|
|
|
if (count($violations) > 0) { |
198
|
|
|
throw InvalidResponseException::withViolations( |
199
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
|
|
|
|
200
|
|
|
$violations |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $secondFactors; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param VettedSecondFactorSearchQuery $query |
209
|
|
|
* @return VettedSecondFactorCollection |
|
|
|
|
210
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
211
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
212
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
213
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
214
|
|
|
*/ |
215
|
|
|
public function searchVetted(VettedSecondFactorSearchQuery $query) |
216
|
|
|
{ |
217
|
|
|
$data = $this->service->searchVetted($query); |
218
|
|
|
|
219
|
|
|
if ($data === null) { |
220
|
|
|
return null; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$secondFactors = VettedSecondFactorCollection::fromData($data); |
224
|
|
|
$violations = $this->validator->validate($secondFactors); |
225
|
|
|
|
226
|
|
|
if (count($violations) > 0) { |
227
|
|
|
throw InvalidResponseException::withViolations( |
228
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
|
|
|
|
229
|
|
|
$violations |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $secondFactors; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.