fix : jwt

This commit is contained in:
2025-09-12 23:54:03 +08:00
parent ea6abe3043
commit 68ecac2fb9
8 changed files with 121 additions and 42 deletions

View File

@@ -14,8 +14,10 @@ use App\Exception\ErrException;
use App\Interface\CheckTokenInterface;
use App\Lib\Jwt\JwtFactory;
use Hyperf\Di\Annotation\Inject;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\UnencryptedToken;
use App\Interface\JwtInterface;
use function Hyperf\Support\value;
final class BaseTokenService implements CheckTokenInterface
{
@@ -35,14 +37,6 @@ final class BaseTokenService implements CheckTokenInterface
return $this->jwtFactory->get($this->jwt);
}
/**
* @return JwtInterface
*/
public function getApiJwt(): JwtInterface
{
return $this->jwtFactory->get('api');
}
/**
* @param UnencryptedToken $token
* @return void
@@ -51,4 +45,20 @@ final class BaseTokenService implements CheckTokenInterface
{
$this->getJwt()->hasBlackList($token) && throw new ErrException('token已过期');
}
/**
* @param UnencryptedToken $token
* @return \Closure
*/
public function refreshToken(UnencryptedToken $token): \Closure
{
return value(static function (JwtInterface $jwt) use ($token) {
$jwt->addBlackList($token);
return [
'access_token' => $jwt->builderAccessToken($token->claims()->get(RegisteredClaims::ID))->toString(),
'refresh_token' => $jwt->builderRefreshToken($token->claims()->get(RegisteredClaims::ID))->toString(),
'expire_at' => (int) $jwt->getConfig('ttl', 0),
];
}, $this->getJwt());
}
}