This commit is contained in:
freek 2018-11-27 00:13:22 +01:00
parent ee0ab6b463
commit 6895961c74
7 changed files with 38 additions and 33 deletions

View File

@ -5,7 +5,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels;
use BeyondCode\LaravelWebSockets\Events\ChannelOccupied;
use BeyondCode\LaravelWebSockets\Events\ChannelVacated;
use BeyondCode\LaravelWebSockets\Events\SubscribedToChannel;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignature;
use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface;
use stdClass;
@ -39,7 +39,7 @@ class Channel
}
if (str_after($auth, ':') !== hash_hmac('sha256', $signature, $connection->client->appSecret)) {
throw new InvalidSignatureException();
throw new InvalidSignature();
}
}

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions;
class InvalidConnectionException extends PusherException
class InvalidConnection extends PusherException
{
public function __construct()
{

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions;
class InvalidSignatureException extends PusherException
class InvalidSignature extends PusherException
{
public function __construct()
{

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions;
class UnknownAppKeyException extends PusherException
class UnknownAppKey extends PusherException
{
public function __construct(string $appKey)
{

View File

@ -4,7 +4,6 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
use BeyondCode\LaravelWebSockets\Events\ClientMessageSent;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
use Ratchet\ConnectionInterface;
use stdClass;
@ -30,7 +29,10 @@ class Message implements RespondableMessage
public function respond()
{
if (starts_with($this->payload->event, 'client-')) {
if (!starts_with($this->payload->event, 'client-')) {
return;
}
event(new ClientMessageSent($this->connection, $this->payload));
$channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel);
@ -38,4 +40,3 @@ class Message implements RespondableMessage
optional($channel)->broadcast($this->payload);
}
}
}

View File

@ -3,7 +3,6 @@
namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket;
use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard;
use BeyondCode\LaravelWebSockets\QueryParameters;
use Exception;
use Ratchet\ConnectionInterface;
@ -12,7 +11,7 @@ use BeyondCode\LaravelWebSockets\WebSocketController;
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\PusherException;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKey;
class PusherServer extends WebSocketController
{
@ -26,11 +25,10 @@ class PusherServer extends WebSocketController
function onOpen(ConnectionInterface $connection)
{
$this->generateSocketId($connection);
$this->verifyConnection($connection);
$this->establishConnection($connection);
$this
->generateSocketId($connection)
->verifyConnection($connection)
->establishConnection($connection);
}
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
@ -45,7 +43,7 @@ class PusherServer extends WebSocketController
$this->channelManager->removeFromAllChannels($connection);
}
function onError(ConnectionInterface $connection, Exception $exception)
public function onError(ConnectionInterface $connection, Exception $exception)
{
if ($exception instanceof PusherException) {
$connection->send(json_encode(
@ -54,15 +52,26 @@ class PusherServer extends WebSocketController
}
}
protected function generateSocketId(ConnectionInterface $connection)
{
$socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000));
$connection->socketId = $socketId;
return $this;
}
protected function verifyConnection(ConnectionInterface $connection)
{
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
if (!$client = Client::findByAppKey($appKey)) {
throw new UnknownAppKeyException($appKey);
throw new UnknownAppKey($appKey);
}
$connection->client = $client;
return $this;
}
protected function establishConnection(ConnectionInterface $connection)
@ -76,12 +85,7 @@ class PusherServer extends WebSocketController
]));
event(new ConnectionEstablished($connection));
}
protected function generateSocketId(ConnectionInterface $connection)
{
$socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000));
$connection->socketId = $socketId;
return $this;
}
}

View File

@ -3,8 +3,8 @@
namespace BeyondCode\LaravelWebSockets\Tests;
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignature;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKey;
use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\PusherServer;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
@ -23,7 +23,7 @@ class ConnectionTest extends TestCase
/** @test */
public function unknown_app_keys_can_not_connect()
{
$this->expectException(UnknownAppKeyException::class);
$this->expectException(UnknownAppKey::class);
$this->pusherServer->onOpen($this->getWebSocketConnection('/?appKey=test'));
}
@ -90,7 +90,7 @@ class ConnectionTest extends TestCase
/** @test */
public function clients_need_valid_auth_signatures_for_private_channels()
{
$this->expectException(InvalidSignatureException::class);
$this->expectException(InvalidSignature::class);
$connection = $this->getWebSocketConnection();
@ -134,7 +134,7 @@ class ConnectionTest extends TestCase
/** @test */
public function clients_need_valid_auth_signatures_for_presence_channels()
{
$this->expectException(InvalidSignatureException::class);
$this->expectException(InvalidSignature::class);
$connection = $this->getWebSocketConnection();