Skip to content

Commit 3117c17

Browse files
committed
feat: do not login banned users
1 parent 6bf2eef commit 3117c17

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Authentication/Authenticators/JWT.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ public function attempt(array $credentials): Result
8888

8989
$user = $result->extraInfo();
9090

91+
if ($user->isBanned()) {
92+
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
93+
// Record a banned login attempt.
94+
$this->tokenLoginModel->recordLoginAttempt(
95+
self::ID_TYPE_JWT,
96+
$credentials['token'] ?? '',
97+
false,
98+
$ipAddress,
99+
$userAgent,
100+
$user->id
101+
);
102+
}
103+
104+
$this->user = null;
105+
106+
return new Result([
107+
'success' => false,
108+
'reason' => $user->getBanMessage() ?? lang('Auth.bannedUser'),
109+
]);
110+
}
111+
91112
$this->login($user);
92113

93114
if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {

tests/Authentication/Authenticators/JWTAuthenticatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ public function testAttemptBadSignatureToken(): void
198198
]);
199199
}
200200

201+
public function testAttemptBannedUser(): void
202+
{
203+
$token = $this->generateJWT();
204+
205+
$this->user->ban();
206+
207+
$result = $this->auth->attempt([
208+
'token' => $token,
209+
]);
210+
211+
$this->assertInstanceOf(Result::class, $result);
212+
$this->assertFalse($result->isOK());
213+
$this->assertSame(lang('Auth.bannedUser'), $result->reason());
214+
215+
// The login attempt should have been recorded
216+
$this->seeInDatabase('auth_token_logins', [
217+
'id_type' => JWT::ID_TYPE_JWT,
218+
'identifier' => $token,
219+
'success' => 0,
220+
'user_id' => $this->user->id,
221+
]);
222+
}
223+
201224
public function testAttemptSuccess(): void
202225
{
203226
// Change $recordLoginAttempt in Config.

0 commit comments

Comments
 (0)