feat : jwt

This commit is contained in:
2025-09-12 18:12:30 +08:00
parent a80c237bbb
commit ff3e0105ec
14 changed files with 362 additions and 38 deletions

View File

@@ -0,0 +1,34 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\AdminUser;
use App\Lib\Jwt\RequestScopedTokenTrait;
use App\Service\Admin\BaseAdminService;
use Lcobucci\JWT\Token\RegisteredClaims;
class UserService extends BaseAdminService
{
use RequestScopedTokenTrait;
public function handle(): array
{
var_dump($this->getToken()->claims()->all());
var_dump($this->getToken()->claims()->get(RegisteredClaims::ID));
var_dump($this->getToken()->claims()->get(RegisteredClaims::AUDIENCE));
return $this->adminReturn->success();
}
public function refresh(): array
{
return $this->adminReturn->success();
}
}

View File

@@ -16,6 +16,7 @@ use App\Interface\JwtInterface;
use App\Lib\Jwt\JwtFactory;
use App\Repository\AdminUserRepository;
use App\Service\Admin\BaseAdminService;
use App\Service\BaseTokenService;
use Hyperf\Di\Annotation\Inject;
class LoginService extends BaseAdminService
@@ -32,10 +33,10 @@ class LoginService extends BaseAdminService
protected AdminUserRepository $userRepository;
/**
* @var JwtFactory
* @var BaseTokenService
*/
#[Inject]
protected JwtFactory $jwtFactory;
protected BaseTokenService $tokenService;
/**
* @return array
@@ -53,20 +54,12 @@ class LoginService extends BaseAdminService
if ($adminInfo->status == AdminUserStatusCode::DISABLE) throw new ErrException('用户已禁用');
$jwtHandle = $this->getJwt();
$jwtHandle = $this->tokenService->getJwt();
return [
return $this->adminReturn->success('success',[
'access_token' => $jwtHandle->builderAccessToken((string) $adminInfo->id)->toString(),
'refresh_token' => $jwtHandle->builderRefreshToken((string) $adminInfo->id)->toString(),
'expire_at' => (int) $jwtHandle->getConfig('ttl', 0),
];
}
/**
* @return JwtInterface
*/
private function getJwt(): JwtInterface
{
return $this->jwtFactory->get($this->jwt);
]);
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service;
use App\Exception\ErrException;
use App\Interface\CheckTokenInterface;
use App\Lib\Jwt\JwtFactory;
use Hyperf\Di\Annotation\Inject;
use Lcobucci\JWT\UnencryptedToken;
use App\Interface\JwtInterface;
final class BaseTokenService implements CheckTokenInterface
{
private string $jwt = 'admin';
/**
* @var JwtFactory
*/
#[Inject]
protected JwtFactory $jwtFactory;
/**
* @return JwtInterface
*/
public function getJwt(): JwtInterface
{
return $this->jwtFactory->get($this->jwt);
}
/**
* @return JwtInterface
*/
public function getApiJwt(): JwtInterface
{
return $this->jwtFactory->get('api');
}
/**
* @param UnencryptedToken $token
* @return void
*/
public function checkJwt(UnencryptedToken $token): void
{
$this->getJwt()->hasBlackList($token) && throw new ErrException('token已过期');
}
}