|
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\CompliedWithUnverifiedSecondFactorRevocationEvent; |
|
25
|
|
|
use Surfnet\Stepup\Identity\Event\EmailVerifiedEvent; |
|
26
|
|
|
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent; |
|
27
|
|
|
use Surfnet\Stepup\Identity\Event\UnverifiedSecondFactorRevokedEvent; |
|
28
|
|
|
use Surfnet\Stepup\Identity\Value\EmailVerificationWindow; |
|
|
|
|
|
|
29
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
|
30
|
|
|
use Surfnet\Stepup\Identity\Value\SecondFactorId; |
|
31
|
|
|
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier; |
|
32
|
|
|
use Surfnet\StepupBundle\Security\OtpGenerator; |
|
33
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* A second factor whose possession has been proven by the registrant. The registrant must verify his/her e-mail |
|
37
|
|
|
* address to verify this second factor. |
|
38
|
|
|
* |
|
39
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
40
|
|
|
*/ |
|
|
|
|
|
|
41
|
|
|
class UnverifiedSecondFactor extends AbstractSecondFactor |
|
42
|
|
|
{ |
|
43
|
|
|
private ?Identity $identity = null; |
|
44
|
|
|
|
|
45
|
|
|
private ?SecondFactorId $id = null; |
|
46
|
|
|
|
|
47
|
|
|
private ?SecondFactorType $type = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var SecondFactorIdentifier |
|
51
|
|
|
*/ |
|
52
|
|
|
private SecondFactorIdentifier $secondFactorIdentifier; |
|
53
|
|
|
|
|
54
|
|
|
private ?EmailVerificationWindow $verificationWindow = null; |
|
55
|
|
|
|
|
56
|
|
|
private ?string $verificationNonce = null; |
|
57
|
|
|
|
|
58
|
|
|
public static function create( |
|
59
|
|
|
SecondFactorId $id, |
|
60
|
|
|
Identity $identity, |
|
61
|
|
|
SecondFactorType $type, |
|
62
|
|
|
SecondFactorIdentifier $secondFactorIdentifier, |
|
63
|
|
|
EmailVerificationWindow $emailVerificationWindow, |
|
64
|
|
|
string $verificationNonce, |
|
65
|
|
|
): self { |
|
66
|
|
|
if ($verificationNonce === '' || $verificationNonce === '0') { |
|
67
|
|
|
throw new InvalidArgumentException("'verificationNonce' may not be empty"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$secondFactor = new self(); |
|
71
|
|
|
$secondFactor->id = $id; |
|
72
|
|
|
$secondFactor->identity = $identity; |
|
73
|
|
|
$secondFactor->type = $type; |
|
74
|
|
|
$secondFactor->secondFactorIdentifier = $secondFactorIdentifier; |
|
75
|
|
|
$secondFactor->verificationWindow = $emailVerificationWindow; |
|
76
|
|
|
$secondFactor->verificationNonce = $verificationNonce; |
|
77
|
|
|
|
|
78
|
|
|
return $secondFactor; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
final public function __construct() |
|
82
|
|
|
{ |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getId(): ?SecondFactorId |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->id; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
|
|
|
|
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public function hasNonce(string $verificationNonce): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->verificationNonce === $verificationNonce; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function canBeVerifiedNow(): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->verificationWindow->isOpen(); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function verifyEmail(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$this->apply( |
|
109
|
|
|
new EmailVerifiedEvent( |
|
110
|
|
|
$this->identity->getId(), |
|
|
|
|
|
|
111
|
|
|
$this->identity->getInstitution(), |
|
112
|
|
|
$this->id, |
|
|
|
|
|
|
113
|
|
|
$this->type, |
|
|
|
|
|
|
114
|
|
|
$this->secondFactorIdentifier, |
|
115
|
|
|
DateTime::now(), |
|
116
|
|
|
OtpGenerator::generate(8), |
|
117
|
|
|
$this->identity->getCommonName(), |
|
118
|
|
|
$this->identity->getEmail(), |
|
119
|
|
|
$this->identity->getPreferredLocale(), |
|
120
|
|
|
), |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function revoke(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$this->apply( |
|
127
|
|
|
new UnverifiedSecondFactorRevokedEvent( |
|
128
|
|
|
$this->identity->getId(), |
|
129
|
|
|
$this->identity->getInstitution(), |
|
130
|
|
|
$this->id, |
|
|
|
|
|
|
131
|
|
|
$this->type, |
|
|
|
|
|
|
132
|
|
|
$this->secondFactorIdentifier, |
|
133
|
|
|
), |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function complyWithRevocation(IdentityId $authorityId): void |
|
138
|
|
|
{ |
|
139
|
|
|
$this->apply( |
|
140
|
|
|
new CompliedWithUnverifiedSecondFactorRevocationEvent( |
|
141
|
|
|
$this->identity->getId(), |
|
142
|
|
|
$this->identity->getInstitution(), |
|
143
|
|
|
$this->id, |
|
|
|
|
|
|
144
|
|
|
$this->type, |
|
|
|
|
|
|
145
|
|
|
$this->secondFactorIdentifier, |
|
146
|
|
|
$authorityId, |
|
147
|
|
|
), |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function asVerified(DateTime $registrationRequestedAt, string $registrationCode): VerifiedSecondFactor |
|
152
|
|
|
{ |
|
153
|
|
|
return VerifiedSecondFactor::create( |
|
154
|
|
|
$this->id, |
|
|
|
|
|
|
155
|
|
|
$this->identity, |
|
|
|
|
|
|
156
|
|
|
$this->type, |
|
|
|
|
|
|
157
|
|
|
$this->secondFactorIdentifier, |
|
158
|
|
|
$registrationRequestedAt, |
|
159
|
|
|
$registrationCode, |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event): void |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
$secondFactorIdentifierClass = $this->secondFactorIdentifier::class; |
|
166
|
|
|
|
|
167
|
|
|
$this->secondFactorIdentifier = $secondFactorIdentifierClass::unknown(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function getType(): SecondFactorType |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->type; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getIdentifier(): SecondFactorIdentifier |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->secondFactorIdentifier; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|