1
0
Fork 0
mirror of synced 2024-06-27 02:31:04 +12:00

fix: realtime authentication on console

This commit is contained in:
Torsten Dittmann 2022-07-06 12:53:30 +02:00
parent bafc8f829a
commit 806a67b1b0
2 changed files with 111 additions and 2 deletions

View file

@ -491,8 +491,12 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re
$database = new Database(new MariaDB($db), $cache);
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace("_console");
$project = Authorization::skip(fn() => $database->getDocument('projects', $realtime->connections[$connection]['projectId']));
$database->setNamespace("_{$project->getInternalId()}");
$projectId = $realtime->connections[$connection]['projectId'];
if ($projectId !== 'console') {
$project = Authorization::skip(fn() => $database->getDocument('projects', $projectId));
$database->setNamespace("_{$project->getInternalId()}");
}
/*
* Abuse Check

View file

@ -13,6 +13,111 @@ class RealtimeConsoleClientTest extends Scope
use ProjectCustom;
use SideConsole;
public function testManualAuthentication()
{
$user = $this->getUser();
$userId = $user['$id'] ?? '';
$session = $user['session'] ?? '';
/**
* Test for SUCCESS
*/
$client = $this->getWebsocket(['account'], [
'origin' => 'http://localhost'
]);
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('connected', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertCount(1, $response['data']['channels']);
$this->assertContains('account', $response['data']['channels']);
$client->send(\json_encode([
'type' => 'authentication',
'data' => [
'session' => $session
]
]));
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('response', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertEquals('authentication', $response['data']['to']);
$this->assertTrue($response['data']['success']);
$this->assertNotEmpty($response['data']['user']);
$this->assertEquals($userId, $response['data']['user']['$id']);
/**
* Test for FAILURE
*/
$client->send(\json_encode([
'type' => 'authentication',
'data' => [
'session' => 'invalid_session'
]
]));
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('error', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertEquals(1003, $response['data']['code']);
$this->assertEquals('Session is not valid.', $response['data']['message']);
$client->send(\json_encode([
'type' => 'authentication',
'data' => []
]));
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('error', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertEquals(1003, $response['data']['code']);
$this->assertEquals('Payload is not valid.', $response['data']['message']);
$client->send(\json_encode([
'type' => 'unknown',
'data' => [
'session' => 'invalid_session'
]
]));
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('error', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertEquals(1003, $response['data']['code']);
$this->assertEquals('Message type is not valid.', $response['data']['message']);
$client->send(\json_encode([
'test' => '123',
]));
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('error', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertEquals(1003, $response['data']['code']);
$this->assertEquals('Message format is not valid.', $response['data']['message']);
$client->close();
}
public function testAttributes()
{
$user = $this->getUser();