Skip to content

Commit 0405d31

Browse files
committed
fix: Access Token Authenticator raw tokens are logged
1 parent f77c6ae commit 0405d31

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ public function attempt(array $credentials): Result
7777
return $result;
7878
}
7979

80-
$user = $result->extraInfo();
80+
$user = $result->extraInfo();
81+
$token = $user->getAccessToken($this->getBearerToken());
8182

8283
if ($user->isBanned()) {
8384
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
8485
// Record a banned login attempt.
8586
$this->loginModel->recordLoginAttempt(
8687
self::ID_TYPE_ACCESS_TOKEN,
87-
$credentials['token'] ?? '',
88+
$token->name ?? '',
8889
false,
8990
$ipAddress,
9091
$userAgent,
@@ -100,17 +101,15 @@ public function attempt(array $credentials): Result
100101
]);
101102
}
102103

103-
$user = $user->setAccessToken(
104-
$user->getAccessToken($this->getBearerToken())
105-
);
104+
$user = $user->setAccessToken($token);
106105

107106
$this->login($user);
108107

109108
if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
110109
// Record a successful login attempt.
111110
$this->loginModel->recordLoginAttempt(
112111
self::ID_TYPE_ACCESS_TOKEN,
113-
$credentials['token'] ?? '',
112+
$token->name ?? '',
114113
true,
115114
$ipAddress,
116115
$userAgent,

tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Shield\Authentication\Authentication;
1818
use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens;
1919
use CodeIgniter\Shield\Config\Auth;
20+
use CodeIgniter\Shield\Config\AuthToken;
2021
use CodeIgniter\Shield\Entities\AccessToken;
2122
use CodeIgniter\Shield\Entities\User;
2223
use CodeIgniter\Shield\Models\UserIdentityModel;
@@ -222,6 +223,38 @@ public function testAttemptSuccess(): void
222223
]);
223224
}
224225

226+
public function testAttemptSuccessLog(): void
227+
{
228+
// Change $recordLoginAttempt in Config.
229+
/** @var AuthToken $config */
230+
$config = config('AuthToken');
231+
$config->recordLoginAttempt = Auth::RECORD_LOGIN_ATTEMPT_ALL;
232+
233+
/** @var User $user */
234+
$user = fake(UserModel::class);
235+
$token = $user->generateAccessToken('foo');
236+
$this->setRequestHeader($token->raw_token);
237+
238+
$result = $this->auth->attempt([
239+
'token' => $token->raw_token,
240+
]);
241+
242+
$this->assertInstanceOf(Result::class, $result);
243+
$this->assertTrue($result->isOK());
244+
245+
$foundUser = $result->extraInfo();
246+
$this->assertInstanceOf(User::class, $foundUser);
247+
$this->assertSame($user->id, $foundUser->id);
248+
$this->assertInstanceOf(AccessToken::class, $foundUser->currentAccessToken());
249+
$this->assertSame($token->token, $foundUser->currentAccessToken()->token);
250+
251+
$this->seeInDatabase($this->tables['token_logins'], [
252+
'id_type' => AccessTokens::ID_TYPE_ACCESS_TOKEN,
253+
'identifier' => 'foo',
254+
'success' => 1,
255+
]);
256+
}
257+
225258
protected function setRequestHeader(string $token): void
226259
{
227260
$request = service('request');

0 commit comments

Comments
 (0)