|
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\Identity\Dto\IdentitySearchQuery; |
|
22
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Service\IdentityService as LibraryIdentityService; |
|
23
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException; |
|
24
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
|
25
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\IdentityCollection; |
|
26
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RegistrationAuthorityCredentials; |
|
27
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Provides access to the Middleware API resources. |
|
31
|
|
|
*/ |
|
32
|
|
|
class IdentityService |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var LibraryIdentityService |
|
36
|
|
|
*/ |
|
37
|
|
|
private $service; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ValidatorInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $validator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param LibraryIdentityService $service |
|
46
|
|
|
* @param ValidatorInterface $validator |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(LibraryIdentityService $service, ValidatorInterface $validator) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->service = $service; |
|
51
|
|
|
$this->validator = $validator; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $id |
|
56
|
|
|
* @return null|Identity |
|
57
|
|
|
*/ |
|
58
|
|
|
public function get($id) |
|
59
|
|
|
{ |
|
60
|
|
|
$data = $this->service->get($id); |
|
61
|
|
|
|
|
62
|
|
|
if ($data === null) { |
|
63
|
|
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$identity = Identity::fromData($data); |
|
67
|
|
|
|
|
68
|
|
|
$message = sprintf("Identity '%s' retrieved from the Middleware is invalid", $id); |
|
69
|
|
|
$this->assertIsValid($identity, $message); |
|
70
|
|
|
|
|
71
|
|
|
return $identity; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param IdentitySearchQuery $searchQuery |
|
76
|
|
|
* @return \Surfnet\StepupMiddlewareClientBundle\Identity\Dto\IdentityCollection |
|
77
|
|
|
*/ |
|
78
|
|
|
public function search(IdentitySearchQuery $searchQuery) |
|
79
|
|
|
{ |
|
80
|
|
|
$data = $this->service->search($searchQuery); |
|
81
|
|
|
|
|
82
|
|
|
$collection = IdentityCollection::fromData($data); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$this->assertIsValid($collection, 'Invalid elements received in collection'); |
|
85
|
|
|
|
|
86
|
|
|
return $collection; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Identity $identity |
|
91
|
|
|
* @return RegistrationAuthorityCredentials|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getRegistrationAuthorityCredentials(Identity $identity) |
|
94
|
|
|
{ |
|
95
|
|
|
$data = $this->service->getRegistrationAuthorityCredentials($identity->id); |
|
96
|
|
|
|
|
97
|
|
|
// 404 Not Found is a valid case. |
|
98
|
|
|
if (!$data) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$credentials = RegistrationAuthorityCredentials::fromData($data); |
|
103
|
|
|
|
|
104
|
|
|
$message = sprintf('Registration Authority Credentials for Identity[%s] are invalid', $identity->id); |
|
105
|
|
|
$this->assertIsValid($credentials, $message); |
|
106
|
|
|
|
|
107
|
|
|
return $credentials; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param object $value |
|
112
|
|
|
* @param null|string $message |
|
113
|
|
|
*/ |
|
114
|
|
|
private function assertIsValid($value, $message = null) |
|
115
|
|
|
{ |
|
116
|
|
|
$violations = $this->validator->validate($value); |
|
117
|
|
|
|
|
118
|
|
|
$message = $message ?: 'Invalid Response Received'; |
|
119
|
|
|
|
|
120
|
|
|
if (count($violations) > 0) { |
|
121
|
|
|
throw InvalidResponseException::withViolations($message, $violations); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.