2020-11-07 22:57:10 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2021-09-11 12:41:28 +08:00
|
|
|
|
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
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
|
|
|
|
namespace App\Middleware;
|
|
|
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
|
2020-11-24 23:23:12 +08:00
|
|
|
/**
|
|
|
|
* WebSocket token 授权验证中间件
|
|
|
|
*
|
|
|
|
* @package App\Middleware
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
class WebSocketAuthMiddleware implements MiddlewareInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
2021-05-23 17:04:20 +08:00
|
|
|
/**
|
|
|
|
* 授权验证守卫
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $guard = 'jwt';
|
|
|
|
|
2020-11-07 22:57:10 +08:00
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
|
{
|
|
|
|
// 授权验证拦截握手请求并实现权限检查
|
2021-05-23 17:04:20 +08:00
|
|
|
if (auth($this->guard)->guest()) {
|
2020-11-07 22:57:10 +08:00
|
|
|
return $this->container->get(\Hyperf\HttpServer\Contract\ResponseInterface::class)->raw('Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $handler->handle($request);
|
|
|
|
}
|
|
|
|
}
|