Compare commits

..

No commits in common. "b6cb9da48159be239ac29d583fca44aa05f3c5b5" and "d884aa658e06322198cbe73faba8931992ca2aee" have entirely different histories.

View File

@ -2,8 +2,7 @@
namespace App\Helpers;
use Log;
use DateTimeImmutable;
class TokenHelper
{
@ -42,10 +41,8 @@ class TokenHelper
public static function verify($tokenString)
{
try {
Log::info("token: " . $tokenString);
$parts = explode('.', $tokenString);
if (count($parts) != 2) {
Log::error("验证失败token无效");
return false;
}
@ -54,31 +51,27 @@ 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;
}
}