hyperf-chat/app/Middleware/JWTAuthMiddleware.php

45 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Middleware;
use Phper666\JWTAuth\Middleware\JWTAuthMiddleware as BaseJWTAuthMiddleware;
use Phper666\JWTAuth\Exception\TokenValidException;
use Phper666\JWTAuth\Util\JWTUtil;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\SimpleCache\InvalidArgumentException;
class JWTAuthMiddleware extends BaseJWTAuthMiddleware
{
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @throws InvalidArgumentException
* @throws \Throwable
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$isValidToken = false;
// 根据具体业务判断逻辑走向这里假设用户携带的token有效
$token = $request->getHeaderLine('Authorization') ?? '';
if (strlen($token) > 0) {
try {
$token = JWTUtil::handleToken($token);
if ($token !== false && $this->jwt->checkToken($token)) {
$isValidToken = true;
}
} catch (InvalidArgumentException $e) {
throw new TokenValidException('Token authentication does not pass', 401);
}
}
if ($isValidToken) {
return $handler->handle($request);
}
throw new TokenValidException('Token authentication does not pass', 401);
}
}