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