mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKit.git
synced 2026-01-15 00:35:08 +08:00
feat(account.auth): 添加了认证中间件
Signed-off-by: 李东云 <dongyun.li@luxcreo.ai>
This commit is contained in:
42
src/Account/Middleware/AuthenticateMiddleware.php
Normal file
42
src/Account/Middleware/AuthenticateMiddleware.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user