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