Skip to content

Commit 1b067c9

Browse files
authored
Merge pull request Tinywan#34 from Rodots/main
修复ES系列和PS系列算法无法正常使用的问题并修正注释,仅提示已支持的算法
2 parents 2f8f7e4 + da36687 commit 1b067c9

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

src/JwtToken.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,14 @@ private static function generatePayload(array $config, array $extend): array
324324
private static function getPublicKey(string $algorithm, int $tokenType = self::ACCESS_TOKEN): string
325325
{
326326
$config = self::_getConfig();
327-
switch ($algorithm) {
328-
case 'HS256':
329-
$key = self::ACCESS_TOKEN == $tokenType ? $config['access_secret_key'] : $config['refresh_secret_key'];
330-
break;
331-
case 'RS512':
332-
case 'RS256':
333-
$key = self::ACCESS_TOKEN == $tokenType ? $config['access_public_key'] : $config['refresh_public_key'];
334-
break;
335-
default:
336-
$key = $config['access_secret_key'];
327+
328+
$isAccessToken = $tokenType === self::ACCESS_TOKEN;
329+
330+
if (in_array($algorithm, ['HS512', 'HS384', 'HS256'], true)) {
331+
return $isAccessToken ? $config['access_secret_key'] : $config['refresh_secret_key'];
337332
}
338333

339-
return $key;
334+
return $isAccessToken ? $config['access_public_key'] : $config['refresh_public_key'];
340335
}
341336

342337
/**
@@ -347,19 +342,13 @@ private static function getPublicKey(string $algorithm, int $tokenType = self::A
347342
*/
348343
private static function getPrivateKey(array $config, int $tokenType = self::ACCESS_TOKEN): string
349344
{
350-
switch ($config['algorithms']) {
351-
case 'HS256':
352-
$key = self::ACCESS_TOKEN == $tokenType ? $config['access_secret_key'] : $config['refresh_secret_key'];
353-
break;
354-
case 'RS512':
355-
case 'RS256':
356-
$key = self::ACCESS_TOKEN == $tokenType ? $config['access_private_key'] : $config['refresh_private_key'];
357-
break;
358-
default:
359-
$key = $config['access_secret_key'];
345+
$isAccessToken = $tokenType === self::ACCESS_TOKEN;
346+
347+
if (in_array($config['algorithms'], ['HS512', 'HS384', 'HS256'], true)) {
348+
return $isAccessToken ? $config['access_secret_key'] : $config['refresh_secret_key'];
360349
}
361350

362-
return $key;
351+
return $isAccessToken ? $config['access_private_key'] : $config['refresh_private_key'];
363352
}
364353

365354
/**

src/config/plugin/tinywan/jwt/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
return [
44
'enable' => true,
55
'jwt' => [
6-
/** 算法类型 HS256、HS384、HS512、RS256、RS384、RS512、ES256、ES384、Ed25519 */
6+
/** 算法类型 HS256、HS384、HS512、RS256、RS384、RS512、ES256、ES384、ES512、PS256、PS384、PS512 */
77
'algorithms' => 'HS256',
88

99
/** access令牌秘钥 */

0 commit comments

Comments
 (0)