Skip to content

Commit 03821fe

Browse files
committed
refactor: load config once in the constructor
1 parent fd89a5d commit 03821fe

File tree

1 file changed

+7
-11
lines changed
  • src/Authentication/Authenticators

1 file changed

+7
-11
lines changed

src/Authentication/Authenticators/JWT.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class JWT implements AuthenticatorInterface
4040
*/
4141
public const ID_TYPE_JWT = 'jwt';
4242

43+
protected AuthJWT $authJWTConfig;
4344
protected ?User $user = null;
4445
protected JWTManager $jwtManager;
4546
protected TokenLoginModel $tokenLoginModel;
@@ -56,6 +57,7 @@ class JWT implements AuthenticatorInterface
5657
public function __construct(
5758
protected UserModel $provider,
5859
) {
60+
$this->authJWTConfig = config('AuthJWT');
5961
$this->jwtManager = service('jwtmanager');
6062
$this->tokenLoginModel = model(TokenLoginModel::class);
6163
}
@@ -68,9 +70,6 @@ public function __construct(
6870
*/
6971
public function attempt(array $credentials): Result
7072
{
71-
/** @var AuthJWT $config */
72-
$config = config('AuthJWT');
73-
7473
/** @var IncomingRequest $request */
7574
$request = service('request');
7675

@@ -80,7 +79,7 @@ public function attempt(array $credentials): Result
8079
$result = $this->check($credentials);
8180

8281
if (! $result->isOK()) {
83-
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
82+
if ($this->authJWTConfig->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
8483
// Record a failed login attempt.
8584
$this->tokenLoginModel->recordLoginAttempt(
8685
self::ID_TYPE_JWT,
@@ -97,7 +96,7 @@ public function attempt(array $credentials): Result
9796
$user = $result->extraInfo();
9897

9998
if ($user->isBanned()) {
100-
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
99+
if ($this->authJWTConfig->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
101100
// Record a banned login attempt.
102101
$this->tokenLoginModel->recordLoginAttempt(
103102
self::ID_TYPE_JWT,
@@ -119,7 +118,7 @@ public function attempt(array $credentials): Result
119118

120119
$this->login($user);
121120

122-
if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
121+
if ($this->authJWTConfig->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
123122
// Record a successful login attempt.
124123
$this->tokenLoginModel->recordLoginAttempt(
125124
self::ID_TYPE_JWT,
@@ -150,7 +149,7 @@ public function check(array $credentials): Result
150149
'success' => false,
151150
'reason' => lang(
152151
'Auth.noToken',
153-
[config('AuthJWT')->authenticatorHeader],
152+
[$this->authJWTConfig->authenticatorHeader],
154153
),
155154
]);
156155
}
@@ -218,11 +217,8 @@ public function getTokenFromRequest(RequestInterface $request): string
218217
{
219218
assert($request instanceof IncomingRequest);
220219

221-
/** @var AuthJWT $config */
222-
$config = config('AuthJWT');
223-
224220
$tokenHeader = $request->getHeaderLine(
225-
$config->authenticatorHeader ?? 'Authorization',
221+
$this->authJWTConfig->authenticatorHeader ?? 'Authorization',
226222
);
227223

228224
if (str_starts_with($tokenHeader, 'Bearer')) {

0 commit comments

Comments
 (0)