feat(account.auth): 添加了认证中间件

Signed-off-by: 李东云 <dongyun.li@luxcreo.ai>
This commit is contained in:
李东云
2022-04-29 02:01:44 +08:00
parent 5b4daebd6e
commit c29e607591

View File

@@ -0,0 +1,42 @@
<?php
namespace Singularity\HDK\Account\Middleware;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Singularity\HDK\Account\Services\Auth\AuthenticationInterface;
use Singularity\HDK\Utils\Enumerations\Http\Header\RFCs\RFC7486;
use Singularity\HDK\Utils\Exceptions\Unauthorized;
/**
* 通用鉴权中间件
*/
class AuthenticateMiddleware implements MiddlewareInterface
{
/**
* @Inject
* @var \Singularity\HDK\Account\Services\Auth\AuthenticationInterface
*/
private AuthenticationInterface $authentication;
/**
* @inheritDoc
*/
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$token = $this->authentication->parseTokenFromHeaders();
if (empty($token) || $token === 'null' || $token === 'undefined' || $token === 'false') {
throw new Unauthorized(authenticationType: RFC7486::HOBA);
}
// 验证 token 中的参数
$this->authentication->verified($token);
return $handler->handle($request);
}
}