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 VettedSecondFactor|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 getVetted($secondFactorId) |
130
|
|
|
{ |
131
|
|
|
$data = $this->service->getVetted($secondFactorId); |
132
|
|
|
|
133
|
|
|
if ($data === null) { |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$secondFactor = VettedSecondFactor::fromData($data); |
138
|
|
|
$violations = $this->validator->validate($secondFactor); |
139
|
|
|
|
140
|
|
|
if (count($violations) > 0) { |
141
|
|
|
throw InvalidResponseException::withViolations( |
142
|
|
|
"Vetted second factor retrieved from the Middleware was invalid", |
143
|
|
|
$violations |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $secondFactor; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param UnverifiedSecondFactorSearchQuery $query |
152
|
|
|
* @return UnverifiedSecondFactorCollection |
|
|
|
|
153
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
154
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
155
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
156
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
157
|
|
|
*/ |
158
|
|
|
public function searchUnverified(UnverifiedSecondFactorSearchQuery $query) |
159
|
|
|
{ |
160
|
|
|
$data = $this->service->searchUnverified($query); |
161
|
|
|
|
162
|
|
|
if ($data === null) { |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$secondFactors = UnverifiedSecondFactorCollection::fromData($data); |
167
|
|
|
$violations = $this->validator->validate($secondFactors); |
168
|
|
|
|
169
|
|
|
if (count($violations) > 0) { |
170
|
|
|
throw InvalidResponseException::withViolations( |
171
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
172
|
|
|
$violations |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $secondFactors; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param VerifiedSecondFactorSearchQuery $query |
181
|
|
|
* @return VerifiedSecondFactorCollection |
|
|
|
|
182
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
183
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
184
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
185
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
186
|
|
|
*/ |
187
|
|
|
public function searchVerified(VerifiedSecondFactorSearchQuery $query) |
188
|
|
|
{ |
189
|
|
|
$data = $this->service->searchVerified($query); |
190
|
|
|
|
191
|
|
|
if ($data === null) { |
192
|
|
|
return null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$secondFactors = VerifiedSecondFactorCollection::fromData($data); |
196
|
|
|
$violations = $this->validator->validate($secondFactors); |
197
|
|
|
|
198
|
|
|
if (count($violations) > 0) { |
199
|
|
|
throw InvalidResponseException::withViolations( |
200
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
201
|
|
|
$violations |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $secondFactors; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param VerifiedSecondFactorOfIdentitySearchQuery $query |
210
|
|
|
* @return VerifiedSecondFactorCollection |
|
|
|
|
211
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
212
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
213
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
214
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
215
|
|
|
*/ |
216
|
|
|
public function searchOwnVerified(VerifiedSecondFactorOfIdentitySearchQuery $query) |
217
|
|
|
{ |
218
|
|
|
$data = $this->service->searchOwnVerified($query); |
219
|
|
|
|
220
|
|
|
if ($data === null) { |
221
|
|
|
return null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$secondFactors = VerifiedSecondFactorCollection::fromData($data); |
225
|
|
|
$violations = $this->validator->validate($secondFactors); |
226
|
|
|
|
227
|
|
|
if (count($violations) > 0) { |
228
|
|
|
throw InvalidResponseException::withViolations( |
229
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
230
|
|
|
$violations |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $secondFactors; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param VettedSecondFactorSearchQuery $query |
239
|
|
|
* @return VettedSecondFactorCollection |
|
|
|
|
240
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
241
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
242
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
243
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
244
|
|
|
*/ |
245
|
|
|
public function searchVetted(VettedSecondFactorSearchQuery $query) |
246
|
|
|
{ |
247
|
|
|
$data = $this->service->searchVetted($query); |
248
|
|
|
|
249
|
|
|
if ($data === null) { |
250
|
|
|
return null; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$secondFactors = VettedSecondFactorCollection::fromData($data); |
254
|
|
|
$violations = $this->validator->validate($secondFactors); |
255
|
|
|
|
256
|
|
|
if (count($violations) > 0) { |
257
|
|
|
throw InvalidResponseException::withViolations( |
258
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
259
|
|
|
$violations |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $secondFactors; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
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.