From 6d743f79240246e726abd4704c3b1895b22e2bc0 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Tue, 4 Dec 2018 10:15:37 +0100 Subject: [PATCH] wip --- src/Apps/App.php | 5 ++ src/Apps/AppProvider.php | 2 + src/Apps/ConfigAppProvider.php | 9 ++++ src/Statistics/Http/Middleware/Authorize.php | 13 +++++ .../Logger/HttpStatisticsLogger.php | 7 ++- .../ClientProviders/ConfigAppProviderTest.php | 51 +++++++++++++++++++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/Statistics/Http/Middleware/Authorize.php diff --git a/src/Apps/App.php b/src/Apps/App.php index ac484e6..92922ab 100644 --- a/src/Apps/App.php +++ b/src/Apps/App.php @@ -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 === '') { diff --git a/src/Apps/AppProvider.php b/src/Apps/AppProvider.php index 04ee2b6..d022d95 100644 --- a/src/Apps/AppProvider.php +++ b/src/Apps/AppProvider.php @@ -10,4 +10,6 @@ interface AppProvider public function findById($appId): ?App; public function findByKey(string $appKey): ?App; + + public function findBySecret(string $appSecret): ?App; } \ No newline at end of file diff --git a/src/Apps/ConfigAppProvider.php b/src/Apps/ConfigAppProvider.php index 78461f0..8e5297d 100644 --- a/src/Apps/ConfigAppProvider.php +++ b/src/Apps/ConfigAppProvider.php @@ -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) { diff --git a/src/Statistics/Http/Middleware/Authorize.php b/src/Statistics/Http/Middleware/Authorize.php new file mode 100644 index 0000000..32e19a6 --- /dev/null +++ b/src/Statistics/Http/Middleware/Authorize.php @@ -0,0 +1,13 @@ +secret)) ? abort(403) : $next($request); + } +} \ No newline at end of file diff --git a/src/Statistics/Logger/HttpStatisticsLogger.php b/src/Statistics/Logger/HttpStatisticsLogger.php index 4985ed9..7543d32 100644 --- a/src/Statistics/Logger/HttpStatisticsLogger.php +++ b/src/Statistics/Logger/HttpStatisticsLogger.php @@ -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); diff --git a/tests/ClientProviders/ConfigAppProviderTest.php b/tests/ClientProviders/ConfigAppProviderTest.php index 4fedcdc..421a73b 100644 --- a/tests/ClientProviders/ConfigAppProviderTest.php +++ b/tests/ClientProviders/ConfigAppProviderTest.php @@ -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); + } } \ No newline at end of file