diff --git a/src/ClientProviders/Client.php b/src/ClientProviders/Client.php index 3834e33..cd3f3c7 100644 --- a/src/ClientProviders/Client.php +++ b/src/ClientProviders/Client.php @@ -16,9 +16,14 @@ class Client /** @var string */ public $appSecret; - public static function find(string $appKey): ?Client + public static function findByAppId(int $appId) { - return app(ClientProvider::class)->findClient($appKey); + return app(ClientProvider::class)->findByAppId($appId); + } + + public static function findByAppKey(string $appKey): ?Client + { + return app(ClientProvider::class)->findByAppKey($appKey); } public function __construct($appId, string $appKey, string $appSecret) @@ -43,4 +48,6 @@ class Client } + + } \ No newline at end of file diff --git a/src/ClientProviders/ClientProvider.php b/src/ClientProviders/ClientProvider.php index cd77b03..f363f53 100644 --- a/src/ClientProviders/ClientProvider.php +++ b/src/ClientProviders/ClientProvider.php @@ -5,5 +5,7 @@ namespace BeyondCode\LaravelWebSockets\ClientProviders; interface ClientProvider { - public function findClient(string $appId): ?Client; + public function findByAppId(int $appId): ?Client; + + public function findByAppKey(string $appKey): ?Client; } \ No newline at end of file diff --git a/src/ClientProviders/ConfigClientProvider.php b/src/ClientProviders/ConfigClientProvider.php index 89f5ddd..6cf63d1 100644 --- a/src/ClientProviders/ConfigClientProvider.php +++ b/src/ClientProviders/ConfigClientProvider.php @@ -2,22 +2,43 @@ namespace BeyondCode\LaravelWebSockets\ClientProviders; +use Illuminate\Support\Collection; + class ConfigClientProvider implements ClientProvider { - public function findClient(string $appKey): ?Client + public function findByAppId(int $appId): ?Client { - $allClients = collect(config('websockets.clients')); + $clientAttributes = $this + ->allClients() + ->firstWhere('app_id', $appId); - $client = $allClients->firstWhere('app_key', $appKey); + return $this->instanciate($clientAttributes); + } - if (! $client) { + public function findByAppKey(string $appKey): ?Client + { + $clientAttributes = $this + ->allClients() + ->firstWhere('app_key', $appKey); + + return $this->instanciate($clientAttributes); + } + + protected function allClients(): Collection + { + return collect(config('websockets.clients')); + } + + protected function instanciate(?array $clientAttributes): ?Client + { + if (! $clientAttributes) { return null; } return new Client( - $client['app_id'], - $client['app_key'], - $client['app_secret'] + $clientAttributes['app_id'], + $clientAttributes['app_key'], + $clientAttributes['app_secret'] ); } } \ No newline at end of file diff --git a/src/LaravelEcho/Http/Controllers/EchoController.php b/src/LaravelEcho/Http/Controllers/EchoController.php index 9199800..79d9e39 100644 --- a/src/LaravelEcho/Http/Controllers/EchoController.php +++ b/src/LaravelEcho/Http/Controllers/EchoController.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers; +use BeyondCode\LaravelWebSockets\ClientProviders\Client; use Exception; use Illuminate\Http\Request; use GuzzleHttp\Psr7 as gPsr; @@ -60,8 +61,7 @@ abstract class EchoController implements HttpServerInterface function onError(ConnectionInterface $connection, Exception $exception) { - if ($exception instanceof HttpException) - { + if ($exception instanceof HttpException) { $response = new Response($exception->getStatusCode(), [ 'Content-Type' => 'application/json' ], json_encode([ @@ -75,10 +75,11 @@ abstract class EchoController implements HttpServerInterface public function verifyAppId(string $appId) { - /** TODO: use client config from config file */ - if ($appId !== config('broadcasting.connections.pusher.app_id')) { - throw new HttpException(401, 'Invalid App ID provided.'); + if ($client = Client::findByAppId($appId)) { + return; } + + throw new HttpException(401, "Unknown app id `{$appId}` provided."); } abstract public function __invoke(Request $request); diff --git a/src/LaravelEcho/WebSocket/PusherServer.php b/src/LaravelEcho/WebSocket/PusherServer.php index b1cf513..79eabcc 100644 --- a/src/LaravelEcho/WebSocket/PusherServer.php +++ b/src/LaravelEcho/WebSocket/PusherServer.php @@ -60,7 +60,7 @@ class PusherServer extends WebSocketController $queryParameters = []; parse_str($request->getUri()->getQuery(), $queryParameters); - if (! $client = Client::find($queryParameters['appKey'])) { + if (! $client = Client::findByAppKey($queryParameters['appKey'])) { throw new UnknownAppKey($queryParameters['appKey']); }