Skip to content

Commit fd89a5d

Browse files
committed
refactor: load config once in the constructor
1 parent 94ec234 commit fd89a5d

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Shield\Authentication\AuthenticationException;
1919
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
2020
use CodeIgniter\Shield\Config\Auth;
21+
use CodeIgniter\Shield\Config\AuthToken;
2122
use CodeIgniter\Shield\Entities\User;
2223
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
2324
use CodeIgniter\Shield\Models\TokenLoginModel;
@@ -29,6 +30,7 @@ class AccessTokens implements AuthenticatorInterface
2930
{
3031
public const ID_TYPE_ACCESS_TOKEN = 'access_token';
3132

33+
protected AuthToken $authTokenConfig;
3234
protected ?User $user = null;
3335
protected TokenLoginModel $loginModel;
3436

@@ -38,7 +40,8 @@ class AccessTokens implements AuthenticatorInterface
3840
public function __construct(
3941
protected UserModel $provider,
4042
) {
41-
$this->loginModel = model(TokenLoginModel::class);
43+
$this->authTokenConfig = config('AuthToken');
44+
$this->loginModel = model(TokenLoginModel::class);
4245
}
4346

4447
/**
@@ -49,8 +52,6 @@ public function __construct(
4952
*/
5053
public function attempt(array $credentials): Result
5154
{
52-
$config = config('AuthToken');
53-
5455
/** @var IncomingRequest $request */
5556
$request = service('request');
5657

@@ -60,7 +61,7 @@ public function attempt(array $credentials): Result
6061
$result = $this->check($credentials);
6162

6263
if (! $result->isOK()) {
63-
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
64+
if ($this->authTokenConfig->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
6465
// Record all failed login attempts.
6566
$this->loginModel->recordLoginAttempt(
6667
self::ID_TYPE_ACCESS_TOKEN,
@@ -78,7 +79,7 @@ public function attempt(array $credentials): Result
7879
$token = $user->getAccessToken($this->getBearerToken());
7980

8081
if ($user->isBanned()) {
81-
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
82+
if ($this->authTokenConfig->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
8283
// Record a banned login attempt.
8384
$this->loginModel->recordLoginAttempt(
8485
self::ID_TYPE_ACCESS_TOKEN,
@@ -102,7 +103,7 @@ public function attempt(array $credentials): Result
102103

103104
$this->login($user);
104105

105-
if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
106+
if ($this->authTokenConfig->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
106107
// Record a successful login attempt.
107108
$this->loginModel->recordLoginAttempt(
108109
self::ID_TYPE_ACCESS_TOKEN,
@@ -131,7 +132,7 @@ public function check(array $credentials): Result
131132
'success' => false,
132133
'reason' => lang(
133134
'Auth.noToken',
134-
[config('AuthToken')->authenticatorHeader['tokens']],
135+
[$this->authTokenConfig->authenticatorHeader['tokens']],
135136
),
136137
]);
137138
}
@@ -158,7 +159,7 @@ public function check(array $credentials): Result
158159
if (
159160
$token->last_used_at
160161
&& $token->last_used_at->isBefore(
161-
Time::now()->subSeconds(config('AuthToken')->unusedTokenLifetime),
162+
Time::now()->subSeconds($this->authTokenConfig->unusedTokenLifetime),
162163
)
163164
) {
164165
return new Result([
@@ -199,7 +200,7 @@ public function loggedIn(): bool
199200

200201
return $this->attempt([
201202
'token' => $request->getHeaderLine(
202-
config('AuthToken')->authenticatorHeader['tokens'],
203+
$this->authTokenConfig->authenticatorHeader['tokens'],
203204
),
204205
])->isOK();
205206
}
@@ -258,7 +259,7 @@ public function getBearerToken(): ?string
258259
/** @var IncomingRequest $request */
259260
$request = service('request');
260261

261-
$header = $request->getHeaderLine(config('AuthToken')->authenticatorHeader['tokens']);
262+
$header = $request->getHeaderLine($this->authTokenConfig->authenticatorHeader['tokens']);
262263

263264
if (empty($header)) {
264265
return null;

0 commit comments

Comments
 (0)