wip
This commit is contained in:
parent
e993457a80
commit
6d743f7924
|
|
@ -34,6 +34,11 @@ class App
|
|||
return app(AppProvider::class)->findByKey($appKey);
|
||||
}
|
||||
|
||||
public static function findBySecret(string $appSecret): ?App
|
||||
{
|
||||
return app(AppProvider::class)->findByKey($appSecret);
|
||||
}
|
||||
|
||||
public function __construct($appId, string $appKey, string $appSecret)
|
||||
{
|
||||
if ($appKey === '') {
|
||||
|
|
|
|||
|
|
@ -10,4 +10,6 @@ interface AppProvider
|
|||
public function findById($appId): ?App;
|
||||
|
||||
public function findByKey(string $appKey): ?App;
|
||||
|
||||
public function findBySecret(string $appSecret): ?App;
|
||||
}
|
||||
|
|
@ -42,6 +42,15 @@ class ConfigAppProvider implements AppProvider
|
|||
return $this->instanciate($appAttributes);
|
||||
}
|
||||
|
||||
public function findBySecret(string $appSecret): ?App
|
||||
{
|
||||
$appAttributes = $this
|
||||
->apps
|
||||
->firstWhere('secret', $appSecret);
|
||||
|
||||
return $this->instanciate($appAttributes);
|
||||
}
|
||||
|
||||
protected function instanciate(?array $appAttributes): ?App
|
||||
{
|
||||
if (!$appAttributes) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace BeyondCode\LaravelWebSockets\Statistics\Http\Middleware;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Apps\App;
|
||||
|
||||
class Authorize
|
||||
{
|
||||
public function handle($request, $next)
|
||||
{
|
||||
return is_null(App::findBySecret($request->secret)) ? abort(403) : $next($request);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Apps\App;
|
||||
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
|
||||
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
|
|
@ -72,12 +73,16 @@ class HttpStatisticsLogger implements StatisticsLogger
|
|||
continue;
|
||||
}
|
||||
|
||||
$postData = array_merge($statistic->toArray(), [
|
||||
'secret' => App::findById($appId)->secret
|
||||
]);
|
||||
|
||||
$this
|
||||
->browser
|
||||
->post(
|
||||
action([WebSocketStatisticsEntriesController::class, 'store']),
|
||||
['Content-Type' => 'application/json'],
|
||||
stream_for(json_encode($statistic->toArray()))
|
||||
stream_for(json_encode($postData))
|
||||
);
|
||||
|
||||
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
|
||||
|
|
|
|||
|
|
@ -34,4 +34,55 @@ class ConfigAppProviderTest extends TestCase
|
|||
$this->assertFalse($app->clientMessagesEnabled);
|
||||
$this->assertTrue($app->statisticsEnabled);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_find_app_by_id()
|
||||
{
|
||||
$app = $this->configAppProvider->findById(0000);
|
||||
|
||||
$this->assertNull($app);
|
||||
|
||||
$app = $this->configAppProvider->findById(1234);
|
||||
|
||||
$this->assertEquals('Test App', $app->name);
|
||||
$this->assertEquals(1234, $app->id);
|
||||
$this->assertEquals('TestKey', $app->key);
|
||||
$this->assertEquals('TestSecret', $app->secret);
|
||||
$this->assertFalse($app->clientMessagesEnabled);
|
||||
$this->assertTrue($app->statisticsEnabled);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_find_app_by_key()
|
||||
{
|
||||
$app = $this->configAppProvider->findByKey('InvalidKey');
|
||||
|
||||
$this->assertNull($app);
|
||||
|
||||
$app = $this->configAppProvider->findByKey('TestKey');
|
||||
|
||||
$this->assertEquals('Test App', $app->name);
|
||||
$this->assertEquals(1234, $app->id);
|
||||
$this->assertEquals('TestKey', $app->key);
|
||||
$this->assertEquals('TestSecret', $app->secret);
|
||||
$this->assertFalse($app->clientMessagesEnabled);
|
||||
$this->assertTrue($app->statisticsEnabled);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_can_find_app_by_secret()
|
||||
{
|
||||
$app = $this->configAppProvider->findBySecret('InvalidSecret');
|
||||
|
||||
$this->assertNull($app);
|
||||
|
||||
$app = $this->configAppProvider->findBySecret('TestSecret');
|
||||
|
||||
$this->assertEquals('Test App', $app->name);
|
||||
$this->assertEquals(1234, $app->id);
|
||||
$this->assertEquals('TestKey', $app->key);
|
||||
$this->assertEquals('TestSecret', $app->secret);
|
||||
$this->assertFalse($app->clientMessagesEnabled);
|
||||
$this->assertTrue($app->statisticsEnabled);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue