From 5df35d68bd0681aa331531b23fa290b23f18bc1d Mon Sep 17 00:00:00 2001 From: valentint Date: Fri, 17 Feb 2017 15:50:19 +0300 Subject: [PATCH 01/21] handshake middleware was added for server --- DependencyInjection/Configuration.php | 14 ++++++ DependencyInjection/GosWebSocketExtension.php | 14 ++++++ Resources/config/services/services.yml | 4 ++ Resources/docs/ConfigurationReference.md | 3 ++ .../Registry/HandshakeMiddlewareRegistry.php | 47 +++++++++++++++++++ .../Stack/HandshakeMiddlewareInterface.php | 13 +++++ Server/Type/WebSocketServer.php | 16 ++++++- 7 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 Server/App/Registry/HandshakeMiddlewareRegistry.php create mode 100644 Server/App/Stack/HandshakeMiddlewareInterface.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index e4404a41..3430262d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -95,6 +95,20 @@ public function getConfigTreeBuilder() ->end() ->end() ->end() + ->arrayNode('handshake_middleware') + ->prototype('array') + ->children() + ->scalarNode('class') + ->end() + ->arrayNode('arguments') + ->prototype('scalar') + ->example('@some_service') + ->end() + ->end() + ->end() + ->example('{class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: []}') + ->end() + ->end() ->end() ->end() ->arrayNode('rpc') diff --git a/DependencyInjection/GosWebSocketExtension.php b/DependencyInjection/GosWebSocketExtension.php index 48a7e2dc..e1be1dc4 100644 --- a/DependencyInjection/GosWebSocketExtension.php +++ b/DependencyInjection/GosWebSocketExtension.php @@ -64,6 +64,20 @@ public function load(array $configs, ContainerBuilder $container) } } + $HandshakeMiddlewareRegistryDef = $container->getDefinition('gos_web_socket.handshake_middleware.registry'); + if (!empty($configs['server']['handshake_middleware'])) { + foreach ($configs['server']['handshake_middleware'] as $middleware) { + $arguments = []; + foreach ($middleware['arguments'] as $argument) { + $arguments[] = new Reference(ltrim($argument, '@')); + } + $middleware['arguments'] = $arguments; + $HandshakeMiddlewareRegistryDef->addMethodCall('addMiddleware', [$middleware]); + } + } + + + $container->setParameter('web_socket_server.client_storage.ttl', $configs['client']['storage']['ttl']); $container->setParameter('web_socket_server.client_storage.prefix', $configs['client']['storage']['prefix']); diff --git a/Resources/config/services/services.yml b/Resources/config/services/services.yml index b8c5903e..57a69866 100644 --- a/Resources/config/services/services.yml +++ b/Resources/config/services/services.yml @@ -52,6 +52,7 @@ services: - '@gos_web_socket.wamp.topic_manager' - '@gos_web_socket.server_push_handler.registry' - '@?monolog.logger.websocket' + - '@gos_web_socket.handshake_middleware.registry' tags: - { name: gos_web_socket.server } @@ -85,6 +86,9 @@ services: gos_web_socket.origins.registry: class: Gos\Bundle\WebSocketBundle\Server\App\Registry\OriginRegistry + gos_web_socket.handshake_middleware.registry: + class: Gos\Bundle\WebSocketBundle\Server\App\Registry\HandshakeMiddlewareRegistry + gos_web_socket_server.wamp_application: class: Gos\Bundle\WebSocketBundle\Server\App\WampApplication public: false diff --git a/Resources/docs/ConfigurationReference.md b/Resources/docs/ConfigurationReference.md index 7d46fcca..8f2ab8bc 100644 --- a/Resources/docs/ConfigurationReference.md +++ b/Resources/docs/ConfigurationReference.md @@ -19,6 +19,9 @@ gos_web_socket: - @AcmeBundle/Resources/config/pubsub/routing.yml context: tokenSeparator: "/" + handshake_middleware: [] + # {class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: ['@some_service']} + rpc: [] topics: [] periodic: [] diff --git a/Server/App/Registry/HandshakeMiddlewareRegistry.php b/Server/App/Registry/HandshakeMiddlewareRegistry.php new file mode 100644 index 00000000..6714cb47 --- /dev/null +++ b/Server/App/Registry/HandshakeMiddlewareRegistry.php @@ -0,0 +1,47 @@ + + */ +class HandshakeMiddlewareRegistry +{ + /** + * @var [] + */ + protected $middlewares; + + public function __construct() + { + $this->middlewares = []; + } + + /** + * @param array $middleware + * @throws \Exception + */ + public function addMiddleware($middleware) + { + $interfaces = class_implements($middleware['class']); + + if (!isset($interfaces['Ratchet\Http\HttpServerInterface'])) { + throw new \Exception("'Ratchet\\Http\\HttpServerInterface' in not implemented by '{$middleware['class']}'"); + } + + $arguments = array_merge([$middleware['class']], $middleware['arguments']); + + $this->middlewares[] = $arguments; + } + + /** + * @return [] + */ + public function getMiddlewares() + { + return $this->middlewares; + } +} diff --git a/Server/App/Stack/HandshakeMiddlewareInterface.php b/Server/App/Stack/HandshakeMiddlewareInterface.php new file mode 100644 index 00000000..311d5b16 --- /dev/null +++ b/Server/App/Stack/HandshakeMiddlewareInterface.php @@ -0,0 +1,13 @@ + + */ +interface HandshakeMiddlewareInterface extends HttpServerInterface +{ +} diff --git a/Server/Type/WebSocketServer.php b/Server/Type/WebSocketServer.php index 861da907..0406aac0 100644 --- a/Server/Type/WebSocketServer.php +++ b/Server/Type/WebSocketServer.php @@ -7,6 +7,7 @@ use Gos\Bundle\WebSocketBundle\Periodic\PeriodicInterface; use Gos\Bundle\WebSocketBundle\Periodic\PeriodicMemoryUsage; use Gos\Bundle\WebSocketBundle\Pusher\ServerPushHandlerRegistry; +use Gos\Bundle\WebSocketBundle\Server\App\Registry\HandshakeMiddlewareRegistry; use Gos\Bundle\WebSocketBundle\Server\App\Registry\OriginRegistry; use Gos\Bundle\WebSocketBundle\Server\App\Registry\PeriodicRegistry; use Gos\Bundle\WebSocketBundle\Server\App\WampApplication; @@ -70,6 +71,9 @@ class WebSocketServer implements ServerInterface /** @var TopicManager */ protected $topicManager; + /** @var HandshakeMiddlewareRegistry */ + protected $handshakeMiddlewareRegistry; + /** * @param LoopInterface $loop * @param EventDispatcherInterface $eventDispatcher @@ -79,6 +83,7 @@ class WebSocketServer implements ServerInterface * @param bool $originCheck * @param TopicManager $topicManager * @param LoggerInterface|null $logger + * @param HandshakeMiddlewareRegistry $handshakeMiddlewareRegistry */ public function __construct( LoopInterface $loop, @@ -89,7 +94,8 @@ public function __construct( $originCheck, TopicManager $topicManager, ServerPushHandlerRegistry $serverPushHandlerRegistry, - LoggerInterface $logger = null + LoggerInterface $logger = null, + HandshakeMiddlewareRegistry $handshakeMiddlewareRegistry ) { $this->loop = $loop; $this->eventDispatcher = $eventDispatcher; @@ -101,6 +107,7 @@ public function __construct( $this->topicManager = $topicManager; $this->serverPusherHandlerRegistry = $serverPushHandlerRegistry; $this->sessionHandler = new NullSessionHandler(); + $this->handshakeMiddlewareRegistry = $handshakeMiddlewareRegistry; } /** @@ -154,6 +161,13 @@ public function launch($host, $port, $profile) $stack->push('Gos\Bundle\WebSocketBundle\Server\App\Stack\OriginCheck', $allowedOrigins, $this->eventDispatcher); } + + if (!empty($this->handshakeMiddlewareRegistry->getMiddlewares())) { + foreach ($this->handshakeMiddlewareRegistry->getMiddlewares() as $middleware) { + call_user_func([$stack, 'push'], $middleware); + } + } + $stack ->push('Ratchet\WebSocket\WsServer') ->push('Gos\Bundle\WebSocketBundle\Server\App\Stack\WampConnectionPeriodicTimer', $this->loop) From 8f46553a4155ed2bd18d23a8aced9a67870851e9 Mon Sep 17 00:00:00 2001 From: valentint Date: Fri, 17 Feb 2017 18:25:43 +0300 Subject: [PATCH 02/21] middleware fabric partly --- Resources/docs/ConfigurationReference.md | 2 +- Server/App/Stack/Factory/Middleware.php | 96 ++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 Server/App/Stack/Factory/Middleware.php diff --git a/Resources/docs/ConfigurationReference.md b/Resources/docs/ConfigurationReference.md index 8f2ab8bc..88be805d 100644 --- a/Resources/docs/ConfigurationReference.md +++ b/Resources/docs/ConfigurationReference.md @@ -20,7 +20,7 @@ gos_web_socket: context: tokenSeparator: "/" handshake_middleware: [] - # {class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: ['@some_service']} + # - {class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: ['@some_service']} rpc: [] topics: [] diff --git a/Server/App/Stack/Factory/Middleware.php b/Server/App/Stack/Factory/Middleware.php new file mode 100644 index 00000000..07d0b35c --- /dev/null +++ b/Server/App/Stack/Factory/Middleware.php @@ -0,0 +1,96 @@ + + */ +class Middleware extends HttpServerInterface +{ + /** + * @var \Ratchet\MessageComponentInterface + */ + protected $_component; + + /** + * @var HandshakeMiddlewareInterface + */ + protected $_middleware; + + /** + * @param MessageComponentInterface $component + * @param HandshakeMiddlewareInterface $middleware + */ + public function __construct( + MessageComponentInterface $component, + HandshakeMiddlewareInterface $middleware + ) { + $this->_component = $component; + $this->_middleware = $middleware; + } + + /** + * {@inheritdoc} + */ + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) + { + $this->_middleware->onOpen($conn, $request); + + return $this->_component->onOpen($conn, $request); + } + + /** + * {@inheritdoc} + */ + public function onMessage(ConnectionInterface $from, $msg) + { + $this->_middleware->onMessage($from, $msg); + + return $this->_component->onMessage($from, $msg); + } + + /** + * {@inheritdoc} + */ + public function onClose(ConnectionInterface $conn) + { + $this->_middleware->onClose($conn); + + return $this->_component->onClose($conn); + } + + /** + * {@inheritdoc} + */ + public function onError(ConnectionInterface $conn, \Exception $e) + { + $this->_middleware->onError($conn, $e); + + return $this->_component->onError($conn, $e); + } + + /** + * Close a connection with an HTTP response. + * + * @param \Ratchet\ConnectionInterface $conn + * @param int $code HTTP status code + */ + protected function close(ConnectionInterface $conn, $code = 400) + { + $response = new Response($code, array( + 'X-Powered-By' => \Ratchet\VERSION, + )); + + $conn->send((string) $response); + $conn->close(); + } +} From e2afa63a8a68d25a2905c208b36511856aed41d6 Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 11:43:07 +0300 Subject: [PATCH 03/21] middleware factory done --- DependencyInjection/Configuration.php | 13 ++------- README.md | 1 + Resources/docs/ConfigurationReference.md | 2 +- Resources/docs/HandshakeMiddleware.md | 14 ++++++++++ Server/App/Stack/Factory/Middleware.php | 34 +++++++----------------- Server/Type/WebSocketServer.php | 2 +- 6 files changed, 28 insertions(+), 38 deletions(-) create mode 100644 Resources/docs/HandshakeMiddleware.md diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 3430262d..869d91e4 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -96,17 +96,8 @@ public function getConfigTreeBuilder() ->end() ->end() ->arrayNode('handshake_middleware') - ->prototype('array') - ->children() - ->scalarNode('class') - ->end() - ->arrayNode('arguments') - ->prototype('scalar') - ->example('@some_service') - ->end() - ->end() - ->end() - ->example('{class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: []}') + ->prototype('scalar') + ->example('@some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface') ->end() ->end() ->end() diff --git a/README.md b/README.md index a276e4b7..e076929d 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Resources * [Performance Bench](Resources/docs/Performance.md) * [Push integration](Resources/docs/Pusher.md) * [SSL configuration](Resources/docs/Ssl.md) +* [Handshake Middleware For Server](Resources/docs/HandshakeMiddleware.md) Code Cookbook -------------- diff --git a/Resources/docs/ConfigurationReference.md b/Resources/docs/ConfigurationReference.md index 88be805d..8ba2ea11 100644 --- a/Resources/docs/ConfigurationReference.md +++ b/Resources/docs/ConfigurationReference.md @@ -20,7 +20,7 @@ gos_web_socket: context: tokenSeparator: "/" handshake_middleware: [] - # - {class: Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface, arguments: ['@some_service']} + # - @some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface rpc: [] topics: [] diff --git a/Resources/docs/HandshakeMiddleware.md b/Resources/docs/HandshakeMiddleware.md new file mode 100644 index 00000000..225ed383 --- /dev/null +++ b/Resources/docs/HandshakeMiddleware.md @@ -0,0 +1,14 @@ +# HandshakeMiddleware + +You can add any middleware as service to server with your business logic + + +**Bundle Configuration** + +```yaml +# Gos Web Socket Bundle +gos_web_socket: + server: + handshake_middleware: [] + - @some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface +``` diff --git a/Server/App/Stack/Factory/Middleware.php b/Server/App/Stack/Factory/Middleware.php index 07d0b35c..a6378133 100644 --- a/Server/App/Stack/Factory/Middleware.php +++ b/Server/App/Stack/Factory/Middleware.php @@ -14,7 +14,7 @@ /** * @author Johann Saunier */ -class Middleware extends HttpServerInterface +class Middleware implements HttpServerInterface { /** * @var \Ratchet\MessageComponentInterface @@ -43,9 +43,9 @@ public function __construct( */ public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { - $this->_middleware->onOpen($conn, $request); + $answer = $this->_middleware->onOpen($conn, $request); - return $this->_component->onOpen($conn, $request); + return $answer instanceof ComponentInterface ? $answer : $this->_component->onOpen($conn, $request); } /** @@ -53,9 +53,9 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu */ public function onMessage(ConnectionInterface $from, $msg) { - $this->_middleware->onMessage($from, $msg); + $answer = $this->_middleware->onMessage($from, $msg); - return $this->_component->onMessage($from, $msg); + return $answer instanceof MessageInterface ? $answer : $this->_component->onMessage($from, $msg); } /** @@ -63,9 +63,9 @@ public function onMessage(ConnectionInterface $from, $msg) */ public function onClose(ConnectionInterface $conn) { - $this->_middleware->onClose($conn); + $answer = $this->_middleware->onClose($conn); - return $this->_component->onClose($conn); + return $answer instanceof ComponentInterface ? $answer : $this->_component->onClose($conn); } /** @@ -73,24 +73,8 @@ public function onClose(ConnectionInterface $conn) */ public function onError(ConnectionInterface $conn, \Exception $e) { - $this->_middleware->onError($conn, $e); + $answer = $this->_middleware->onError($conn, $e); - return $this->_component->onError($conn, $e); - } - - /** - * Close a connection with an HTTP response. - * - * @param \Ratchet\ConnectionInterface $conn - * @param int $code HTTP status code - */ - protected function close(ConnectionInterface $conn, $code = 400) - { - $response = new Response($code, array( - 'X-Powered-By' => \Ratchet\VERSION, - )); - - $conn->send((string) $response); - $conn->close(); + return $answer instanceof ComponentInterface ? $answer : $this->_component->onError($conn, $e); } } diff --git a/Server/Type/WebSocketServer.php b/Server/Type/WebSocketServer.php index 0406aac0..c3ec095f 100644 --- a/Server/Type/WebSocketServer.php +++ b/Server/Type/WebSocketServer.php @@ -164,7 +164,7 @@ public function launch($host, $port, $profile) if (!empty($this->handshakeMiddlewareRegistry->getMiddlewares())) { foreach ($this->handshakeMiddlewareRegistry->getMiddlewares() as $middleware) { - call_user_func([$stack, 'push'], $middleware); + call_user_func([$stack, 'push'], ['Gos\Bundle\WebSocketBundle\Server\App\Stack\Factory\Middleware', $middleware]); } } From b63368d9d7e2dfe41358cf23ce095e3627fb76d1 Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 11:44:08 +0300 Subject: [PATCH 04/21] fix --- Resources/docs/ConfigurationReference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/docs/ConfigurationReference.md b/Resources/docs/ConfigurationReference.md index 8ba2ea11..5b853e63 100644 --- a/Resources/docs/ConfigurationReference.md +++ b/Resources/docs/ConfigurationReference.md @@ -20,7 +20,7 @@ gos_web_socket: context: tokenSeparator: "/" handshake_middleware: [] - # - @some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface + # - @some_service rpc: [] topics: [] From b6c66dfdec65b9a57fb8afa5ebaa36641d25717f Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 11:45:25 +0300 Subject: [PATCH 05/21] fix --- Resources/docs/HandshakeMiddleware.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/docs/HandshakeMiddleware.md b/Resources/docs/HandshakeMiddleware.md index 225ed383..936318e8 100644 --- a/Resources/docs/HandshakeMiddleware.md +++ b/Resources/docs/HandshakeMiddleware.md @@ -9,6 +9,6 @@ You can add any middleware as service to server with your business logic # Gos Web Socket Bundle gos_web_socket: server: - handshake_middleware: [] + handshake_middleware: - @some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface ``` From f7ecd3f6497467cd7cd46ee1d3a814b1792060f9 Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 13:10:48 +0300 Subject: [PATCH 06/21] update Middleware class --- Server/App/Stack/Factory/Middleware.php | 14 +++++++------- Server/App/Stack/HandshakeMiddlewareInterface.php | 11 ++++++++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Server/App/Stack/Factory/Middleware.php b/Server/App/Stack/Factory/Middleware.php index a6378133..caf990fb 100644 --- a/Server/App/Stack/Factory/Middleware.php +++ b/Server/App/Stack/Factory/Middleware.php @@ -45,7 +45,7 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu { $answer = $this->_middleware->onOpen($conn, $request); - return $answer instanceof ComponentInterface ? $answer : $this->_component->onOpen($conn, $request); + return $answer instanceof ConnectionInterface ? $answer : $this->_component->onOpen($conn, $request); } /** @@ -53,9 +53,9 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu */ public function onMessage(ConnectionInterface $from, $msg) { - $answer = $this->_middleware->onMessage($from, $msg); + $this->_middleware->onMessage($from, $msg); - return $answer instanceof MessageInterface ? $answer : $this->_component->onMessage($from, $msg); + return $this->_component->onMessage($from, $msg); } /** @@ -63,9 +63,9 @@ public function onMessage(ConnectionInterface $from, $msg) */ public function onClose(ConnectionInterface $conn) { - $answer = $this->_middleware->onClose($conn); + $this->_middleware->onClose($conn); - return $answer instanceof ComponentInterface ? $answer : $this->_component->onClose($conn); + return $this->_component->onClose($conn); } /** @@ -73,8 +73,8 @@ public function onClose(ConnectionInterface $conn) */ public function onError(ConnectionInterface $conn, \Exception $e) { - $answer = $this->_middleware->onError($conn, $e); + $this->_middleware->onError($conn, $e); - return $answer instanceof ComponentInterface ? $answer : $this->_component->onError($conn, $e); + return $this->_component->onError($conn, $e); } } diff --git a/Server/App/Stack/HandshakeMiddlewareInterface.php b/Server/App/Stack/HandshakeMiddlewareInterface.php index 311d5b16..a1defcc5 100644 --- a/Server/App/Stack/HandshakeMiddlewareInterface.php +++ b/Server/App/Stack/HandshakeMiddlewareInterface.php @@ -2,12 +2,21 @@ namespace Gos\Bundle\WebSocketBundle\Server\App\Stack; +use Guzzle\Http\Message\RequestInterface; use Ratchet\Http\HttpServerInterface; - +use Ratchet\ConnectionInterface; /** * @author Tkachew <7tkachew@gmail.com> */ interface HandshakeMiddlewareInterface extends HttpServerInterface { + /** + * @param \Ratchet\ConnectionInterface $conn + * @param \Guzzle\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!! + * + * @return ConnectionInterface|void + * @throws \UnexpectedValueException if a RequestInterface is not passed + */ + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null); } From 0edc7c7324fc7e6b7115cf1bf5d9d872c7461f9e Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 13:40:38 +0300 Subject: [PATCH 07/21] ClientRejectedEvent update --- Event/ClientEventListener.php | 4 ++-- Event/ClientRejectedEvent.php | 12 ++++++------ Resources/docs/Events.md | 4 ++-- Server/App/Stack/OriginCheck.php | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Event/ClientEventListener.php b/Event/ClientEventListener.php index c982a7ad..4cc4b8b2 100644 --- a/Event/ClientEventListener.php +++ b/Event/ClientEventListener.php @@ -124,8 +124,8 @@ public function onClientError(ClientErrorEvent $event) */ public function onClientRejected(ClientRejectedEvent $event) { - $this->logger->warning('Client rejected, bad origin', [ - 'origin' => $event->getOrigin(), + $this->logger->warning('Client rejected, msg', [ + 'msg' => $event->getMsg(), ]); } } diff --git a/Event/ClientRejectedEvent.php b/Event/ClientRejectedEvent.php index 7963b4e0..f2f3e951 100644 --- a/Event/ClientRejectedEvent.php +++ b/Event/ClientRejectedEvent.php @@ -13,7 +13,7 @@ class ClientRejectedEvent extends Event /** * @var string */ - protected $origin; + protected $msg; /** * @var RequestInterface @@ -21,21 +21,21 @@ class ClientRejectedEvent extends Event protected $request; /** - * @param string $origin + * @param string $msg * @param RequestInterface $request */ - public function __construct($origin, RequestInterface $request = null) + public function __construct($msg, RequestInterface $request = null) { - $this->origin = $origin; + $this->msg = $msg; $this->request = $request; } /** * @return string */ - public function getOrigin() + public function getMsg() { - return $this->origin; + return $this->msg; } /** diff --git a/Resources/docs/Events.md b/Resources/docs/Events.md index ef6af821..754fc935 100644 --- a/Resources/docs/Events.md +++ b/Resources/docs/Events.md @@ -81,9 +81,9 @@ class AcmeClientEventListener */ public function onClientRejected(ClientRejectedEvent $event) { - $origin = $event->getOrigin; + $msg = $event->getMsg(); - echo 'connection rejected from '. $origin . PHP_EOL; + echo $msg . PHP_EOL; } } ``` diff --git a/Server/App/Stack/OriginCheck.php b/Server/App/Stack/OriginCheck.php index 030a712c..a0682120 100644 --- a/Server/App/Stack/OriginCheck.php +++ b/Server/App/Stack/OriginCheck.php @@ -45,7 +45,7 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu if (!in_array($origin, $this->allowedOrigins)) { $this->eventDispatcher->dispatch( Events::CLIENT_REJECTED, - new ClientRejectedEvent($origin, $request) + new ClientRejectedEvent('connection rejected from '. $origin, $request) ); return $this->close($conn, 403); From ddbdbfe3e532a7a7d2c7cae3a0fe9240656f0277 Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 16:18:05 +0300 Subject: [PATCH 08/21] upd --- .../App/Registry/HandshakeMiddlewareRegistry.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Server/App/Registry/HandshakeMiddlewareRegistry.php b/Server/App/Registry/HandshakeMiddlewareRegistry.php index 6714cb47..881922d3 100644 --- a/Server/App/Registry/HandshakeMiddlewareRegistry.php +++ b/Server/App/Registry/HandshakeMiddlewareRegistry.php @@ -3,7 +3,6 @@ namespace Gos\Bundle\WebSocketBundle\Server\App\Registry; use Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface; -use Ratchet\Http\HttpServerInterface; /** * @author Tkachew <7tkachew@gmail.com> @@ -11,7 +10,7 @@ class HandshakeMiddlewareRegistry { /** - * @var [] + * @var HandshakeMiddlewareInterface[] */ protected $middlewares; @@ -26,19 +25,18 @@ public function __construct() */ public function addMiddleware($middleware) { - $interfaces = class_implements($middleware['class']); + $interfaces = class_implements($middleware); - if (!isset($interfaces['Ratchet\Http\HttpServerInterface'])) { - throw new \Exception("'Ratchet\\Http\\HttpServerInterface' in not implemented by '{$middleware['class']}'"); + if (!isset($interfaces['Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface'])) { + $className = get_class($middleware); + throw new \Exception('"Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface" in not implemented by "' . $className . '"'); } - $arguments = array_merge([$middleware['class']], $middleware['arguments']); - - $this->middlewares[] = $arguments; + $this->middlewares[] = $middleware; } /** - * @return [] + * @return HandshakeMiddlewareInterface[] */ public function getMiddlewares() { From 57aa6c5b2a3dff965cfafc728ae743ba26da2f24 Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 19:04:35 +0300 Subject: [PATCH 09/21] fix config middleware --- DependencyInjection/GosWebSocketExtension.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/DependencyInjection/GosWebSocketExtension.php b/DependencyInjection/GosWebSocketExtension.php index e1be1dc4..589ae3ca 100644 --- a/DependencyInjection/GosWebSocketExtension.php +++ b/DependencyInjection/GosWebSocketExtension.php @@ -67,12 +67,7 @@ public function load(array $configs, ContainerBuilder $container) $HandshakeMiddlewareRegistryDef = $container->getDefinition('gos_web_socket.handshake_middleware.registry'); if (!empty($configs['server']['handshake_middleware'])) { foreach ($configs['server']['handshake_middleware'] as $middleware) { - $arguments = []; - foreach ($middleware['arguments'] as $argument) { - $arguments[] = new Reference(ltrim($argument, '@')); - } - $middleware['arguments'] = $arguments; - $HandshakeMiddlewareRegistryDef->addMethodCall('addMiddleware', [$middleware]); + $HandshakeMiddlewareRegistryDef->addMethodCall('addMiddleware', new Reference(ltrim($middleware, '@'))); } } From 1c2a388fd601825533cf2827236ce5490afaadba Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 19:21:24 +0300 Subject: [PATCH 10/21] fix config middleware --- DependencyInjection/GosWebSocketExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DependencyInjection/GosWebSocketExtension.php b/DependencyInjection/GosWebSocketExtension.php index 589ae3ca..dcda5167 100644 --- a/DependencyInjection/GosWebSocketExtension.php +++ b/DependencyInjection/GosWebSocketExtension.php @@ -67,7 +67,7 @@ public function load(array $configs, ContainerBuilder $container) $HandshakeMiddlewareRegistryDef = $container->getDefinition('gos_web_socket.handshake_middleware.registry'); if (!empty($configs['server']['handshake_middleware'])) { foreach ($configs['server']['handshake_middleware'] as $middleware) { - $HandshakeMiddlewareRegistryDef->addMethodCall('addMiddleware', new Reference(ltrim($middleware, '@'))); + $HandshakeMiddlewareRegistryDef->addMethodCall('addMiddleware', [new Reference(ltrim($middleware, '@'))]); } } From 1e3af3a6eec8bf7b95d1389a8bb0f80576c7764c Mon Sep 17 00:00:00 2001 From: valentint Date: Mon, 20 Feb 2017 19:31:56 +0300 Subject: [PATCH 11/21] middleware fixes --- Server/App/Registry/HandshakeMiddlewareRegistry.php | 11 ++--------- Server/App/Stack/Factory/Middleware.php | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/Server/App/Registry/HandshakeMiddlewareRegistry.php b/Server/App/Registry/HandshakeMiddlewareRegistry.php index 881922d3..d82900b8 100644 --- a/Server/App/Registry/HandshakeMiddlewareRegistry.php +++ b/Server/App/Registry/HandshakeMiddlewareRegistry.php @@ -20,18 +20,11 @@ public function __construct() } /** - * @param array $middleware + * @param HandshakeMiddlewareInterface $middleware * @throws \Exception */ - public function addMiddleware($middleware) + public function addMiddleware(HandshakeMiddlewareInterface $middleware) { - $interfaces = class_implements($middleware); - - if (!isset($interfaces['Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface'])) { - $className = get_class($middleware); - throw new \Exception('"Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface" in not implemented by "' . $className . '"'); - } - $this->middlewares[] = $middleware; } diff --git a/Server/App/Stack/Factory/Middleware.php b/Server/App/Stack/Factory/Middleware.php index caf990fb..1ab0c710 100644 --- a/Server/App/Stack/Factory/Middleware.php +++ b/Server/App/Stack/Factory/Middleware.php @@ -1,6 +1,6 @@ Date: Mon, 20 Feb 2017 20:13:31 +0300 Subject: [PATCH 12/21] middleware fix --- Server/Type/WebSocketServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/Type/WebSocketServer.php b/Server/Type/WebSocketServer.php index c3ec095f..571e56a1 100644 --- a/Server/Type/WebSocketServer.php +++ b/Server/Type/WebSocketServer.php @@ -164,7 +164,7 @@ public function launch($host, $port, $profile) if (!empty($this->handshakeMiddlewareRegistry->getMiddlewares())) { foreach ($this->handshakeMiddlewareRegistry->getMiddlewares() as $middleware) { - call_user_func([$stack, 'push'], ['Gos\Bundle\WebSocketBundle\Server\App\Stack\Factory\Middleware', $middleware]); + call_user_func([$stack, 'push'], 'Gos\Bundle\WebSocketBundle\Server\App\Stack\Factory\Middleware', $middleware); } } From ffe74e3f32256a4d170c4bab9f0033d0d0a2c0c2 Mon Sep 17 00:00:00 2001 From: valentint Date: Tue, 21 Feb 2017 12:25:45 +0300 Subject: [PATCH 13/21] middleware interface to abstract --- DependencyInjection/Configuration.php | 2 +- Event/ClientEventListener.php | 4 +--- Resources/docs/HandshakeMiddleware.md | 2 +- .../App/Registry/HandshakeMiddlewareRegistry.php | 10 +++++----- Server/App/Stack/Factory/Middleware.php | 8 ++++---- ...erface.php => HandshakeMiddlewareAbstract.php} | 15 +++++++++++++-- 6 files changed, 25 insertions(+), 16 deletions(-) rename Server/App/Stack/{HandshakeMiddlewareInterface.php => HandshakeMiddlewareAbstract.php} (57%) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 869d91e4..a8dd3f54 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -97,7 +97,7 @@ public function getConfigTreeBuilder() ->end() ->arrayNode('handshake_middleware') ->prototype('scalar') - ->example('@some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface') + ->example('@some_service # have to extends Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareAbstract') ->end() ->end() ->end() diff --git a/Event/ClientEventListener.php b/Event/ClientEventListener.php index 4cc4b8b2..0b2b6509 100644 --- a/Event/ClientEventListener.php +++ b/Event/ClientEventListener.php @@ -124,8 +124,6 @@ public function onClientError(ClientErrorEvent $event) */ public function onClientRejected(ClientRejectedEvent $event) { - $this->logger->warning('Client rejected, msg', [ - 'msg' => $event->getMsg(), - ]); + $this->logger->warning('Client rejected, ' . $event->getMsg()); } } diff --git a/Resources/docs/HandshakeMiddleware.md b/Resources/docs/HandshakeMiddleware.md index 936318e8..e4d06d01 100644 --- a/Resources/docs/HandshakeMiddleware.md +++ b/Resources/docs/HandshakeMiddleware.md @@ -10,5 +10,5 @@ You can add any middleware as service to server with your business logic gos_web_socket: server: handshake_middleware: - - @some_service # implements Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface + - @some_service # have to extends Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareAbstract ``` diff --git a/Server/App/Registry/HandshakeMiddlewareRegistry.php b/Server/App/Registry/HandshakeMiddlewareRegistry.php index d82900b8..3c315c1a 100644 --- a/Server/App/Registry/HandshakeMiddlewareRegistry.php +++ b/Server/App/Registry/HandshakeMiddlewareRegistry.php @@ -2,7 +2,7 @@ namespace Gos\Bundle\WebSocketBundle\Server\App\Registry; -use Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface; +use Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareAbstract; /** * @author Tkachew <7tkachew@gmail.com> @@ -10,7 +10,7 @@ class HandshakeMiddlewareRegistry { /** - * @var HandshakeMiddlewareInterface[] + * @var HandshakeMiddlewareAbstract[] */ protected $middlewares; @@ -20,16 +20,16 @@ public function __construct() } /** - * @param HandshakeMiddlewareInterface $middleware + * @param HandshakeMiddlewareAbstract $middleware * @throws \Exception */ - public function addMiddleware(HandshakeMiddlewareInterface $middleware) + public function addMiddleware(HandshakeMiddlewareAbstract $middleware) { $this->middlewares[] = $middleware; } /** - * @return HandshakeMiddlewareInterface[] + * @return HandshakeMiddlewareAbstract[] */ public function getMiddlewares() { diff --git a/Server/App/Stack/Factory/Middleware.php b/Server/App/Stack/Factory/Middleware.php index 1ab0c710..830fc855 100644 --- a/Server/App/Stack/Factory/Middleware.php +++ b/Server/App/Stack/Factory/Middleware.php @@ -4,7 +4,7 @@ use Gos\Bundle\WebSocketBundle\Event\ClientRejectedEvent; use Gos\Bundle\WebSocketBundle\Event\Events; -use Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareInterface; +use Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareAbstract; use Guzzle\Http\Message\RequestInterface; use Ratchet\ConnectionInterface; use Ratchet\Http\HttpServerInterface; @@ -22,17 +22,17 @@ class Middleware implements HttpServerInterface protected $_component; /** - * @var HandshakeMiddlewareInterface + * @var HandshakeMiddlewareAbstract */ protected $_middleware; /** * @param MessageComponentInterface $component - * @param HandshakeMiddlewareInterface $middleware + * @param HandshakeMiddlewareAbstract $middleware */ public function __construct( MessageComponentInterface $component, - HandshakeMiddlewareInterface $middleware + HandshakeMiddlewareAbstract $middleware ) { $this->_component = $component; $this->_middleware = $middleware; diff --git a/Server/App/Stack/HandshakeMiddlewareInterface.php b/Server/App/Stack/HandshakeMiddlewareAbstract.php similarity index 57% rename from Server/App/Stack/HandshakeMiddlewareInterface.php rename to Server/App/Stack/HandshakeMiddlewareAbstract.php index a1defcc5..2cb8d975 100644 --- a/Server/App/Stack/HandshakeMiddlewareInterface.php +++ b/Server/App/Stack/HandshakeMiddlewareAbstract.php @@ -5,12 +5,23 @@ use Guzzle\Http\Message\RequestInterface; use Ratchet\Http\HttpServerInterface; use Ratchet\ConnectionInterface; +use Ratchet\MessageComponentInterface; /** * @author Tkachew <7tkachew@gmail.com> */ -interface HandshakeMiddlewareInterface extends HttpServerInterface +abstract class HandshakeMiddlewareAbstract implements HttpServerInterface { + /** + * @var MessageComponentInterface + */ + protected $_component; + + public function setComponent(MessageComponentInterface $component) + { + $this->_component = $component; + } + /** * @param \Ratchet\ConnectionInterface $conn * @param \Guzzle\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!! @@ -18,5 +29,5 @@ interface HandshakeMiddlewareInterface extends HttpServerInterface * @return ConnectionInterface|void * @throws \UnexpectedValueException if a RequestInterface is not passed */ - public function onOpen(ConnectionInterface $conn, RequestInterface $request = null); + abstract public function onOpen(ConnectionInterface $conn, RequestInterface $request = null); } From 86a91f2a6ad08df399a63c9bfa6c8614efc9791b Mon Sep 17 00:00:00 2001 From: valentint Date: Tue, 21 Feb 2017 14:48:46 +0300 Subject: [PATCH 14/21] middleware update --- Server/App/Stack/Factory/Middleware.php | 17 +++---- .../App/Stack/HandshakeMiddlewareAbstract.php | 45 ++++++++++++++++--- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Server/App/Stack/Factory/Middleware.php b/Server/App/Stack/Factory/Middleware.php index 830fc855..5a602c59 100644 --- a/Server/App/Stack/Factory/Middleware.php +++ b/Server/App/Stack/Factory/Middleware.php @@ -36,6 +36,7 @@ public function __construct( ) { $this->_component = $component; $this->_middleware = $middleware; + $this->_middleware->setComponent($component); } /** @@ -43,9 +44,7 @@ public function __construct( */ public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { - $answer = $this->_middleware->onOpen($conn, $request); - - return $answer instanceof ConnectionInterface ? $answer : $this->_component->onOpen($conn, $request); + return $this->_middleware->onOpen($conn, $request); } /** @@ -53,9 +52,7 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu */ public function onMessage(ConnectionInterface $from, $msg) { - $this->_middleware->onMessage($from, $msg); - - return $this->_component->onMessage($from, $msg); + return $this->_middleware->onMessage($from, $msg); } /** @@ -63,9 +60,7 @@ public function onMessage(ConnectionInterface $from, $msg) */ public function onClose(ConnectionInterface $conn) { - $this->_middleware->onClose($conn); - - return $this->_component->onClose($conn); + return $this->_middleware->onClose($conn); } /** @@ -73,8 +68,6 @@ public function onClose(ConnectionInterface $conn) */ public function onError(ConnectionInterface $conn, \Exception $e) { - $this->_middleware->onError($conn, $e); - - return $this->_component->onError($conn, $e); + return $this->_middleware->onError($conn, $e); } } diff --git a/Server/App/Stack/HandshakeMiddlewareAbstract.php b/Server/App/Stack/HandshakeMiddlewareAbstract.php index 2cb8d975..6021e78c 100644 --- a/Server/App/Stack/HandshakeMiddlewareAbstract.php +++ b/Server/App/Stack/HandshakeMiddlewareAbstract.php @@ -17,17 +17,50 @@ abstract class HandshakeMiddlewareAbstract implements HttpServerInterface */ protected $_component; + /** + * @param MessageComponentInterface $component + */ public function setComponent(MessageComponentInterface $component) { $this->_component = $component; } /** - * @param \Ratchet\ConnectionInterface $conn - * @param \Guzzle\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!! - * - * @return ConnectionInterface|void - * @throws \UnexpectedValueException if a RequestInterface is not passed + * @param ConnectionInterface $conn + * @param RequestInterface|null $request + * @return mixed + */ + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) + { + return $this->_component->onOpen($conn, $request); + } + + /** + * @param ConnectionInterface $conn + * @return mixed */ - abstract public function onOpen(ConnectionInterface $conn, RequestInterface $request = null); + public function onClose(ConnectionInterface $conn) + { + return $this->_component->onClose($conn); + } + + /** + * @param ConnectionInterface $conn + * @param \Exception $e + * @return mixed + */ + public function onError(ConnectionInterface $conn, \Exception $e) + { + return $this->_component->onError($conn, $e); + } + + /** + * @param ConnectionInterface $from + * @param string $msg + * @return mixed + */ + public function onMessage(ConnectionInterface $from, $msg) + { + return $this->_component->onMessage($from, $msg); + } } From 254eb38deeb9669e1d895ca64fbf66e85da75c5b Mon Sep 17 00:00:00 2001 From: valentint Date: Tue, 21 Feb 2017 15:58:40 +0300 Subject: [PATCH 15/21] WebsocketAuthenticationProvider to WebsocketAuthenticationProviderInterface --- Event/ClientEventListener.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Event/ClientEventListener.php b/Event/ClientEventListener.php index 0b2b6509..01c2b228 100644 --- a/Event/ClientEventListener.php +++ b/Event/ClientEventListener.php @@ -3,6 +3,7 @@ namespace Gos\Bundle\WebSocketBundle\Event; use Gos\Bundle\WebSocketBundle\Client\Auth\WebsocketAuthenticationProvider; +use Gos\Bundle\WebSocketBundle\Client\Auth\WebsocketAuthenticationProviderInterface; use Gos\Bundle\WebSocketBundle\Client\ClientStorageInterface; use Gos\Bundle\WebSocketBundle\Client\Exception\ClientNotFoundException; use Psr\Log\LoggerInterface; @@ -31,12 +32,12 @@ class ClientEventListener /** * @param ClientStorageInterface $clientStorage - * @param WebsocketAuthenticationProvider $authenticationProvider + * @param WebsocketAuthenticationProviderInterface $authenticationProvider * @param LoggerInterface|null $logger */ public function __construct( ClientStorageInterface $clientStorage, - WebsocketAuthenticationProvider $authenticationProvider, + WebsocketAuthenticationProviderInterface $authenticationProvider, LoggerInterface $logger = null ) { $this->clientStorage = $clientStorage; From 7f4ce2e3a450e330ad33ea51bb5196089af5cf71 Mon Sep 17 00:00:00 2001 From: valentint Date: Tue, 21 Feb 2017 16:19:57 +0300 Subject: [PATCH 16/21] WebsocketAuthenticationProvider fix --- Client/Auth/WebsocketAuthenticationProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Client/Auth/WebsocketAuthenticationProvider.php b/Client/Auth/WebsocketAuthenticationProvider.php index 880fdbcc..69005eba 100644 --- a/Client/Auth/WebsocketAuthenticationProvider.php +++ b/Client/Auth/WebsocketAuthenticationProvider.php @@ -76,11 +76,11 @@ protected function getToken(ConnectionInterface $connection) } } - if (null === $token) { + if (null === $token && !$this->tokenStorage->getToken()) { $token = new AnonymousToken($this->firewalls[0], 'anon-' . $connection->WAMP->sessionId); } - if ($this->tokenStorage->getToken() !== $token) { + if ($token && $this->tokenStorage->getToken() !== $token) { $this->tokenStorage->setToken($token); } From b2b64ed1d8258159495342f03e8dd519a064f307 Mon Sep 17 00:00:00 2001 From: valentint Date: Tue, 21 Feb 2017 16:30:38 +0300 Subject: [PATCH 17/21] WebsocketAuthenticationProvider fix --- Client/Auth/WebsocketAuthenticationProvider.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Client/Auth/WebsocketAuthenticationProvider.php b/Client/Auth/WebsocketAuthenticationProvider.php index 69005eba..bd3e4e75 100644 --- a/Client/Auth/WebsocketAuthenticationProvider.php +++ b/Client/Auth/WebsocketAuthenticationProvider.php @@ -76,11 +76,15 @@ protected function getToken(ConnectionInterface $connection) } } - if (null === $token && !$this->tokenStorage->getToken()) { - $token = new AnonymousToken($this->firewalls[0], 'anon-' . $connection->WAMP->sessionId); + if (null === $token) { + if (!$this->tokenStorage->getToken()) { + $token = new AnonymousToken($this->firewalls[0], 'anon-' . $connection->WAMP->sessionId); + } else { + $token = $this->tokenStorage->getToken(); + } } - if ($token && $this->tokenStorage->getToken() !== $token) { + if ($this->tokenStorage->getToken() !== $token) { $this->tokenStorage->setToken($token); } From 8d743dcb513f333c6c012f4fa1e4049fa09e4f29 Mon Sep 17 00:00:00 2001 From: valentint Date: Thu, 2 Mar 2017 14:36:45 +0300 Subject: [PATCH 18/21] WebSocketServer constructor update --- Resources/config/services/services.yml | 2 +- Server/Type/WebSocketServer.php | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Resources/config/services/services.yml b/Resources/config/services/services.yml index 57a69866..206e4a65 100644 --- a/Resources/config/services/services.yml +++ b/Resources/config/services/services.yml @@ -51,8 +51,8 @@ services: - '%web_socket_origin_check%' - '@gos_web_socket.wamp.topic_manager' - '@gos_web_socket.server_push_handler.registry' - - '@?monolog.logger.websocket' - '@gos_web_socket.handshake_middleware.registry' + - '@?monolog.logger.websocket' tags: - { name: gos_web_socket.server } diff --git a/Server/Type/WebSocketServer.php b/Server/Type/WebSocketServer.php index 571e56a1..fbe0e4bb 100644 --- a/Server/Type/WebSocketServer.php +++ b/Server/Type/WebSocketServer.php @@ -82,8 +82,9 @@ class WebSocketServer implements ServerInterface * @param OriginRegistry $originRegistry * @param bool $originCheck * @param TopicManager $topicManager - * @param LoggerInterface|null $logger + * @param ServerPushHandlerRegistry $serverPushHandlerRegistry * @param HandshakeMiddlewareRegistry $handshakeMiddlewareRegistry + * @param LoggerInterface|null $logger */ public function __construct( LoopInterface $loop, @@ -94,8 +95,8 @@ public function __construct( $originCheck, TopicManager $topicManager, ServerPushHandlerRegistry $serverPushHandlerRegistry, - LoggerInterface $logger = null, - HandshakeMiddlewareRegistry $handshakeMiddlewareRegistry + HandshakeMiddlewareRegistry $handshakeMiddlewareRegistry, + LoggerInterface $logger = null ) { $this->loop = $loop; $this->eventDispatcher = $eventDispatcher; From c24eabc8967e81f115f394b6feb25e602e33543d Mon Sep 17 00:00:00 2001 From: valentint Date: Thu, 2 Mar 2017 14:46:30 +0300 Subject: [PATCH 19/21] Handshake middleware example for OAuth --- Resources/docs/HandshakeMiddleware.md | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/Resources/docs/HandshakeMiddleware.md b/Resources/docs/HandshakeMiddleware.md index e4d06d01..5b07e24f 100644 --- a/Resources/docs/HandshakeMiddleware.md +++ b/Resources/docs/HandshakeMiddleware.md @@ -12,3 +12,115 @@ gos_web_socket: handshake_middleware: - @some_service # have to extends Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareAbstract ``` + + + +### Handshake middleware example for OAuth + +```php +oAuthService = $oAuthService; + $this->eventDispatcher = $eventDispatcher; + $this->firewalls = $firewalls; + $this->tokenStorage = $tokenStorage; + } + + /** + * @param ConnectionInterface $conn + * @param RequestInterface|null $request + * + * @return void + */ + public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) + { + try { + $accessToken = $this->oAuthService->verifyAccessToken($request->getQuery()->get('access_token')); + } catch (OAuth2AuthenticateException $e) { + $this->eventDispatcher->dispatch( + Events::CLIENT_REJECTED, + new ClientRejectedEvent($e->getMessage(), $request) + ); + + $this->close($conn, 403); + return ; + } + + $user = $accessToken->getUser(); + $token = new AnonymousToken( + $request->getQuery()->get('access_token'), + $user, + $user->getRoles() + ); + $this->tokenStorage->setToken($token); + + return $this->_component->onOpen($conn, $request); + } + + /** + * Close a connection with an HTTP response. + * + * @param \Ratchet\ConnectionInterface $conn + * @param int $code HTTP status code + */ + protected function close(ConnectionInterface $conn, $code = 400) + { + $response = new Response($code, [ + 'X-Powered-By' => \Ratchet\VERSION, + ]); + + $conn->send((string)$response); + $conn->close(); + } +} +``` \ No newline at end of file From 9b195a0b8c29d9e86080e950ff5c86c8ae7d38c0 Mon Sep 17 00:00:00 2001 From: valentint Date: Wed, 22 Mar 2017 11:39:03 +0300 Subject: [PATCH 20/21] config fix - handshake_middleware is not necessary --- DependencyInjection/GosWebSocketExtension.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/GosWebSocketExtension.php b/DependencyInjection/GosWebSocketExtension.php index dcda5167..aa073d24 100644 --- a/DependencyInjection/GosWebSocketExtension.php +++ b/DependencyInjection/GosWebSocketExtension.php @@ -64,8 +64,9 @@ public function load(array $configs, ContainerBuilder $container) } } - $HandshakeMiddlewareRegistryDef = $container->getDefinition('gos_web_socket.handshake_middleware.registry'); if (!empty($configs['server']['handshake_middleware'])) { + $HandshakeMiddlewareRegistryDef = $container->getDefinition('gos_web_socket.handshake_middleware.registry'); + foreach ($configs['server']['handshake_middleware'] as $middleware) { $HandshakeMiddlewareRegistryDef->addMethodCall('addMiddleware', [new Reference(ltrim($middleware, '@'))]); } From 36ddf3a4bb7e3d9d710c0ea404fa839e54711402 Mon Sep 17 00:00:00 2001 From: valentint Date: Wed, 22 Mar 2017 13:05:30 +0300 Subject: [PATCH 21/21] annotation fix --- Server/App/Registry/HandshakeMiddlewareRegistry.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Server/App/Registry/HandshakeMiddlewareRegistry.php b/Server/App/Registry/HandshakeMiddlewareRegistry.php index 3c315c1a..a040e4e4 100644 --- a/Server/App/Registry/HandshakeMiddlewareRegistry.php +++ b/Server/App/Registry/HandshakeMiddlewareRegistry.php @@ -21,7 +21,6 @@ public function __construct() /** * @param HandshakeMiddlewareAbstract $middleware - * @throws \Exception */ public function addMiddleware(HandshakeMiddlewareAbstract $middleware) {