Refactor header validation and add GetSessionState scaffold.

This commit is contained in:
netkas 2024-12-12 05:12:28 -05:00
parent 701acfde35
commit f0624e71d1
3 changed files with 49 additions and 18 deletions

View file

@ -0,0 +1,20 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class GetSessionState extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
// TODO: Implement execute() method.
}
}

View file

@ -19,8 +19,8 @@
private RequestType $requestType; private RequestType $requestType;
private ?string $requestBody; private ?string $requestBody;
private string $clientName; private ?string $clientName;
private string $clientVersion; private ?string $clientVersion;
private ?string $identifyAs; private ?string $identifyAs;
private ?string $sessionUuid; private ?string $sessionUuid;
private ?string $signature; private ?string $signature;
@ -30,8 +30,8 @@
$this->headers = $headers; $this->headers = $headers;
$this->requestBody = $requestBody; $this->requestBody = $requestBody;
$this->clientName = $headers[StandardHeaders::CLIENT_NAME->value]; $this->clientName = $headers[StandardHeaders::CLIENT_NAME->value] ?? null;
$this->clientVersion = $headers[StandardHeaders::CLIENT_VERSION->value]; $this->clientVersion = $headers[StandardHeaders::CLIENT_VERSION->value] ?? null;
$this->requestType = RequestType::from($headers[StandardHeaders::REQUEST_TYPE->value]); $this->requestType = RequestType::from($headers[StandardHeaders::REQUEST_TYPE->value]);
$this->identifyAs = $headers[StandardHeaders::IDENTIFY_AS->value] ?? null; $this->identifyAs = $headers[StandardHeaders::IDENTIFY_AS->value] ?? null;
$this->sessionUuid = $headers[StandardHeaders::SESSION_UUID->value] ?? null; $this->sessionUuid = $headers[StandardHeaders::SESSION_UUID->value] ?? null;

View file

@ -43,20 +43,6 @@
return; return;
} }
if(!isset($requestHeaders[StandardHeaders::CLIENT_NAME->value]))
{
http_response_code(400);
print('Missing required header: ' . StandardHeaders::CLIENT_NAME->value);
return;
}
if(!isset($requestHeaders[StandardHeaders::CLIENT_VERSION->value]))
{
http_response_code(400);
print('Missing required header: ' . StandardHeaders::CLIENT_VERSION->value);
return;
}
$clientRequest = new ClientRequest($requestHeaders, file_get_contents('php://input') ?? null); $clientRequest = new ClientRequest($requestHeaders, file_get_contents('php://input') ?? null);
// Handle the request type, only `init` and `dhe` are not encrypted using the session's encrypted key // Handle the request type, only `init` and `dhe` are not encrypted using the session's encrypted key
@ -94,6 +80,21 @@
*/ */
private static function handleInitiateSession(ClientRequest $clientRequest): void private static function handleInitiateSession(ClientRequest $clientRequest): void
{ {
if(!isset($requestHeaders[StandardHeaders::CLIENT_NAME->value]))
{
http_response_code(400);
print('Missing required header: ' . StandardHeaders::CLIENT_NAME->value);
return;
}
if(!isset($requestHeaders[StandardHeaders::CLIENT_VERSION->value]))
{
http_response_code(400);
print('Missing required header: ' . StandardHeaders::CLIENT_VERSION->value);
return;
}
if(!$clientRequest->headerExists(StandardHeaders::PUBLIC_KEY)) if(!$clientRequest->headerExists(StandardHeaders::PUBLIC_KEY))
{ {
http_response_code(400); http_response_code(400);
@ -148,6 +149,7 @@
} }
// Create the session UUID // Create the session UUID
// TODO: Save client name and version to the database
$sessionUuid = SessionManager::createSession($clientRequest->getHeader(StandardHeaders::PUBLIC_KEY), $registeredPeer); $sessionUuid = SessionManager::createSession($clientRequest->getHeader(StandardHeaders::PUBLIC_KEY), $registeredPeer);
http_response_code(201); // Created http_response_code(201); // Created
print($sessionUuid); // Return the session UUID print($sessionUuid); // Return the session UUID
@ -265,6 +267,15 @@
*/ */
private static function handleRpc(ClientRequest $clientRequest): void private static function handleRpc(ClientRequest $clientRequest): void
{ {
if(!$clientRequest->headerExists(StandardHeaders::SESSION_UUID))
{
Logger::getLogger()->verbose('Missing required header: ' . StandardHeaders::SESSION_UUID->value);
http_response_code(412);
print('Missing required header: ' . StandardHeaders::SESSION_UUID->value);
return;
}
try try
{ {
$clientRequests = $clientRequest->getRpcRequests(); $clientRequests = $clientRequest->getRpcRequests();