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\VerifiedSecondFactorOfIdentitySearchQuery; |
26
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\VerifiedSecondFactorSearchQuery; |
27
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\VettedSecondFactorSearchQuery; |
28
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Service\SecondFactorService as LibrarySecondFactorService; |
29
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException; |
30
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactor; |
31
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactorCollection; |
32
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactor; |
33
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactorCollection; |
34
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactor; |
35
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactorCollection; |
36
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
40
|
|
|
*/ |
41
|
|
|
class SecondFactorService |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var LibrarySecondFactorService |
45
|
|
|
*/ |
46
|
|
|
private $service; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ValidatorInterface |
50
|
|
|
*/ |
51
|
|
|
private $validator; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param LibrarySecondFactorService $service |
55
|
|
|
* @param ValidatorInterface $validator |
56
|
|
|
*/ |
57
|
|
|
public function __construct(LibrarySecondFactorService $service, ValidatorInterface $validator) |
58
|
|
|
{ |
59
|
|
|
$this->service = $service; |
60
|
|
|
$this->validator = $validator; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $secondFactorId |
65
|
|
|
* @return UnverifiedSecondFactor|null |
66
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
67
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
68
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
69
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
70
|
|
|
*/ |
71
|
|
|
public function getUnverified($secondFactorId) |
72
|
|
|
{ |
73
|
|
|
$data = $this->service->getUnverified($secondFactorId); |
74
|
|
|
|
75
|
|
|
if ($data === null) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$secondFactor = UnverifiedSecondFactor::fromData($data); |
80
|
|
|
$violations = $this->validator->validate($secondFactor); |
81
|
|
|
|
82
|
|
|
if (count($violations) > 0) { |
83
|
|
|
throw InvalidResponseException::withViolations( |
84
|
|
|
"Unverified second factor retrieved from the Middleware was invalid", |
85
|
|
|
$violations |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $secondFactor; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $secondFactorId |
94
|
|
|
* @return VerifiedSecondFactor|null |
95
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
96
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
97
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
98
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
99
|
|
|
*/ |
100
|
|
|
public function getVerified($secondFactorId) |
101
|
|
|
{ |
102
|
|
|
$data = $this->service->getVerified($secondFactorId); |
103
|
|
|
|
104
|
|
|
if ($data === null) { |
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$secondFactor = VerifiedSecondFactor::fromData($data); |
109
|
|
|
$violations = $this->validator->validate($secondFactor); |
110
|
|
|
|
111
|
|
|
if (count($violations) > 0) { |
112
|
|
|
throw InvalidResponseException::withViolations( |
113
|
|
|
"Verified second factor retrieved from the Middleware was invalid", |
114
|
|
|
$violations |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $secondFactor; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $secondFactorId |
123
|
|
|
* @return bool|null |
124
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
125
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
126
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
127
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
128
|
|
|
*/ |
129
|
|
|
public function getVerifiedCanSkipProvePossession($secondFactorId) |
130
|
|
|
{ |
131
|
|
|
$data = $this->service->getVerifiedCanSkipProvePossession($secondFactorId); |
132
|
|
|
|
133
|
|
|
return (bool)$data; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $secondFactorId |
138
|
|
|
* @return VettedSecondFactor|null |
139
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
140
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
141
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
142
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
143
|
|
|
*/ |
144
|
|
|
public function getVetted($secondFactorId) |
145
|
|
|
{ |
146
|
|
|
$data = $this->service->getVetted($secondFactorId); |
147
|
|
|
|
148
|
|
|
if ($data === null) { |
149
|
|
|
return null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$secondFactor = VettedSecondFactor::fromData($data); |
153
|
|
|
$violations = $this->validator->validate($secondFactor); |
154
|
|
|
|
155
|
|
|
if (count($violations) > 0) { |
156
|
|
|
throw InvalidResponseException::withViolations( |
157
|
|
|
"Vetted second factor retrieved from the Middleware was invalid", |
158
|
|
|
$violations |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $secondFactor; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param UnverifiedSecondFactorSearchQuery $query |
167
|
|
|
* @return UnverifiedSecondFactorCollection |
|
|
|
|
168
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
169
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
170
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
171
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
172
|
|
|
*/ |
173
|
|
|
public function searchUnverified(UnverifiedSecondFactorSearchQuery $query) |
174
|
|
|
{ |
175
|
|
|
$data = $this->service->searchUnverified($query); |
176
|
|
|
|
177
|
|
|
if ($data === null) { |
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$secondFactors = UnverifiedSecondFactorCollection::fromData($data); |
182
|
|
|
$violations = $this->validator->validate($secondFactors); |
183
|
|
|
|
184
|
|
|
if (count($violations) > 0) { |
185
|
|
|
throw InvalidResponseException::withViolations( |
186
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
187
|
|
|
$violations |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $secondFactors; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param VerifiedSecondFactorSearchQuery $query |
196
|
|
|
* @return VerifiedSecondFactorCollection |
|
|
|
|
197
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
198
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
199
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
200
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
201
|
|
|
*/ |
202
|
|
|
public function searchVerified(VerifiedSecondFactorSearchQuery $query) |
203
|
|
|
{ |
204
|
|
|
$data = $this->service->searchVerified($query); |
205
|
|
|
|
206
|
|
|
if ($data === null) { |
207
|
|
|
return null; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$secondFactors = VerifiedSecondFactorCollection::fromData($data); |
211
|
|
|
$violations = $this->validator->validate($secondFactors); |
212
|
|
|
|
213
|
|
|
if (count($violations) > 0) { |
214
|
|
|
throw InvalidResponseException::withViolations( |
215
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
216
|
|
|
$violations |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $secondFactors; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param VerifiedSecondFactorOfIdentitySearchQuery $query |
225
|
|
|
* @return VerifiedSecondFactorCollection |
|
|
|
|
226
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
227
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
228
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
229
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
230
|
|
|
*/ |
231
|
|
|
public function searchOwnVerified(VerifiedSecondFactorOfIdentitySearchQuery $query) |
232
|
|
|
{ |
233
|
|
|
$data = $this->service->searchOwnVerified($query); |
234
|
|
|
|
235
|
|
|
if ($data === null) { |
236
|
|
|
return null; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$secondFactors = VerifiedSecondFactorCollection::fromData($data); |
240
|
|
|
$violations = $this->validator->validate($secondFactors); |
241
|
|
|
|
242
|
|
|
if (count($violations) > 0) { |
243
|
|
|
throw InvalidResponseException::withViolations( |
244
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
245
|
|
|
$violations |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $secondFactors; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param VettedSecondFactorSearchQuery $query |
254
|
|
|
* @return VettedSecondFactorCollection |
|
|
|
|
255
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
256
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
257
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
258
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
259
|
|
|
*/ |
260
|
|
|
public function searchVetted(VettedSecondFactorSearchQuery $query) |
261
|
|
|
{ |
262
|
|
|
$data = $this->service->searchVetted($query); |
263
|
|
|
|
264
|
|
|
if ($data === null) { |
265
|
|
|
return null; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$secondFactors = VettedSecondFactorCollection::fromData($data); |
269
|
|
|
$violations = $this->validator->validate($secondFactors); |
270
|
|
|
|
271
|
|
|
if (count($violations) > 0) { |
272
|
|
|
throw InvalidResponseException::withViolations( |
273
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
274
|
|
|
$violations |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $secondFactors; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.