Skip to content

Commit 2907a5c

Browse files
authored
Merge pull request #843 from kenjis/fix-token-auth-logging
fix: AccessTokens authenticator records all accesses to database
2 parents fc0926b + 5e75998 commit 2907a5c

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

UPGRADING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Upgrade Guide
22

3+
## Version 1.0.0-beta.6 to 1.0.0-beta.7
4+
5+
### Install New Config AuthToken.php
6+
7+
A new Config file **AuthToken.php** has been introduced. Run `php spark shield:setup`
8+
again to install it into **app/Config/**, or install it manually.
9+
10+
Then change the default settings as necessary. When using Token authentication,
11+
the default value has been changed from all accesses to be recorded in the
12+
``token_logins`` table to only accesses that fail authentication to be recorded.
13+
314
## Version 1.0.0-beta.3 to 1.0.0-beta.4
415

516
### Important Password Changes

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodeIgniter\I18n\Time;
99
use CodeIgniter\Shield\Authentication\AuthenticationException;
1010
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
11+
use CodeIgniter\Shield\Config\Auth;
1112
use CodeIgniter\Shield\Entities\User;
1213
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
1314
use CodeIgniter\Shield\Models\TokenLoginModel;
@@ -42,6 +43,8 @@ public function __construct(UserModel $provider)
4243
*/
4344
public function attempt(array $credentials): Result
4445
{
46+
$config = config('AuthToken');
47+
4548
/** @var IncomingRequest $request */
4649
$request = service('request');
4750

@@ -51,21 +54,35 @@ public function attempt(array $credentials): Result
5154
$result = $this->check($credentials);
5255

5356
if (! $result->isOK()) {
54-
// Always record a login attempt, whether success or not.
55-
$this->loginModel->recordLoginAttempt(
56-
self::ID_TYPE_ACCESS_TOKEN,
57-
$credentials['token'] ?? '',
58-
false,
59-
$ipAddress,
60-
$userAgent
61-
);
57+
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
58+
// Record all failed login attempts.
59+
$this->loginModel->recordLoginAttempt(
60+
self::ID_TYPE_ACCESS_TOKEN,
61+
$credentials['token'] ?? '',
62+
false,
63+
$ipAddress,
64+
$userAgent
65+
);
66+
}
6267

6368
return $result;
6469
}
6570

6671
$user = $result->extraInfo();
6772

6873
if ($user->isBanned()) {
74+
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
75+
// Record a banned login attempt.
76+
$this->loginModel->recordLoginAttempt(
77+
self::ID_TYPE_ACCESS_TOKEN,
78+
$credentials['token'] ?? '',
79+
false,
80+
$ipAddress,
81+
$userAgent,
82+
$user->id
83+
);
84+
}
85+
6986
$this->user = null;
7087

7188
return new Result([
@@ -80,14 +97,17 @@ public function attempt(array $credentials): Result
8097

8198
$this->login($user);
8299

83-
$this->loginModel->recordLoginAttempt(
84-
self::ID_TYPE_ACCESS_TOKEN,
85-
$credentials['token'] ?? '',
86-
true,
87-
$ipAddress,
88-
$userAgent,
89-
$this->user->id
90-
);
100+
if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
101+
// Record a successful login attempt.
102+
$this->loginModel->recordLoginAttempt(
103+
self::ID_TYPE_ACCESS_TOKEN,
104+
$credentials['token'] ?? '',
105+
true,
106+
$ipAddress,
107+
$userAgent,
108+
$this->user->id
109+
);
110+
}
91111

92112
return $result;
93113
}

src/Config/AuthToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use CodeIgniter\Config\BaseConfig;
88

99
/**
10-
* Authenticator Configuration for Token Auth and HMAC Auth
10+
* Configuration for Token Auth and HMAC Auth
1111
*/
1212
class AuthToken extends BaseConfig
1313
{
1414
/**
1515
* --------------------------------------------------------------------
16-
* Record Login Attempts for Token and HMAC Authorization
16+
* Record Login Attempts for Token Auth and HMAC Auth
1717
* --------------------------------------------------------------------
1818
* Specify which login attempts are recorded in the database.
1919
*

src/Filters/TokenAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TokenAuth implements FilterInterface
2121
{
2222
/**
2323
* Do whatever processing this filter needs to do.
24-
* By default it should not return anything during
24+
* By default, it should not return anything during
2525
* normal execution. However, when an abnormal state
2626
* is found, it should return an instance of
2727
* CodeIgniter\HTTP\Response. If it does, script

tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function testAttemptCannotFindUser(): void
174174
$this->assertFalse($result->isOK());
175175
$this->assertSame(lang('Auth.badToken'), $result->reason());
176176

177-
// A login attempt should have always been recorded
177+
// A failed login attempt should have been recorded by default.
178178
$this->seeInDatabase($this->tables['token_logins'], [
179179
'id_type' => AccessTokens::ID_TYPE_ACCESS_TOKEN,
180180
'identifier' => 'abc123',
@@ -202,8 +202,8 @@ public function testAttemptSuccess(): void
202202
$this->assertInstanceOf(AccessToken::class, $foundUser->currentAccessToken());
203203
$this->assertSame($token->token, $foundUser->currentAccessToken()->token);
204204

205-
// A login attempt should have been recorded
206-
$this->seeInDatabase($this->tables['token_logins'], [
205+
// A successful login attempt is not recorded by default.
206+
$this->dontSeeInDatabase($this->tables['token_logins'], [
207207
'id_type' => AccessTokens::ID_TYPE_ACCESS_TOKEN,
208208
'identifier' => $token->raw_token,
209209
'success' => 1,

0 commit comments

Comments
 (0)