Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handshake middleware example for OAuth
  • Loading branch information
valentint committed Mar 2, 2017
commit c24eabc8967e81f115f394b6feb25e602e33543d
112 changes: 112 additions & 0 deletions Resources/docs/HandshakeMiddleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

namespace WebSocketBundle\Service\Middleware;

use Gos\Bundle\WebSocketBundle\Event\ClientRejectedEvent;
use Gos\Bundle\WebSocketBundle\Event\Events;
use Gos\Bundle\WebSocketBundle\Server\App\Stack\HandshakeMiddlewareAbstract;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use OAuth2\OAuth2;
use OAuth2\OAuth2AuthenticateException;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;


class OAuthMiddleware extends HandshakeMiddlewareAbstract
{
/**
* @var OAuth2
*/
protected $oAuthService;

/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

/**
* @var array
*/
protected $firewalls;

/**
* @var SecurityContextInterface|TokenStorageInterface
*/
protected $tokenStorage;

/**
* OAuthMiddleware constructor.
* @param EventDispatcherInterface $eventDispatcher
* @param OAuth2 $oAuthService
* @param array $firewalls
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
OAuth2 $oAuthService,
$firewalls = array(),
$tokenStorage
)
{
$this->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'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the request object is null? and what if the access token is given in header?

$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();
}
}
```