Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Optimize AuthToken/AuthJWT Config Loading by Initializing in Constructor #1252

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/Authentication/Authenticators/AccessTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CodeIgniter\Shield\Authentication\AuthenticationException;
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Config\AuthToken;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
use CodeIgniter\Shield\Models\TokenLoginModel;
Expand All @@ -29,6 +30,7 @@ class AccessTokens implements AuthenticatorInterface
{
public const ID_TYPE_ACCESS_TOKEN = 'access_token';

protected AuthToken $authTokenConfig;
protected ?User $user = null;
protected TokenLoginModel $loginModel;

Expand All @@ -38,7 +40,8 @@ class AccessTokens implements AuthenticatorInterface
public function __construct(
protected UserModel $provider,
) {
$this->loginModel = model(TokenLoginModel::class);
$this->authTokenConfig = config('AuthToken');
$this->loginModel = model(TokenLoginModel::class);
}

/**
Expand All @@ -49,8 +52,6 @@ public function __construct(
*/
public function attempt(array $credentials): Result
{
$config = config('AuthToken');

/** @var IncomingRequest $request */
$request = service('request');

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

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

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

$this->login($user);

if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
if ($this->authTokenConfig->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
// Record a successful login attempt.
$this->loginModel->recordLoginAttempt(
self::ID_TYPE_ACCESS_TOKEN,
Expand Down Expand Up @@ -131,7 +132,7 @@ public function check(array $credentials): Result
'success' => false,
'reason' => lang(
'Auth.noToken',
[config('AuthToken')->authenticatorHeader['tokens']],
[$this->authTokenConfig->authenticatorHeader['tokens']],
),
]);
}
Expand All @@ -158,7 +159,7 @@ public function check(array $credentials): Result
if (
$token->last_used_at
&& $token->last_used_at->isBefore(
Time::now()->subSeconds(config('AuthToken')->unusedTokenLifetime),
Time::now()->subSeconds($this->authTokenConfig->unusedTokenLifetime),
)
) {
return new Result([
Expand Down Expand Up @@ -199,7 +200,7 @@ public function loggedIn(): bool

return $this->attempt([
'token' => $request->getHeaderLine(
config('AuthToken')->authenticatorHeader['tokens'],
$this->authTokenConfig->authenticatorHeader['tokens'],
),
])->isOK();
}
Expand Down Expand Up @@ -258,7 +259,7 @@ public function getBearerToken(): ?string
/** @var IncomingRequest $request */
$request = service('request');

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

if (empty($header)) {
return null;
Expand Down
21 changes: 11 additions & 10 deletions src/Authentication/Authenticators/HmacSha256.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
use CodeIgniter\Shield\Authentication\HMAC\HmacEncrypter;
use CodeIgniter\Shield\Config\Auth;
use CodeIgniter\Shield\Config\AuthToken;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
use CodeIgniter\Shield\Models\TokenLoginModel;
Expand All @@ -32,14 +33,16 @@ class HmacSha256 implements AuthenticatorInterface

protected ?User $user = null;
protected TokenLoginModel $loginModel;
protected AuthToken $authTokenConfig;

/**
* @param UserModel $provider The persistence engine
*/
public function __construct(
protected UserModel $provider,
) {
$this->loginModel = model(TokenLoginModel::class);
$this->authTokenConfig = config('AuthToken');
$this->loginModel = model(TokenLoginModel::class);
}

/**
Expand All @@ -50,8 +53,6 @@ public function __construct(
*/
public function attempt(array $credentials): Result
{
$config = config('AuthToken');

/** @var IncomingRequest $request */
$request = service('request');

Expand All @@ -61,7 +62,7 @@ public function attempt(array $credentials): Result
$result = $this->check($credentials);

if (! $result->isOK()) {
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
if ($this->authTokenConfig->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
// Record all failed login attempts.
$this->loginModel->recordLoginAttempt(
self::ID_TYPE_HMAC_TOKEN,
Expand All @@ -79,7 +80,7 @@ public function attempt(array $credentials): Result
$token = $user->getHmacToken($this->getHmacKeyFromToken());

if ($user->isBanned()) {
if ($config->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
if ($this->authTokenConfig->recordLoginAttempt >= Auth::RECORD_LOGIN_ATTEMPT_FAILURE) {
// Record a banned login attempt.
$this->loginModel->recordLoginAttempt(
self::ID_TYPE_HMAC_TOKEN,
Expand All @@ -103,7 +104,7 @@ public function attempt(array $credentials): Result

$this->login($user);

if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
if ($this->authTokenConfig->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
// Record a successful login attempt.
$this->loginModel->recordLoginAttempt(
self::ID_TYPE_HMAC_TOKEN,
Expand Down Expand Up @@ -132,7 +133,7 @@ public function check(array $credentials): Result
'success' => false,
'reason' => lang(
'Auth.noToken',
[config('AuthToken')->authenticatorHeader['hmac']],
[$this->authTokenConfig->authenticatorHeader['hmac']],
),
]);
}
Expand Down Expand Up @@ -174,7 +175,7 @@ public function check(array $credentials): Result
if (
isset($token->last_used_at)
&& $token->last_used_at->isBefore(
Time::now()->subSeconds(config('AuthToken')->unusedTokenLifetime),
Time::now()->subSeconds($this->authTokenConfig->unusedTokenLifetime),
)
) {
return new Result([
Expand Down Expand Up @@ -215,7 +216,7 @@ public function loggedIn(): bool

return $this->attempt([
'token' => $request->getHeaderLine(
config('AuthToken')->authenticatorHeader['hmac'],
$this->authTokenConfig->authenticatorHeader['hmac'],
),
])->isOK();
}
Expand Down Expand Up @@ -276,7 +277,7 @@ public function getFullHmacToken(): ?string
/** @var IncomingRequest $request */
$request = service('request');

$header = $request->getHeaderLine(config('AuthToken')->authenticatorHeader['hmac']);
$header = $request->getHeaderLine($this->authTokenConfig->authenticatorHeader['hmac']);

if ($header === '') {
return null;
Expand Down
18 changes: 7 additions & 11 deletions src/Authentication/Authenticators/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class JWT implements AuthenticatorInterface
*/
public const ID_TYPE_JWT = 'jwt';

protected AuthJWT $authJWTConfig;
protected ?User $user = null;
protected JWTManager $jwtManager;
protected TokenLoginModel $tokenLoginModel;
Expand All @@ -56,6 +57,7 @@ class JWT implements AuthenticatorInterface
public function __construct(
protected UserModel $provider,
) {
$this->authJWTConfig = config('AuthJWT');
$this->jwtManager = service('jwtmanager');
$this->tokenLoginModel = model(TokenLoginModel::class);
}
Expand All @@ -68,9 +70,6 @@ public function __construct(
*/
public function attempt(array $credentials): Result
{
/** @var AuthJWT $config */
$config = config('AuthJWT');

/** @var IncomingRequest $request */
$request = service('request');

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

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

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

$this->login($user);

if ($config->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
if ($this->authJWTConfig->recordLoginAttempt === Auth::RECORD_LOGIN_ATTEMPT_ALL) {
// Record a successful login attempt.
$this->tokenLoginModel->recordLoginAttempt(
self::ID_TYPE_JWT,
Expand Down Expand Up @@ -150,7 +149,7 @@ public function check(array $credentials): Result
'success' => false,
'reason' => lang(
'Auth.noToken',
[config('AuthJWT')->authenticatorHeader],
[$this->authJWTConfig->authenticatorHeader],
),
]);
}
Expand Down Expand Up @@ -218,11 +217,8 @@ public function getTokenFromRequest(RequestInterface $request): string
{
assert($request instanceof IncomingRequest);

/** @var AuthJWT $config */
$config = config('AuthJWT');

$tokenHeader = $request->getHeaderLine(
$config->authenticatorHeader ?? 'Authorization',
$this->authJWTConfig->authenticatorHeader ?? 'Authorization',
);

if (str_starts_with($tokenHeader, 'Bearer')) {
Expand Down
Loading