mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 11:22:10 +08:00
43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Middleware\Admin;
|
|
|
|
use App\Common\Interface\JwtInterface;
|
|
use App\Constants\ResultCode;
|
|
use App\Exception\ErrException;
|
|
use App\Middleware\Token\AbstractTokenMiddleware;
|
|
use Hyperf\Context\Context;
|
|
use Lcobucci\JWT\Token\RegisteredClaims;
|
|
use Lcobucci\JWT\UnencryptedToken;
|
|
use function Hyperf\Support\env;
|
|
|
|
final class AdminTokenMiddleware extends AbstractTokenMiddleware
|
|
{
|
|
public function getJwt(): JwtInterface
|
|
{
|
|
return $this->jwtFactory->get('admin');
|
|
}
|
|
|
|
/**
|
|
* @param UnencryptedToken $token
|
|
* @return void
|
|
*/
|
|
public function checkIssuer(UnencryptedToken $token): void
|
|
{
|
|
$audience = $token->claims()->get(RegisteredClaims::ISSUER);
|
|
|
|
if ($audience !== env('APP_NAME') .'_admin') throw new ErrException('token错误',ResultCode::JWT_ERROR);
|
|
}
|
|
|
|
/**
|
|
* @param UnencryptedToken $token
|
|
* @return void
|
|
*/
|
|
public function setContext(UnencryptedToken $token): void
|
|
{
|
|
Context::set('current_admin_id',(int)$token->claims()?->get(RegisteredClaims::ID) ?? 0);
|
|
}
|
|
}
|