Skip to content

Commit 56e2b78

Browse files
committed
feat: change Config\Auth::jwtConfig
1 parent 8aded7f commit 56e2b78

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

src/Authentication/TokenGenerator/JWTGenerator.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ public function generateAccessToken(User $user): string
2828
$iat = $this->currentTime->getTimestamp();
2929
$exp = $iat + $config['timeToLive'];
3030

31-
$payload = [
32-
'iss' => $config['issuer'], // issuer
33-
'aud' => $config['audience'], // audience
34-
'sub' => (string) $user->id, // subject
35-
'iat' => $iat, // issued at
36-
'exp' => $exp, // expiration time
37-
];
31+
$payload = array_merge(
32+
$config['claims'],
33+
[
34+
'sub' => (string) $user->id, // subject
35+
'iat' => $iat, // issued at
36+
'exp' => $exp, // expiration time
37+
]
38+
);
3839

3940
return $this->jwtAdapter->generate(
4041
$payload,
@@ -46,25 +47,28 @@ public function generateAccessToken(User $user): string
4647
/**
4748
* Issues JWT
4849
*
49-
* @param int|null $ttl Time to live in seconds.
50-
* @param string $key The secret key.
50+
* @param array $claims The payload items.
51+
* @param int|null $ttl Time to live in seconds.
52+
* @param string $key The secret key.
5153
*/
52-
public function generate(array $payload, ?int $ttl = null, $key = null, ?string $algorithm = null): string
54+
public function generate(array $claims, ?int $ttl = null, $key = null, ?string $algorithm = null): string
5355
{
5456
assert(
55-
(array_key_exists('exp', $payload) && ($ttl !== null)) === false,
56-
'Cannot pass $payload[\'exp\'] and $ttl at the same time.'
57+
(array_key_exists('exp', $claims) && ($ttl !== null)) === false,
58+
'Cannot pass $claims[\'exp\'] and $ttl at the same time.'
5759
);
5860

5961
$config = setting('Auth.jwtConfig');
6062
$algorithm ??= $config['algorithm'];
6163
$key ??= $config['secretKey'];
6264

63-
if (! array_key_exists('iat', $payload)) {
65+
$payload = $claims;
66+
67+
if (! array_key_exists('iat', $claims)) {
6468
$payload['iat'] = $this->currentTime->getTimestamp();
6569
}
6670

67-
if (! array_key_exists('exp', $payload)) {
71+
if (! array_key_exists('exp', $claims)) {
6872
$payload['exp'] = $payload['iat'] + $config['timeToLive'];
6973
}
7074

src/Config/Auth.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,21 @@ class Auth extends BaseConfig
198198
* These settings only apply if you are using the JWT Authenticator
199199
* for authentication.
200200
*
201+
* These are the default values when you generate and validate JWT
202+
*
203+
* - claims The payload items that all JWT have.
201204
* - secretKey The secret key. Needs more than 256 bits random string.
202205
* E.g., $ php -r 'echo base64_encode(random_bytes(32));'
203206
* - algorithm JWT Signing Algorithms.
204207
* - timeToLive Specifies the amount of time, in seconds, that a token is valid.
205208
*
206-
* @var array<string, bool|int|string>
209+
* @var array<string, array<string, array|bool|int|string>|bool|int|string>
207210
*/
208211
public array $jwtConfig = [
209-
'issuer' => '<Issuer of the JWT>',
210-
'audience' => '<Audience of the JWT>',
212+
'claims' => [
213+
'iss' => '<Issuer of the JWT>',
214+
'aud' => '<Audience of the JWT>',
215+
],
211216
'secretKey' => '<Set secret random string like MQ4GfWut1OYZxPY9fXAIq2YP6KzTSKOGNS7dJNcRrR8=>',
212217
'algorithm' => 'HS256',
213218
'timeToLive' => 1 * HOUR,

tests/Authentication/Authenticators/JWTAuthenticatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public function testCheckNoSubToken()
114114
{
115115
$config = setting('Auth.jwtConfig');
116116
$payload = [
117-
'iss' => $config['issuer'], // issuer
118-
'aud' => $config['audience'], // audience
117+
'iss' => $config['claims']['iss'], // issuer
118+
'aud' => $config['claims']['aud'], // audience
119119
];
120120
$token = FirebaseJWT::encode($payload, $config['secretKey'], $config['algorithm']);
121121

@@ -168,7 +168,7 @@ public function testGetPayload()
168168
$payload = $this->auth->getPayload();
169169

170170
$this->assertSame((string) $this->user->id, $payload->sub);
171-
$this->assertSame((\setting('Auth.jwtConfig')['issuer']), $payload->iss);
171+
$this->assertSame((\setting('Auth.jwtConfig')['claims']['iss']), $payload->iss);
172172
}
173173

174174
public function testAttemptBadSignatureToken()

tests/Unit/Authentication/Authenticators/JWT/FirebaseAdapaterTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ public function testDecode()
2121

2222
$jwtDecoder = new FirebaseAdapter();
2323

24-
$payload = $jwtDecoder->decode($token);
24+
$config = setting('Auth.jwtConfig');
25+
$key = $config['secretKey'];
26+
$algorithm = $config['algorithm'];
27+
28+
$payload = $jwtDecoder->decode($token, $key, $algorithm);
2529

26-
$this->assertSame(setting('Auth.jwtConfig')['issuer'], $payload->iss);
27-
$this->assertSame(setting('Auth.jwtConfig')['audience'], $payload->aud);
30+
$this->assertSame(setting('Auth.jwtConfig')['claims']['iss'], $payload->iss);
31+
$this->assertSame(setting('Auth.jwtConfig')['claims']['aud'], $payload->aud);
2832
$this->assertSame('1', $payload->sub);
2933
}
3034

0 commit comments

Comments
 (0)