helper
This commit is contained in:
parent
d884aa658e
commit
347344bae0
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Helpers;
|
namespace App\Helpers;
|
||||||
|
|
||||||
use DateTimeImmutable;
|
use Log;
|
||||||
|
|
||||||
|
|
||||||
class TokenHelper
|
class TokenHelper
|
||||||
{
|
{
|
||||||
@ -43,6 +44,7 @@ class TokenHelper
|
|||||||
try {
|
try {
|
||||||
$parts = explode('.', $tokenString);
|
$parts = explode('.', $tokenString);
|
||||||
if (count($parts) != 2) {
|
if (count($parts) != 2) {
|
||||||
|
Log::error("验证失败,token无效");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,27 +53,31 @@ class TokenHelper
|
|||||||
// 验证签名
|
// 验证签名
|
||||||
$expectedSignature = hash_hmac('sha256', $payloadEncoded, self::$secret);
|
$expectedSignature = hash_hmac('sha256', $payloadEncoded, self::$secret);
|
||||||
if (!hash_equals($expectedSignature, $signature)) {
|
if (!hash_equals($expectedSignature, $signature)) {
|
||||||
|
Log::error("验证失败,签名错误");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解码 payload
|
// 解码 payload
|
||||||
$payload = json_decode(base64_decode($payloadEncoded), true);
|
$payload = json_decode(base64_decode($payloadEncoded), true);
|
||||||
if (!$payload) {
|
if (!$payload) {
|
||||||
|
Log::error("验证失败,解码错误");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否过期
|
// 检查是否过期
|
||||||
$now = time();
|
$now = time();
|
||||||
if (isset($payload['exp']) && $payload['exp'] < $now) {
|
if (isset($payload['exp']) && $payload['exp'] < $now) {
|
||||||
|
Log::error("验证失败,已过期");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Log::info("验证成功");
|
||||||
return [
|
return [
|
||||||
'user_id' => $payload['user_id'],
|
'user_id' => $payload['user_id'],
|
||||||
'expires_at' => $payload['exp']
|
'expires_at' => $payload['exp']
|
||||||
];
|
];
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
Log::error("验证失败,代码错误");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user