/
LoginAttemptService.php
244 lines (204 loc) · 6.74 KB
/
LoginAttemptService.php
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace Concrete\Core\User\Login;
use Carbon\Carbon;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Entity\User\LoginAttempt;
use Concrete\Core\Entity\User\User;
use Concrete\Core\User\Event\DeactivateUser;
use Concrete\Core\User\User as LegacyUser;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use Concrete\Core\Events\EventDispatcher;
/**
* A service for tracking and acting upon login attempts
* This service is NOT in charge of logging in users, it is meant to react to attempts
*
* @todo Replace this with a more general login service
*/
class LoginAttemptService
{
/**
* The entitymanager we're tracking attempts with
*
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* The config repository that has configuration for this service
*
* @var \Concrete\Core\User\Login\Repository
*/
protected $config;
/**
* The event dispatcher we use to send out events
*
* @var EventDispatcher
*/
protected $director;
/**
* The currently waiting login attempts
*
* @var LoginAttempt[]
*/
protected $batch = [];
/**
* A cache for known users
*
* @var array
*/
protected $knownUsers = [];
public function __construct(EntityManagerInterface $entityManager, Repository $config, EventDispatcher $director)
{
$this->entityManager = $entityManager;
$this->config = $config;
$this->director = $director;
}
/**
* Track a login attempt for a user
*
* @param string $username
* @param string $password
*
* @return $this
*/
public function trackAttempt($username, $password)
{
if (!$this->config->get('concrete.user.deactivation.authentication_failure.enabled', false)) {
return $this;
}
$user = $this->resolveUser($username);
if (!$user) {
return;
}
$attempt = new LoginAttempt();
$attempt->setUtcDate(Carbon::now('UTC'));
$attempt->setUserId($user->getUserID());
$this->entityManager->persist($attempt);
// Make sure we get flushed with the next batch
$this->batch[] = $attempt;
// Prune old attempts
$this->pruneAttempts();
return $this;
}
/**
* Determine the amount of attempts remaining for a username
*
* @param $username
* @return int
*/
public function remainingAttempts($username)
{
$config = $this->config->get('concrete.user.deactivation.authentication_failure');
$allowed = (int) array_get($config, 'amount', 0);
$duration = (int) array_get($config, 'duration', 0);
$after = Carbon::now('UTC')->subSeconds($duration);
$user = $this->resolveUser($username);
if (!$user) {
return $allowed;
}
/** @var \Concrete\Core\Entity\User\LoginAttemptRepository $repository */
$repository = $this->entityManager->getRepository(LoginAttempt::class);
$attempts = $repository->after($after, $user, true);
return max(0, $allowed - $attempts);
}
/**
* Deactivate a given username
*
* @param $username
*/
public function deactivate($username)
{
if (!$this->config->get('concrete.user.deactivation.authentication_failure.enabled', false)) {
return;
}
$user = $this->resolveUser($username);
if (!$user) {
throw new InvalidArgumentException(t('Unable to find and deactivate given user'));
}
$event = DeactivateUser::create($user);
$this->director->dispatch('on_before_user_deactivate', $event);
// Here we could retrieve the user entity from the event and allow an event handler to change which user gets
// deactivated, but we don't want to support that at the moment
// $user = $event->getUserEntity();
// Set the user to inactive
$this->entityManager->transactional(function(EntityManagerInterface $localManager) use ($user) {
$user = $localManager->merge($user);
$user->setUserIsActive(false);
});
// Send out a `user_deactivate` event
$this->director->dispatch('on_after_user_deactivate', $event);
}
/**
* Prune old login attempts
*
* @param \DateTime|null $before The date to prune before. This MUST be in UTC, if not passed this value is derived
* from config
*/
public function pruneAttempts(DateTime $before = null)
{
if (!$before) {
$duration = (int) $this->config->get('concrete.user.deactivation.authentication_failure.duration');
$before = Carbon::now('UTC')->subSeconds($duration);
}
// Load our repository and get entries before our date
$repository = $this->entityManager->getRepository(LoginAttempt::class);
$results = $repository->before($before);
// Loop through and remove those entries
$batch = [];
$max = 50;
foreach ($results as $result) {
$this->entityManager->remove($result);
$batch[] = $result;
// Handle the batch
$batch = $this->manageBatch($batch, $max);
}
// Close off the batch
$this->manageBatch($batch);
}
/**
* Manage a batched ORM operation
*
* @param array $batch
* @param int $max
*
* @return array
*/
private function manageBatch(array $batch, $max = 0)
{
if (count($batch) >= $max) {
$this->entityManager->flush();
$batch = [];
}
return $batch;
}
/**
* Resolve the user from username / user email
*
* @param $username
* @return User|null
*/
private function resolveUser($username)
{
if (isset($this->knownUsers[strtolower($username)])) {
return $this->knownUsers[strtolower($username)];
}
$repository = $this->entityManager->getRepository(User::class);
$user = $repository->findOneBy(['uName' => $username]);
// If we couldn't find the user by username, let's try to get it by email
if (!$user) {
$user = $repository->findOneBy(['uEmail' => $username]);
}
// No user to actually track against.
if (!$user) {
return null;
}
/**
* I don't know what detach is doing or why it's here, but it's causing an exception on UserSignup
* not being a known entity. Much like drugs, `detach` is not the answer
*/
//$this->entityManager->detach($user);
$this->knownUsers[strtolower($username)] = $user;
return $user;
}
}