mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 11:22:10 +08:00
feat : jwt
This commit is contained in:
@@ -88,24 +88,20 @@ abstract class AbstractJwt implements JwtInterface
|
|||||||
*/
|
*/
|
||||||
public function parserAccessToken(string $accessToken): UnencryptedToken
|
public function parserAccessToken(string $accessToken): UnencryptedToken
|
||||||
{
|
{
|
||||||
try {
|
return $this->getJwtFacade()
|
||||||
return $this->getJwtFacade()
|
->parse(
|
||||||
->parse(
|
$accessToken,
|
||||||
$accessToken,
|
new SignedWith(
|
||||||
new SignedWith(
|
$this->getSigner(),
|
||||||
$this->getSigner(),
|
$this->getSigningKey()
|
||||||
$this->getSigningKey()
|
),
|
||||||
),
|
new StrictValidAt(
|
||||||
new StrictValidAt(
|
$this->clock,
|
||||||
$this->clock,
|
$this->clock->now()->diff($this->getExpireAt($this->clock->now()))
|
||||||
$this->clock->now()->diff($this->getExpireAt($this->clock->now()))
|
),
|
||||||
),
|
$this->getBlackListConstraint(),
|
||||||
$this->getBlackListConstraint(),
|
$this->refreshTokenConstraint
|
||||||
$this->refreshTokenConstraint
|
);
|
||||||
);
|
|
||||||
} catch (RequiredConstraintsViolated $e) {
|
|
||||||
throw new ErrException('token过期',ResultCode::JWT_EXPIRED,['err_msg' => $e->getMessage()]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,24 +110,20 @@ abstract class AbstractJwt implements JwtInterface
|
|||||||
*/
|
*/
|
||||||
public function parserRefreshToken(string $refreshToken): UnencryptedToken
|
public function parserRefreshToken(string $refreshToken): UnencryptedToken
|
||||||
{
|
{
|
||||||
try {
|
return $this->getJwtFacade()
|
||||||
return $this->getJwtFacade()
|
->parse(
|
||||||
->parse(
|
$refreshToken,
|
||||||
$refreshToken,
|
new SignedWith(
|
||||||
new SignedWith(
|
$this->getSigner(),
|
||||||
$this->getSigner(),
|
$this->getSigningKey()
|
||||||
$this->getSigningKey()
|
),
|
||||||
),
|
new StrictValidAt(
|
||||||
new StrictValidAt(
|
$this->clock,
|
||||||
$this->clock,
|
$this->clock->now()->diff($this->getRefreshExpireAt($this->clock->now()))
|
||||||
$this->clock->now()->diff($this->getRefreshExpireAt($this->clock->now()))
|
),
|
||||||
),
|
$this->getBlackListConstraint(),
|
||||||
$this->getBlackListConstraint(),
|
$this->accessTokenConstraint
|
||||||
$this->accessTokenConstraint
|
);
|
||||||
);
|
|
||||||
}catch (RequiredConstraintsViolated $e) {
|
|
||||||
throw new ErrException('refresh_token过期',ResultCode::JWT_EXPIRED,['err_msg' => $e->getMessage()]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Middleware\Token;
|
namespace App\Middleware\Token;
|
||||||
|
|
||||||
|
use App\Constants\ResultCode;
|
||||||
|
use App\Exception\ErrException;
|
||||||
use App\Interface\CheckTokenInterface;
|
use App\Interface\CheckTokenInterface;
|
||||||
use App\Interface\JwtInterface;
|
use App\Interface\JwtInterface;
|
||||||
use App\Lib\Jwt\JwtFactory;
|
use App\Lib\Jwt\JwtFactory;
|
||||||
@@ -11,6 +13,7 @@ use Hyperf\Collection\Arr;
|
|||||||
use Hyperf\Stringable\Str;
|
use Hyperf\Stringable\Str;
|
||||||
use Lcobucci\JWT\Token;
|
use Lcobucci\JWT\Token;
|
||||||
use Lcobucci\JWT\UnencryptedToken;
|
use Lcobucci\JWT\UnencryptedToken;
|
||||||
|
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
@@ -57,7 +60,11 @@ abstract class AbstractTokenMiddleware
|
|||||||
*/
|
*/
|
||||||
protected function parserToken(ServerRequestInterface $request): Token
|
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()]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use App\Exception\ErrException;
|
|||||||
use App\Interface\JwtInterface;
|
use App\Interface\JwtInterface;
|
||||||
use Lcobucci\JWT\Token\RegisteredClaims;
|
use Lcobucci\JWT\Token\RegisteredClaims;
|
||||||
use Lcobucci\JWT\UnencryptedToken;
|
use Lcobucci\JWT\UnencryptedToken;
|
||||||
|
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
@@ -49,7 +50,11 @@ class RefreshAdminTokenMiddleware extends AbstractTokenMiddleware
|
|||||||
*/
|
*/
|
||||||
protected function parserToken(ServerRequestInterface $request): UnencryptedToken
|
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()]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user