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\Stepup\Identity\Entity; |
20
|
|
|
|
21
|
|
|
use Surfnet\Stepup\DateTime\DateTime; |
22
|
|
|
use Surfnet\Stepup\Exception\InvalidArgumentException; |
23
|
|
|
use Surfnet\Stepup\Identity\Api\Identity; |
24
|
|
|
use Surfnet\Stepup\Identity\Event\CompliedWithVerifiedSecondFactorRevocationEvent; |
25
|
|
|
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent; |
26
|
|
|
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent; |
27
|
|
|
use Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent; |
28
|
|
|
use Surfnet\Stepup\Identity\Value\DocumentNumber; |
29
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
30
|
|
|
use Surfnet\Stepup\Identity\Value\SecondFactorId; |
31
|
|
|
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier; |
32
|
|
|
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory; |
33
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* A second factor whose possession has been proven by the registrant and the registrant's e-mail address has been |
37
|
|
|
* verified. The registrant must visit a registration authority next. |
38
|
|
|
* |
39
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateFields) |
40
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
41
|
|
|
*/ |
42
|
|
|
class VerifiedSecondFactor extends AbstractSecondFactor |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var \Surfnet\Stepup\Identity\Value\SecondFactorId |
46
|
|
|
*/ |
47
|
|
|
private $id; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \Surfnet\Stepup\Identity\Api\Identity |
51
|
|
|
*/ |
52
|
|
|
private $identity; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var \Surfnet\StepupBundle\Value\SecondFactorType |
56
|
|
|
*/ |
57
|
|
|
private $type; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var \Surfnet\Stepup\Identity\Value\SecondFactorIdentifier |
61
|
|
|
*/ |
62
|
|
|
private $secondFactorIdentifier; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var \Surfnet\Stepup\DateTime\DateTime |
66
|
|
|
*/ |
67
|
|
|
private $registrationRequestedAt; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $registrationCode; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param SecondFactorId $id |
76
|
|
|
* @param Identity $identity |
77
|
|
|
* @param SecondFactorType $type |
78
|
|
|
* @param SecondFactorIdentifier $secondFactorIdentifier |
79
|
|
|
* @param DateTime $registrationRequestedAt |
80
|
|
|
* @param string $registrationCode |
81
|
|
|
* @return self |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public static function create( |
|
|
|
|
84
|
|
|
SecondFactorId $id, |
85
|
|
|
Identity $identity, |
86
|
|
|
SecondFactorType $type, |
87
|
|
|
SecondFactorIdentifier $secondFactorIdentifier, |
88
|
|
|
DateTime $registrationRequestedAt, |
89
|
|
|
$registrationCode |
90
|
|
|
) { |
91
|
|
|
if (!is_string($registrationCode)) { |
92
|
|
|
throw InvalidArgumentException::invalidType('string', 'registrationCode', $registrationCode); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$secondFactor = new self; |
96
|
|
|
$secondFactor->id = $id; |
97
|
|
|
$secondFactor->identity = $identity; |
98
|
|
|
$secondFactor->type = $type; |
99
|
|
|
$secondFactor->secondFactorIdentifier = $secondFactorIdentifier; |
100
|
|
|
$secondFactor->registrationRequestedAt = $registrationRequestedAt; |
101
|
|
|
$secondFactor->registrationCode = $registrationCode; |
102
|
|
|
|
103
|
|
|
return $secondFactor; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
final private function __construct() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return SecondFactorId |
112
|
|
|
*/ |
113
|
|
|
public function getId() |
114
|
|
|
{ |
115
|
|
|
return $this->id; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $registrationCode |
120
|
|
|
* @param SecondFactorIdentifier $secondFactorIdentifier |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function hasRegistrationCodeAndIdentifier($registrationCode, SecondFactorIdentifier $secondFactorIdentifier) |
124
|
|
|
{ |
125
|
|
|
return strcasecmp($registrationCode, $this->registrationCode) === 0 |
126
|
|
|
&& $secondFactorIdentifier->equals($this->secondFactorIdentifier); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function canBeVettedNow() |
133
|
|
|
{ |
134
|
|
|
return !DateTime::now()->comesAfter( |
135
|
|
|
$this->registrationRequestedAt |
136
|
|
|
->add(new \DateInterval('P14D')) |
137
|
|
|
->endOfDay() |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function vet(DocumentNumber $documentNumber) |
142
|
|
|
{ |
143
|
|
|
$this->apply( |
144
|
|
|
new SecondFactorVettedEvent( |
145
|
|
|
$this->identity->getId(), |
146
|
|
|
$this->identity->getNameId(), |
147
|
|
|
$this->identity->getInstitution(), |
148
|
|
|
$this->id, |
149
|
|
|
$this->type, |
150
|
|
|
$this->secondFactorIdentifier, |
151
|
|
|
$documentNumber, |
152
|
|
|
$this->identity->getCommonName(), |
153
|
|
|
$this->identity->getEmail(), |
154
|
|
|
$this->identity->getPreferredLocale() |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function revoke() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this->apply( |
162
|
|
|
new VerifiedSecondFactorRevokedEvent( |
163
|
|
|
$this->identity->getId(), |
164
|
|
|
$this->identity->getInstitution(), |
165
|
|
|
$this->id, |
166
|
|
|
$this->type, |
167
|
|
|
$this->secondFactorIdentifier |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
public function complyWithRevocation(IdentityId $authorityId) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$this->apply( |
175
|
|
|
new CompliedWithVerifiedSecondFactorRevocationEvent( |
176
|
|
|
$this->identity->getId(), |
177
|
|
|
$this->identity->getInstitution(), |
178
|
|
|
$this->id, |
179
|
|
|
$this->type, |
180
|
|
|
$this->secondFactorIdentifier, |
181
|
|
|
$authorityId |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return VettedSecondFactor |
188
|
|
|
*/ |
189
|
|
|
public function asVetted() |
190
|
|
|
{ |
191
|
|
|
return VettedSecondFactor::create( |
192
|
|
|
$this->id, |
193
|
|
|
$this->identity, |
194
|
|
|
$this->type, |
195
|
|
|
$this->secondFactorIdentifier |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$secondFactorIdentifierClass = get_class($this->secondFactorIdentifier); |
202
|
|
|
|
203
|
|
|
$this->secondFactorIdentifier = $secondFactorIdentifierClass::unknown(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function getType() |
207
|
|
|
{ |
208
|
|
|
return $this->type; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
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.