Ver Fonte

fix: 修复token不失效

runphp há 1 mês atrás
pai
commit
9b7ed9e7ad
2 ficheiros alterados com 10 adições e 2 exclusões
  1. 3 0
      src/Auth.php
  2. 7 2
      src/Hook/AuthHook.php

+ 3 - 0
src/Auth.php

@@ -72,6 +72,9 @@ class Auth implements AuthInterface
         JWT::$leeway = self::SLEEP_WAY;
         try {
             $payload = JWT::decode($jwt, new Key($this->config['jwt_secret'], self::ALGORITHM));
+        } catch (ExpiredException $e) {
+            // ... 忽略
+            $payload = $e->getPayload();
         } catch (\Exception) {
             return;
         }

+ 7 - 2
src/Hook/AuthHook.php

@@ -4,6 +4,7 @@ declare(strict_types=1);
 
 namespace SixShop\Auth\Hook;
 
+use Firebase\JWT\ExpiredException;
 use SixShop\Auth\Auth;
 use SixShop\Core\Attribute\Hook;
 use think\Cache;
@@ -23,9 +24,13 @@ class AuthHook
     public function checkToken($payload): void
     {
         if ($this->cache->has(self::TOKEN_REVOKE . $payload->jti)
-            && $this->cache->get(self::TOKEN_REVOKE . $payload->jti) < time() + Auth::SLEEP_WAY) {
-            throw new \Exception('token 已失效');
+            && $this->cache->get(self::TOKEN_REVOKE . $payload->jti) > time() + Auth::SLEEP_WAY) {
+            return;
         }
+        $ex = new ExpiredException('Expired token');
+        $ex->setPayload($payload->exp);
+        $ex->setTimestamp($timestamp);
+        throw $ex;
     }
 
     #[Hook("token_revoke")]