Skip to content

Commit 75cf64d

Browse files
committed
refactor: move $authenticatorHeader and $unusedTokenLifetime to Config\AuthToken
1 parent 7c2c66b commit 75cf64d

6 files changed

Lines changed: 40 additions & 10 deletions

File tree

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function check(array $credentials): Result
124124
if (! array_key_exists('token', $credentials) || empty($credentials['token'])) {
125125
return new Result([
126126
'success' => false,
127-
'reason' => lang('Auth.noToken', [config('Auth')->authenticatorHeader['tokens']]),
127+
'reason' => lang('Auth.noToken', [config('AuthToken')->authenticatorHeader['tokens']]),
128128
]);
129129
}
130130

@@ -149,7 +149,9 @@ public function check(array $credentials): Result
149149
// Hasn't been used in a long time
150150
if (
151151
$token->last_used_at
152-
&& $token->last_used_at->isBefore(Time::now()->subSeconds(config('Auth')->unusedTokenLifetime))
152+
&& $token->last_used_at->isBefore(
153+
Time::now()->subSeconds(config('AuthToken')->unusedTokenLifetime)
154+
)
153155
) {
154156
return new Result([
155157
'success' => false,
@@ -188,7 +190,7 @@ public function loggedIn(): bool
188190
$request = service('request');
189191

190192
return $this->attempt([
191-
'token' => $request->getHeaderLine(config('Auth')->authenticatorHeader['tokens']),
193+
'token' => $request->getHeaderLine(config('AuthToken')->authenticatorHeader['tokens']),
192194
])->isOK();
193195
}
194196

@@ -246,7 +248,7 @@ public function getBearerToken(): ?string
246248
/** @var IncomingRequest $request */
247249
$request = service('request');
248250

249-
$header = $request->getHeaderLine(config('Auth')->authenticatorHeader['tokens']);
251+
$header = $request->getHeaderLine(config('AuthToken')->authenticatorHeader['tokens']);
250252

251253
if (empty($header)) {
252254
return null;

src/Authentication/Authenticators/HmacSha256.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function check(array $credentials): Result
124124
if (! array_key_exists('token', $credentials) || $credentials['token'] === '') {
125125
return new Result([
126126
'success' => false,
127-
'reason' => lang('Auth.noToken', [config('Auth')->authenticatorHeader['hmac']]),
127+
'reason' => lang('Auth.noToken', [config('AuthToken')->authenticatorHeader['hmac']]),
128128
]);
129129
}
130130

@@ -161,7 +161,9 @@ public function check(array $credentials): Result
161161
// Hasn't been used in a long time
162162
if (
163163
isset($token->last_used_at)
164-
&& $token->last_used_at->isBefore(Time::now()->subSeconds(config('Auth')->unusedTokenLifetime))
164+
&& $token->last_used_at->isBefore(
165+
Time::now()->subSeconds(config('AuthToken')->unusedTokenLifetime)
166+
)
165167
) {
166168
return new Result([
167169
'success' => false,
@@ -200,7 +202,7 @@ public function loggedIn(): bool
200202
$request = service('request');
201203

202204
return $this->attempt([
203-
'token' => $request->getHeaderLine(config('Auth')->authenticatorHeader['hmac']),
205+
'token' => $request->getHeaderLine(config('AuthToken')->authenticatorHeader['hmac']),
204206
])->isOK();
205207
}
206208

@@ -260,7 +262,7 @@ public function getFullHmacToken(): ?string
260262
/** @var IncomingRequest $request */
261263
$request = service('request');
262264

263-
$header = $request->getHeaderLine(config('Auth')->authenticatorHeader['hmac']);
265+
$header = $request->getHeaderLine(config('AuthToken')->authenticatorHeader['hmac']);
264266

265267
if ($header === '') {
266268
return null;

src/Config/Auth.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ class Auth extends BaseConfig
146146
* The name of Header that the Authorization token should be found.
147147
* According to the specs, this should be `Authorization`, but rare
148148
* circumstances might need a different header.
149+
*
150+
* @deprecated Moved to AuthToken. No longer used.
149151
*/
150152
public array $authenticatorHeader = [
151153
'tokens' => 'Authorization',
@@ -158,6 +160,8 @@ class Auth extends BaseConfig
158160
* --------------------------------------------------------------------
159161
* Determines the amount of time, in seconds, that an unused
160162
* access token can be used.
163+
*
164+
* @deprecated Moved to AuthToken. No longer used.
161165
*/
162166
public int $unusedTokenLifetime = YEAR;
163167

src/Config/AuthToken.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ class AuthToken extends BaseConfig
2424
*/
2525
public int $recordLoginAttempt = Auth::RECORD_LOGIN_ATTEMPT_FAILURE;
2626

27+
/**
28+
* --------------------------------------------------------------------
29+
* Name of Authenticator Header
30+
* --------------------------------------------------------------------
31+
* The name of Header that the Authorization token should be found.
32+
* According to the specs, this should be `Authorization`, but rare
33+
* circumstances might need a different header.
34+
*/
35+
public array $authenticatorHeader = [
36+
'tokens' => 'Authorization',
37+
'hmac' => 'Authorization',
38+
];
39+
40+
/**
41+
* --------------------------------------------------------------------
42+
* Unused Token Lifetime
43+
* --------------------------------------------------------------------
44+
* Determines the amount of time, in seconds, that an unused token can
45+
* be used.
46+
*/
47+
public int $unusedTokenLifetime = YEAR;
48+
2749
/**
2850
* --------------------------------------------------------------------
2951
* HMAC secret key byte size

tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testCheckNoToken(): void
110110
$result = $this->auth->check([]);
111111

112112
$this->assertFalse($result->isOK());
113-
$this->assertSame(lang('Auth.noToken', [config('Auth')->authenticatorHeader['tokens']]), $result->reason());
113+
$this->assertSame(lang('Auth.noToken', [config('AuthToken')->authenticatorHeader['tokens']]), $result->reason());
114114
}
115115

116116
public function testCheckBadToken(): void

tests/Authentication/Authenticators/HmacAuthenticatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function testCheckNoToken(): void
131131
$result = $this->auth->check([]);
132132

133133
$this->assertFalse($result->isOK());
134-
$this->assertSame(lang('Auth.noToken', [config('Auth')->authenticatorHeader['hmac']]), $result->reason());
134+
$this->assertSame(lang('Auth.noToken', [config('AuthToken')->authenticatorHeader['hmac']]), $result->reason());
135135
}
136136

137137
public function testCheckBadSignature(): void

0 commit comments

Comments
 (0)