|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Model; |
|
6
|
|
|
|
|
7
|
|
|
use LM\AuthAbstractor\Enum\AuthenticationProcess\Status; |
|
8
|
|
|
use LM\Common\DataStructure\TypedMap; |
|
9
|
|
|
use LM\Common\Enum\Scalar; |
|
10
|
|
|
use LM\Common\Model\ArrayObject; |
|
11
|
|
|
use LM\Common\Model\IntegerObject; |
|
12
|
|
|
use LM\Common\Model\StringObject; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This class is an implementation of IAuthenticationProcess. It stores the data |
|
16
|
|
|
* associated with an authentication process. |
|
17
|
|
|
* |
|
18
|
|
|
* @todo Merge IAuthenticationProcess and TypedMap |
|
19
|
|
|
*/ |
|
20
|
|
|
class AuthenticationProcess implements IAuthenticationProcess |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var TypedMap */ |
|
23
|
|
|
private $typedMap; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param TypedMap $typedMap |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(?TypedMap $typedMap = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->typedMap = $typedMap; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getChallenges(): ArrayObject |
|
34
|
|
|
{ |
|
35
|
|
|
return $this |
|
36
|
|
|
->typedMap |
|
37
|
|
|
->get('challenges', ArrayObject::class) |
|
38
|
|
|
; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @todo Should check for validity. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getCurrentChallenge(): string |
|
45
|
|
|
{ |
|
46
|
|
|
return $this |
|
|
|
|
|
|
47
|
|
|
->typedMap |
|
48
|
|
|
->get('challenges', ArrayObject::class) |
|
49
|
|
|
->getCurrentItem(Scalar::_STR) |
|
50
|
|
|
; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getTypedMap(): TypedMap |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->typedMap; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getMaxNFailedAttempts(): int |
|
59
|
|
|
{ |
|
60
|
|
|
return $this |
|
61
|
|
|
->typedMap |
|
62
|
|
|
->get('max_n_failed_attempts', IntegerObject::class) |
|
63
|
|
|
->toInteger() |
|
64
|
|
|
; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getMember(): IMember |
|
68
|
|
|
{ |
|
69
|
|
|
return $this |
|
70
|
|
|
->typedMap |
|
71
|
|
|
->get('member', IMember::class) |
|
72
|
|
|
; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getNFailedAttempts(): int |
|
76
|
|
|
{ |
|
77
|
|
|
return $this |
|
78
|
|
|
->typedMap |
|
79
|
|
|
->get('n_failed_attempts', IntegerObject::class) |
|
80
|
|
|
->toInteger() |
|
81
|
|
|
; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getPersistOperations(): array |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->typedMap->has('persist_operations')) { |
|
87
|
|
|
return $this |
|
88
|
|
|
->typedMap |
|
89
|
|
|
->get('persist_operations', ArrayObject::class) |
|
90
|
|
|
->toArray(PersistOperation::class) |
|
91
|
|
|
; |
|
92
|
|
|
} else { |
|
93
|
|
|
return []; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getStatus(): Status |
|
98
|
|
|
{ |
|
99
|
|
|
return $this |
|
100
|
|
|
->typedMap |
|
101
|
|
|
->get('status', Status::class) |
|
102
|
|
|
; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getUsername(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this |
|
108
|
|
|
->typedMap |
|
109
|
|
|
->get('username', StringObject::class) |
|
110
|
|
|
->toString() |
|
111
|
|
|
; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function incrementNFailedAttempts(): IAuthenticationProcess |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->setNFailedAttempts($this->getNFailedAttempts() + 1); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function isFailed(): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->getStatus()->is(new Status(Status::FAILED)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function isFinished(): bool |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->isFailed() || $this->isSucceeded(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function isOngoing(): bool |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->getStatus()->is(new Status(Status::ONGOING)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function isSucceeded(): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->getStatus()->is(new Status(Status::SUCCEEDED)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function resetNFailedAttempts(): IAuthenticationProcess |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->setNFailedAttempts(0); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @internal |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setNFailedAttempts(int $nFailedAttempts): IAuthenticationProcess |
|
148
|
|
|
{ |
|
149
|
|
|
$newDm = $this |
|
150
|
|
|
->typedMap |
|
151
|
|
|
->set( |
|
152
|
|
|
'n_failed_attempts', |
|
153
|
|
|
new IntegerObject($nFailedAttempts), |
|
154
|
|
|
IntegerObject::class |
|
155
|
|
|
) |
|
156
|
|
|
; |
|
157
|
|
|
if ($nFailedAttempts < $this->getMaxNFailedAttempts()) { |
|
158
|
|
|
return new self($newDm); |
|
159
|
|
|
} else { |
|
160
|
|
|
return new self($newDm |
|
161
|
|
|
->set( |
|
162
|
|
|
'status', |
|
163
|
|
|
new Status(Status::FAILED), |
|
164
|
|
|
Status::class |
|
165
|
|
|
)) |
|
166
|
|
|
; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @internal |
|
172
|
|
|
*/ |
|
173
|
|
|
public function setToNextChallenge(): IAuthenticationProcess |
|
174
|
|
|
{ |
|
175
|
|
|
$challenges = $this->getChallenges(); |
|
176
|
|
|
if ($challenges->hasNextItem()) { |
|
177
|
|
|
$challenges->setToNextItem(); |
|
178
|
|
|
return new self($this |
|
179
|
|
|
->typedMap |
|
180
|
|
|
->set( |
|
181
|
|
|
'challenges', |
|
182
|
|
|
$challenges, |
|
183
|
|
|
ArrayObject::class |
|
184
|
|
|
)) |
|
185
|
|
|
; |
|
186
|
|
|
} else { |
|
187
|
|
|
return new self($this |
|
188
|
|
|
->typedMap |
|
189
|
|
|
->set( |
|
190
|
|
|
'status', |
|
191
|
|
|
new Status(Status::SUCCEEDED), |
|
192
|
|
|
Status::class |
|
193
|
|
|
)) |
|
194
|
|
|
; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function serialize() |
|
199
|
|
|
{ |
|
200
|
|
|
return serialize($this->typedMap); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function unserialize($serialized) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->typedMap = unserialize($serialized); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|