This commit is contained in:
Marcel Pociot 2018-12-04 10:15:37 +01:00
parent e993457a80
commit 6d743f7924
6 changed files with 86 additions and 1 deletions

View File

@ -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 === '') {

View File

@ -10,4 +10,6 @@ interface AppProvider
public function findById($appId): ?App;
public function findByKey(string $appKey): ?App;
public function findBySecret(string $appSecret): ?App;
}

View File

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

View File

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

View File

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

View File

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