From 8665b27294553ee8f2c3ee60ef18c5bf706e108d Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Mon, 15 Sep 2025 09:32:15 +0800 Subject: [PATCH] feat : jwt --- app/Lib/Jwt/AbstractJwt.php | 64 ++++++++----------- .../Token/AbstractTokenMiddleware.php | 9 ++- .../Token/RefreshAdminTokenMiddleware.php | 7 +- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/Lib/Jwt/AbstractJwt.php b/app/Lib/Jwt/AbstractJwt.php index aee0eaa..8383848 100644 --- a/app/Lib/Jwt/AbstractJwt.php +++ b/app/Lib/Jwt/AbstractJwt.php @@ -88,24 +88,20 @@ abstract class AbstractJwt implements JwtInterface */ public function parserAccessToken(string $accessToken): UnencryptedToken { - try { - return $this->getJwtFacade() - ->parse( - $accessToken, - new SignedWith( - $this->getSigner(), - $this->getSigningKey() - ), - new StrictValidAt( - $this->clock, - $this->clock->now()->diff($this->getExpireAt($this->clock->now())) - ), - $this->getBlackListConstraint(), - $this->refreshTokenConstraint - ); - } catch (RequiredConstraintsViolated $e) { - throw new ErrException('token过期',ResultCode::JWT_EXPIRED,['err_msg' => $e->getMessage()]); - } + return $this->getJwtFacade() + ->parse( + $accessToken, + new SignedWith( + $this->getSigner(), + $this->getSigningKey() + ), + new StrictValidAt( + $this->clock, + $this->clock->now()->diff($this->getExpireAt($this->clock->now())) + ), + $this->getBlackListConstraint(), + $this->refreshTokenConstraint + ); } /** @@ -114,24 +110,20 @@ abstract class AbstractJwt implements JwtInterface */ public function parserRefreshToken(string $refreshToken): UnencryptedToken { - try { - return $this->getJwtFacade() - ->parse( - $refreshToken, - new SignedWith( - $this->getSigner(), - $this->getSigningKey() - ), - new StrictValidAt( - $this->clock, - $this->clock->now()->diff($this->getRefreshExpireAt($this->clock->now())) - ), - $this->getBlackListConstraint(), - $this->accessTokenConstraint - ); - }catch (RequiredConstraintsViolated $e) { - throw new ErrException('refresh_token过期',ResultCode::JWT_EXPIRED,['err_msg' => $e->getMessage()]); - } + return $this->getJwtFacade() + ->parse( + $refreshToken, + new SignedWith( + $this->getSigner(), + $this->getSigningKey() + ), + new StrictValidAt( + $this->clock, + $this->clock->now()->diff($this->getRefreshExpireAt($this->clock->now())) + ), + $this->getBlackListConstraint(), + $this->accessTokenConstraint + ); } /** diff --git a/app/Middleware/Token/AbstractTokenMiddleware.php b/app/Middleware/Token/AbstractTokenMiddleware.php index 7c6661b..a50d5cc 100644 --- a/app/Middleware/Token/AbstractTokenMiddleware.php +++ b/app/Middleware/Token/AbstractTokenMiddleware.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Middleware\Token; +use App\Constants\ResultCode; +use App\Exception\ErrException; use App\Interface\CheckTokenInterface; use App\Interface\JwtInterface; use App\Lib\Jwt\JwtFactory; @@ -11,6 +13,7 @@ use Hyperf\Collection\Arr; use Hyperf\Stringable\Str; use Lcobucci\JWT\Token; use Lcobucci\JWT\UnencryptedToken; +use Lcobucci\JWT\Validation\RequiredConstraintsViolated; use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -57,7 +60,11 @@ abstract class AbstractTokenMiddleware */ protected function parserToken(ServerRequestInterface $request): Token { - return $this->getJwt()->parserAccessToken($this->getToken($request)); + try { + return $this->getJwt()->parserAccessToken($this->getToken($request)); + } catch (RequiredConstraintsViolated $e) { + throw new ErrException('token过期',ResultCode::JWT_EXPIRED,['err_msg' => $e->getMessage()]); + } } /** diff --git a/app/Middleware/Token/RefreshAdminTokenMiddleware.php b/app/Middleware/Token/RefreshAdminTokenMiddleware.php index f9438ad..5265ec2 100644 --- a/app/Middleware/Token/RefreshAdminTokenMiddleware.php +++ b/app/Middleware/Token/RefreshAdminTokenMiddleware.php @@ -9,6 +9,7 @@ use App\Exception\ErrException; use App\Interface\JwtInterface; use Lcobucci\JWT\Token\RegisteredClaims; use Lcobucci\JWT\UnencryptedToken; +use Lcobucci\JWT\Validation\RequiredConstraintsViolated; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -49,7 +50,11 @@ class RefreshAdminTokenMiddleware extends AbstractTokenMiddleware */ protected function parserToken(ServerRequestInterface $request): UnencryptedToken { - return $this->getJwt()->parserRefreshToken($this->getToken($request)); + try { + return $this->getJwt()->parserRefreshToken($this->getToken($request)); + } catch (RequiredConstraintsViolated $e) { + throw new ErrException('token过期',ResultCode::JWT_EXPIRED,['err_msg' => $e->getMessage()]); + } } /**