1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2019 SURFnet B.V. |
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\StepupSelfService\SelfServiceBundle\Service; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
23
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\AttributeMapper; |
24
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto\AttributeListDto; |
25
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto\RemoteVettingTokenDto; |
26
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Encryption\IdentityData; |
27
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Encryption\IdentityEncrypterInterface; |
28
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\RemoteVettingContext; |
29
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeCollectionAggregate; |
30
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\AttributeMatchCollection; |
31
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\FeedbackCollection; |
32
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\ProcessId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
36
|
|
|
*/ |
37
|
|
|
class RemoteVettingService |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
private $logger; |
43
|
|
|
/** |
44
|
|
|
* @var RemoteVettingContext |
45
|
|
|
*/ |
46
|
|
|
private $remoteVettingContext; |
47
|
|
|
/** |
48
|
|
|
* @var IdentityEncrypterInterface |
49
|
|
|
*/ |
50
|
|
|
private $identityEncrypter; |
51
|
|
|
/** |
52
|
|
|
* @var AttributeMapper |
53
|
|
|
*/ |
54
|
|
|
private $attributeMapper; |
55
|
|
|
/** |
56
|
|
|
* @var ApplicationHelper |
57
|
|
|
*/ |
58
|
|
|
private $applicationHelper; |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
public function __construct( |
62
|
|
|
RemoteVettingContext $remoteVettingContext, |
63
|
|
|
AttributeMapper $attributeMapper, |
64
|
|
|
IdentityEncrypterInterface $identityEncrypter, |
65
|
|
|
ApplicationHelper $applicationHelper, |
66
|
|
|
LoggerInterface $logger |
67
|
|
|
) { |
68
|
|
|
$this->remoteVettingContext = $remoteVettingContext; |
69
|
|
|
$this->logger = $logger; |
70
|
|
|
$this->identityEncrypter = $identityEncrypter; |
71
|
|
|
$this->attributeMapper = $attributeMapper; |
72
|
|
|
$this->applicationHelper = $applicationHelper; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $identityProviderSlug |
77
|
|
|
* @param RemoteVettingTokenDto $remoteVettingToken |
78
|
|
|
*/ |
79
|
|
|
public function start($identityProviderSlug, RemoteVettingTokenDto $remoteVettingToken) |
80
|
|
|
{ |
81
|
|
|
$this->logger->notice('Starting a remote vetting process', [ |
82
|
|
|
'second-factor' => $remoteVettingToken->getSecondFactorId(), |
83
|
|
|
'identity' => $remoteVettingToken->getIdentityId(), |
84
|
|
|
'provider' => $identityProviderSlug, |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
$this->remoteVettingContext->initialize($identityProviderSlug, $remoteVettingToken); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ProcessId $processId |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function startValidation(ProcessId $processId) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->logger->notice('Starting a remote vetting authentication', [ |
96
|
|
|
'second-factor' => $this->remoteVettingContext->getTokenId(), |
97
|
|
|
'process' => $processId->getProcessId(), |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
$this->remoteVettingContext->validating($processId); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param ProcessId $processId |
105
|
|
|
* @param AttributeListDto $externalAttributes |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function finishValidation(ProcessId $processId, AttributeListDto $externalAttributes) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$this->logger->notice('Finishing a remote vetting authentication', [ |
110
|
|
|
'second-factor' => $this->remoteVettingContext->getTokenId(), |
111
|
|
|
'process' => $processId->getProcessId(), |
112
|
|
|
]); |
113
|
|
|
|
114
|
|
|
$this->remoteVettingContext->validated($processId, $externalAttributes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ProcessId $processId |
119
|
|
|
* @param Identity $identity |
120
|
|
|
* @param AttributeListDto $localAttributes |
121
|
|
|
* @param AttributeMatchCollection $attributeMatches |
122
|
|
|
* @param FeedbackCollection $feedback |
123
|
|
|
* @return RemoteVettingTokenDto |
124
|
|
|
*/ |
125
|
|
|
public function done( |
126
|
|
|
ProcessId $processId, |
127
|
|
|
Identity $identity, |
128
|
|
|
AttributeListDto $localAttributes, |
129
|
|
|
AttributeMatchCollection $attributeMatches, |
130
|
|
|
FeedbackCollection $feedback |
131
|
|
|
) { |
132
|
|
|
$this->remoteVettingContext->done($processId); |
133
|
|
|
$this->logger->notice('Saving the encrypted match data to the filesystem', [ |
134
|
|
|
'second-factor' => $this->remoteVettingContext->getTokenId(), |
135
|
|
|
'process' => $processId->getProcessId(), |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
$identityData = $this->aggregateIdentityData($identity, $localAttributes, $attributeMatches, $feedback); |
139
|
|
|
$this->identityEncrypter->encrypt($identityData->serialize()); |
140
|
|
|
|
141
|
|
|
$this->logger->notice('Finished the remote vetting process', [ |
142
|
|
|
'second-factor' => $this->remoteVettingContext->getTokenId(), |
143
|
|
|
'process' => $processId->getProcessId(), |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
return $this->remoteVettingContext->getValidatedToken(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param AttributeListDto $localAttributes |
151
|
|
|
* @return AttributeMatchCollection |
152
|
|
|
*/ |
153
|
|
|
public function getAttributeMatchCollection(AttributeListDto $localAttributes) |
154
|
|
|
{ |
155
|
|
|
$externalAttributes = $this->remoteVettingContext->getAttributes(); |
156
|
|
|
$identityProviderSlug = $this->remoteVettingContext->getIdentityProviderSlug(); |
157
|
|
|
|
158
|
|
|
return $this->attributeMapper->map($identityProviderSlug, $localAttributes, $externalAttributes); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getActiveIdentityProviderSlug() |
165
|
|
|
{ |
166
|
|
|
return $this->remoteVettingContext->getIdentityProviderSlug(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param Identity $identity |
171
|
|
|
* @param AttributeListDto $localAttributes |
172
|
|
|
* @param AttributeMatchCollection $attributeMatches |
173
|
|
|
* @param FeedbackCollection $feedback |
174
|
|
|
* @return IdentityData |
175
|
|
|
*/ |
176
|
|
|
private function aggregateIdentityData( |
177
|
|
|
Identity $identity, |
178
|
|
|
AttributeListDto $localAttributes, |
179
|
|
|
AttributeMatchCollection $attributeMatches, |
180
|
|
|
FeedbackCollection $feedback |
181
|
|
|
) { |
182
|
|
|
$nameId = $identity->nameId; |
183
|
|
|
$institution = $identity->institution; |
184
|
|
|
$version = $this->applicationHelper->getApplicationVersion(); |
185
|
|
|
$remoteVettingSource = $this->remoteVettingContext->getIdentityProviderSlug(); |
186
|
|
|
|
187
|
|
|
$attributeCollectionAggregate = new AttributeCollectionAggregate(); |
188
|
|
|
$attributeCollectionAggregate->add('local-attributes', $localAttributes); |
189
|
|
|
$attributeCollectionAggregate->add('remote-attributes', $this->remoteVettingContext->getAttributes()); |
190
|
|
|
$attributeCollectionAggregate->add('matching-results', $attributeMatches); |
191
|
|
|
$attributeCollectionAggregate->add('feedback', $feedback); |
192
|
|
|
|
193
|
|
|
return new IdentityData( |
194
|
|
|
$attributeCollectionAggregate, |
195
|
|
|
$nameId, |
196
|
|
|
$version, |
197
|
|
|
$institution, |
198
|
|
|
$remoteVettingSource |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.