hyperf-chat/app/Middleware/JWTAuthMiddleware.php

99 lines
2.5 KiB
PHP
Raw Normal View History

2020-11-14 17:37:55 +08:00
<?php
2020-12-26 21:33:40 +08:00
/**
*
* This is my open source code, please do not use it for commercial applications.
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
2021-04-20 16:30:57 +08:00
2020-11-14 17:37:55 +08:00
namespace App\Middleware;
2020-11-24 23:23:12 +08:00
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
2020-11-14 17:37:55 +08:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2020-11-24 23:23:12 +08:00
use Psr\Http\Server\MiddlewareInterface;
2020-11-14 17:37:55 +08:00
use Psr\Http\Server\RequestHandlerInterface;
2020-11-24 23:23:12 +08:00
use Phper666\JWTAuth\JWT;
use Phper666\JWTAuth\Util\JWTUtil;
2020-12-26 21:33:40 +08:00
use Hyperf\Utils\Context;
2020-11-14 17:37:55 +08:00
2020-11-24 23:23:12 +08:00
/**
* Http Token 授权验证中间件
*
* @package App\Middleware
*/
class JWTAuthMiddleware implements MiddlewareInterface
2020-11-14 17:37:55 +08:00
{
/**
2020-11-24 23:23:12 +08:00
* @var RequestInterface
*/
protected $request;
/**
* @var HttpResponse
*/
protected $response;
/**
* @var JWT
2020-11-14 17:37:55 +08:00
*/
2020-11-24 23:23:12 +08:00
protected $jwt;
public function __construct(HttpResponse $response, RequestInterface $request, JWT $jwt)
{
$this->response = $response;
2021-04-20 16:30:57 +08:00
$this->request = $request;
$this->jwt = $jwt;
2020-11-24 23:23:12 +08:00
}
2020-11-14 17:37:55 +08:00
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
2020-12-01 13:54:40 +08:00
$isValidToken = false;
2020-11-24 23:23:12 +08:00
// 获取请求token
2020-12-01 13:54:40 +08:00
$token = $request->getHeaderLine('Authorization');
if (empty($token)) {
$token = $this->request->input('token', '');
2020-12-09 23:41:31 +08:00
} else {
$token = JWTUtil::handleToken($token);
2020-12-01 13:54:40 +08:00
}
2020-11-24 23:23:12 +08:00
if (!empty($token)) {
2020-11-14 17:37:55 +08:00
try {
if ($token !== false && $this->jwt->checkToken($token)) {
$isValidToken = true;
}
2020-11-24 23:23:12 +08:00
} catch (\Exception $e) {
2020-11-14 17:37:55 +08:00
}
}
2020-11-24 23:23:12 +08:00
if (!$isValidToken) {
return $this->response->withStatus(401)->json([
2021-04-20 16:30:57 +08:00
'code' => 401,
2020-11-24 23:23:12 +08:00
'message' => 'Token authentication does not pass',
]);
2020-11-14 17:37:55 +08:00
}
2020-12-26 21:33:40 +08:00
$request = $this->setRequestContext($token);
2020-11-24 23:23:12 +08:00
return $handler->handle($request);
2020-11-14 17:37:55 +08:00
}
2020-12-26 21:33:40 +08:00
private function setRequestContext(string $token): ServerRequestInterface
{
$request = Context::get(ServerRequestInterface::class);
$jwtData = $this->jwt->getParserData($token);
$request = $request->withAttribute('auth_data', $jwtData);
Context::set(ServerRequestInterface::class, $request);
return $request;
}
2020-11-14 17:37:55 +08:00
}