This commit is contained in:
freek 2018-11-24 01:53:20 +01:00
parent 6a45296154
commit 60444e1097
5 changed files with 47 additions and 16 deletions

View File

@ -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
}
}

View File

@ -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;
}

View File

@ -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']
);
}
}

View File

@ -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);

View File

@ -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']);
}