-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathLockoutHandler.php
More file actions
206 lines (185 loc) · 5.93 KB
/
Copy pathLockoutHandler.php
File metadata and controls
206 lines (185 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2024, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Identifier\PasswordLockout;
use Cake\Core\InstanceConfigTrait;
use Cake\Datasource\EntityInterface;
use Cake\I18n\DateTime;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Query\SelectQuery;
class LockoutHandler implements LockoutHandlerInterface
{
use InstanceConfigTrait;
use LocatorAwareTrait;
/**
* Default configuration.
*
* @var array{timeWindowInSeconds: int, lockoutTimeInSeconds: int, numberOfAttemptsFail:int}
*/
protected array $_defaultConfig = [
'timeWindowInSeconds' => 15 * 60,
'lockoutTimeInSeconds' => 30 * 60,
'numberOfAttemptsFail' => 6,
'failedPasswordAttemptsModel' => 'CakeDC/Users.FailedPasswordAttempts',
'userLockoutField' => 'lockout_time',
'usersModel' => 'Users',
'userForeignKeyField' => 'user_id',
];
/**
* Constructor
*
* @param array $config Configuration
*/
public function __construct(array $config = [])
{
$this->setConfig($config);
}
/**
* @param \ArrayAccess|array $identity
* @return bool
*/
public function isUnlocked(\ArrayAccess|array $identity): bool
{
if (!isset($identity['id'])) {
return false;
}
$lockoutField = $this->getConfig('userLockoutField');
$userLockoutTime = $identity[$lockoutField] ?? null;
if ($userLockoutTime) {
if (!$this->checkLockoutTime($userLockoutTime)) {//Still locked?
return false;
}
}
$timeWindow = $this->getTimeWindow();
$attemptsCount = $this->getAttemptsCount($identity['id'], $timeWindow);
$numberOfAttemptsFail = $this->getNumberOfAttemptsFail();
if ($numberOfAttemptsFail > $attemptsCount) {
return true;
}
$lastAttempt = $this->getLastAttempt($identity['id'], $timeWindow);
$this->getTableLocator()
->get($this->getConfig('usersModel'))
->updateAll([$lockoutField => $lastAttempt->get('created')], ['id' => $identity['id']]);
return $this->checkLockoutTime($lastAttempt->get('created'));
}
/**
* @param string|int $id User's id
* @return void
*/
public function newFail(string|int $id): void
{
$timeWindow = $this->getTimeWindow();
$Table = $this->getTable();
$entity = $Table->newEntity([$this->getConfig('userForeignKeyField') => $id]);
$Table->saveOrFail($entity);
$Table->deleteAll($Table->query()->newExpr()->lt('created', $timeWindow));
}
/**
* @return \Cake\ORM\Table
*/
protected function getTable(): \Cake\ORM\Table
{
return $this->getTableLocator()->get($this->getConfig('failedPasswordAttemptsModel'));
}
/**
* @param string|int $id
* @param \Cake\I18n\DateTime $timeWindow
* @return int
*/
protected function getAttemptsCount(string|int $id, DateTime $timeWindow): int
{
return $this->getAttemptsQuery($id, $timeWindow)->count();
}
/**
* @param string|int $id
* @param \Cake\I18n\DateTime $timeWindow
* @return \Cake\Datasource\EntityInterface
*/
protected function getLastAttempt(int|string $id, DateTime $timeWindow): EntityInterface
{
/**
* @var \CakeDC\Users\Model\Entity\FailedPasswordAttempt $attempt
*/
$attempt = $this->getAttemptsQuery($id, $timeWindow)->first();
return $attempt;
}
/**
* @param string|int $id
* @param \Cake\I18n\DateTime $timeWindow
* @return \Cake\ORM\Query\SelectQuery
*/
protected function getAttemptsQuery(int|string $id, DateTime $timeWindow): SelectQuery
{
$query = $this->getTable()->find();
return $query
->where([
$this->getConfig('userForeignKeyField') => $id,
$query->newExpr()->gte('created', $timeWindow),
])
->orderByDesc('created');
}
/**
* @return \Cake\I18n\DateTime
*/
protected function getTimeWindow(): DateTime
{
$timeWindow = $this->getConfig('timeWindowInSeconds');
if (is_int($timeWindow) && $timeWindow >= 60) {
return (new DateTime())->subSeconds($timeWindow);
}
throw new \UnexpectedValueException(
__d(
'cake_d_c/users',
'Config "timeWindowInSeconds" must be integer greater than 60',
),
);
}
/**
* @return int
*/
protected function getNumberOfAttemptsFail(): int
{
$number = $this->getConfig('numberOfAttemptsFail');
if (is_int($number) && $number >= 1) {
return $number;
}
throw new \UnexpectedValueException(
__d(
'cake_d_c/users',
'Config "numberOfAttemptsFail" must be integer greater or equal 0',
),
);
}
/**
* @return int
*/
protected function getLockoutTime(): int
{
$lockTime = $this->getConfig('lockoutTimeInSeconds');
if (is_int($lockTime) && $lockTime >= 60) {
return $lockTime;
}
throw new \UnexpectedValueException(
__d(
'cake_d_c/users',
'Config "lockoutTimeInSeconds" must be integer greater than 60',
),
);
}
/**
* @param \Cake\I18n\DateTime $dateTime
* @return bool
*/
protected function checkLockoutTime(DateTime $dateTime): bool
{
return $dateTime->addSeconds($this->getLockoutTime())->isPast();
}
}