commit
This commit is contained in:
parent
6a45296154
commit
60444e1097
|
|
@ -16,9 +16,14 @@ class Client
|
||||||
/** @var string */
|
/** @var string */
|
||||||
public $appSecret;
|
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)
|
public function __construct($appId, string $appKey, string $appSecret)
|
||||||
|
|
@ -43,4 +48,6 @@ class Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -5,5 +5,7 @@ namespace BeyondCode\LaravelWebSockets\ClientProviders;
|
||||||
|
|
||||||
interface ClientProvider
|
interface ClientProvider
|
||||||
{
|
{
|
||||||
public function findClient(string $appId): ?Client;
|
public function findByAppId(int $appId): ?Client;
|
||||||
|
|
||||||
|
public function findByAppKey(string $appKey): ?Client;
|
||||||
}
|
}
|
||||||
|
|
@ -2,22 +2,43 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\ClientProviders;
|
namespace BeyondCode\LaravelWebSockets\ClientProviders;
|
||||||
|
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
class ConfigClientProvider implements ClientProvider
|
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 null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Client(
|
return new Client(
|
||||||
$client['app_id'],
|
$clientAttributes['app_id'],
|
||||||
$client['app_key'],
|
$clientAttributes['app_key'],
|
||||||
$client['app_secret']
|
$clientAttributes['app_secret']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
|
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
|
||||||
|
|
||||||
|
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use GuzzleHttp\Psr7 as gPsr;
|
use GuzzleHttp\Psr7 as gPsr;
|
||||||
|
|
@ -60,8 +61,7 @@ abstract class EchoController implements HttpServerInterface
|
||||||
|
|
||||||
function onError(ConnectionInterface $connection, Exception $exception)
|
function onError(ConnectionInterface $connection, Exception $exception)
|
||||||
{
|
{
|
||||||
if ($exception instanceof HttpException)
|
if ($exception instanceof HttpException) {
|
||||||
{
|
|
||||||
$response = new Response($exception->getStatusCode(), [
|
$response = new Response($exception->getStatusCode(), [
|
||||||
'Content-Type' => 'application/json'
|
'Content-Type' => 'application/json'
|
||||||
], json_encode([
|
], json_encode([
|
||||||
|
|
@ -75,10 +75,11 @@ abstract class EchoController implements HttpServerInterface
|
||||||
|
|
||||||
public function verifyAppId(string $appId)
|
public function verifyAppId(string $appId)
|
||||||
{
|
{
|
||||||
/** TODO: use client config from config file */
|
if ($client = Client::findByAppId($appId)) {
|
||||||
if ($appId !== config('broadcasting.connections.pusher.app_id')) {
|
return;
|
||||||
throw new HttpException(401, 'Invalid App ID provided.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new HttpException(401, "Unknown app id `{$appId}` provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public function __invoke(Request $request);
|
abstract public function __invoke(Request $request);
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ class PusherServer extends WebSocketController
|
||||||
$queryParameters = [];
|
$queryParameters = [];
|
||||||
parse_str($request->getUri()->getQuery(), $queryParameters);
|
parse_str($request->getUri()->getQuery(), $queryParameters);
|
||||||
|
|
||||||
if (! $client = Client::find($queryParameters['appKey'])) {
|
if (! $client = Client::findByAppKey($queryParameters['appKey'])) {
|
||||||
throw new UnknownAppKey($queryParameters['appKey']);
|
throw new UnknownAppKey($queryParameters['appKey']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue