Add client metadata to session creation and enforce TOS/PP

This commit is contained in:
netkas 2024-12-12 14:55:44 -05:00
parent d2119df824
commit 756297671f
10 changed files with 414 additions and 206 deletions

View file

@ -6,11 +6,14 @@
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\SessionState;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
class SessionRecord implements SerializableInterface
{
private string $uuid;
private ?string $peerUuid;
private string $clientName;
private string $clientVersion;
private bool $authenticated;
private string $publicKey;
private SessionState $state;
@ -36,6 +39,8 @@
{
$this->uuid = $data['uuid'];
$this->peerUuid = $data['peer_uuid'] ?? null;
$this->clientName = $data['client_name'];
$this->clientVersion = $data['client_version'];
$this->authenticated = $data['authenticated'] ?? false;
$this->publicKey = $data['public_key'];
$this->created = $data['created'];
@ -149,6 +154,38 @@
return $this->lastRequest;
}
/**
* Retrieves the client name.
*
* @return string Returns the client name.
*/
public function getClientName(): string
{
return $this->clientName;
}
/**
* Retrieves the client version.
*
* @return string Returns the client version.
*/
public function getClientVersion(): string
{
return $this->clientVersion;
}
public function toStandardSessionState(): \Socialbox\Objects\Standard\SessionState
{
return new \Socialbox\Objects\Standard\SessionState([
'uuid' => $this->uuid,
'identified_as' => RegisteredPeerManager::getPeer($this->peerUuid)->getAddress(),
'authenticated' => $this->authenticated,
'flags' => $this->flags,
'created' => $this->created
]);
}
/**
* Creates a new instance of the class using the provided array data.
*

View file

@ -1,193 +1,193 @@
<?php
namespace Socialbox\Objects;
namespace Socialbox\Objects;
use InvalidArgumentException;
use ncc\ThirdParty\nikic\PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Socialbox\Classes\Logger;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use InvalidArgumentException;
use ncc\ThirdParty\nikic\PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Socialbox\Classes\Logger;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
class RpcRequest implements SerializableInterface
{
private ?string $id;
private string $method;
private ?array $parameters;
/**
* Constructs the object from an array of data.
*
* @param string $method The method of the request.
* @param string|null $id The ID of the request.
* @param array|null $parameters The parameters of the request.
*/
public function __construct(string $method, ?string $id, ?array $parameters)
class RpcRequest implements SerializableInterface
{
$this->method = $method;
$this->parameters = $parameters;
$this->id = $id;
}
private ?string $id;
private string $method;
private ?array $parameters;
/**
* Returns the ID of the request.
*
* @return string|null The ID of the request.
*/
public function getId(): ?string
{
return $this->id;
}
/**
* Returns the method of the request.
*
* @return string The method of the request.
*/
public function getMethod(): string
{
return $this->method;
}
/**
* Returns the parameters of the request.
*
* @return array|null The parameters of the request, null if the request is a notification.
*/
public function getParameters(): ?array
{
return $this->parameters;
}
/**
* Checks if the parameter exists within the RPC request
*
* @param string $parameter The parameter to check
* @return bool True if the parameter exists, False otherwise.
*/
public function containsParameter(string $parameter): bool
{
return isset($this->parameters[$parameter]);
}
/**
* Returns the parameter value from the RPC request
*
* @param string $parameter The parameter name to get
* @return mixed The parameter value, null if the parameter value is null or not found.
*/
public function getParameter(string $parameter): mixed
{
if(!$this->containsParameter($parameter))
/**
* Constructs the object from an array of data.
*
* @param string $method The method of the request.
* @param string|null $id The ID of the request.
* @param array|null $parameters The parameters of the request.
*/
public function __construct(string $method, ?string $id, ?array $parameters)
{
return null;
$this->method = $method;
$this->parameters = $parameters;
$this->id = $id;
}
return $this->parameters[$parameter];
}
/**
* Produces a response based off the request, null if the request is a notification
*
* @param mixed|null $result
* @return RpcResponse|null
*/
public function produceResponse(mixed $result=null): ?RpcResponse
{
if($this->id == null)
/**
* Returns the ID of the request.
*
* @return string|null The ID of the request.
*/
public function getId(): ?string
{
return null;
return $this->id;
}
$valid = false;
if(is_array($result))
/**
* Returns the method of the request.
*
* @return string The method of the request.
*/
public function getMethod(): string
{
$valid = true;
}
elseif($result instanceof SerializableInterface)
{
$valid = true;
}
elseif(is_string($result))
{
$valid = true;
}
elseif(is_bool($result))
{
$valid = true;
}
elseif(is_int($result))
{
$valid = true;
}
elseif(is_null($result))
{
$valid = true;
return $this->method;
}
if(!$valid)
/**
* Returns the parameters of the request.
*
* @return array|null The parameters of the request, null if the request is a notification.
*/
public function getParameters(): ?array
{
throw new InvalidArgumentException('The \'$result\' property must either be string, boolean, integer, array, null or SerializableInterface');
return $this->parameters;
}
Logger::getLogger()->verbose(sprintf('Producing response for request %s', $this->id));
return new RpcResponse($this->id, $result);
}
/**
* Produces an error response based off the request, null if the request is a notification
*
* @param StandardError $error
* @param string|null $message
* @return RpcError|null
*/
public function produceError(StandardError $error, ?string $message=null): ?RpcError
{
if($this->id == null)
/**
* Checks if the parameter exists within the RPC request
*
* @param string $parameter The parameter to check
* @return bool True if the parameter exists, False otherwise.
*/
public function containsParameter(string $parameter): bool
{
return null;
return isset($this->parameters[$parameter]);
}
if($message == null)
/**
* Returns the parameter value from the RPC request
*
* @param string $parameter The parameter name to get
* @return mixed The parameter value, null if the parameter value is null or not found.
*/
public function getParameter(string $parameter): mixed
{
$message = $error->getMessage();
if(!$this->containsParameter($parameter))
{
return null;
}
return $this->parameters[$parameter];
}
return new RpcError($this->id, $error, $message);
}
/**
* Produces a response based off the request, null if the request is a notification
*
* @param mixed|null $result
* @return RpcResponse|null
*/
public function produceResponse(mixed $result=null): ?RpcResponse
{
if($this->id == null)
{
return null;
}
/**
* @param StandardException $e
* @return RpcError|null
*/
public function handleStandardException(StandardException $e): ?RpcError
{
return $this->produceError($e->getStandardError(), $e->getMessage());
}
$valid = false;
if(is_array($result))
{
$valid = true;
}
elseif($result instanceof SerializableInterface)
{
$valid = true;
}
elseif(is_string($result))
{
$valid = true;
}
elseif(is_bool($result))
{
$valid = true;
}
elseif(is_int($result))
{
$valid = true;
}
elseif(is_null($result))
{
$valid = true;
}
/**
* Returns an array representation of the object.
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'method' => $this->method,
'parameters' => $this->parameters
];
}
if(!$valid)
{
throw new InvalidArgumentException('The \'$result\' property must either be string, boolean, integer, array, null or SerializableInterface');
}
/**
* Returns the request object from an array of data.
*
* @param array $data The data to construct the object from.
* @return RpcRequest The request object.
*/
public static function fromArray(array $data): RpcRequest
{
return new RpcRequest($data['method'], $data['id'] ?? null, $data['parameters'] ?? null);
}
}
Logger::getLogger()->verbose(sprintf('Producing response for request %s', $this->id));
return new RpcResponse($this->id, $result);
}
/**
* Produces an error response based off the request, null if the request is a notification
*
* @param StandardError $error
* @param string|null $message
* @return RpcError|null
*/
public function produceError(StandardError $error, ?string $message=null): ?RpcError
{
if($this->id == null)
{
return null;
}
if($message == null)
{
$message = $error->getMessage();
}
return new RpcError($this->id, $error, $message);
}
/**
* @param StandardException $e
* @return RpcError|null
*/
public function handleStandardException(StandardException $e): ?RpcError
{
return $this->produceError($e->getStandardError(), $e->getMessage());
}
/**
* Returns an array representation of the object.
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'method' => $this->method,
'parameters' => $this->parameters
];
}
/**
* Returns the request object from an array of data.
*
* @param array $data The data to construct the object from.
* @return RpcRequest The request object.
*/
public static function fromArray(array $data): RpcRequest
{
return new RpcRequest($data['method'], $data['id'] ?? null, $data['parameters'] ?? null);
}
}

View file

@ -2,7 +2,103 @@
namespace Socialbox\Objects\Standard;
class SessionState
{
use DateTime;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Interfaces\SerializableInterface;
class SessionState implements SerializableInterface
{
private string $uuid;
private string $identifiedAs;
private bool $authenticated;
/**
* @var SessionFlags[]|null
*/
private ?array $flags;
private DateTime $created;
/**
* Constructor for initializing the object with the provided data.
*
* @param array $data An associative array containing the values for initializing the object.
* - 'uuid': string, Unique identifier.
* - 'identified_as': mixed, The identity information.
* - 'authenticated': bool, Whether the object is authenticated.
* - 'flags': string|null, Optional flags in
* @throws \DateMalformedStringException
*/
public function __construct(array $data)
{
$this->uuid = $data['uuid'];
$this->identifiedAs = $data['identified_as'];
$this->authenticated = $data['authenticated'];
if(is_string($data['flags']))
{
$this->flags = SessionFlags::fromString($data['flags']);
}
elseif(is_array($data['flags']))
{
$this->flags = $data['flags'];
}
else
{
$this->flags = null;
}
if(is_int($data['created']))
{
$this->created = new DateTime();
$this->created->setTimestamp($data['created']);
}
elseif($data['created'] instanceof DateTime)
{
$this->created = $data['created'];
}
else
{
$this->created = new DateTime($data['created']);
}
}
public function getUuid(): string
{
return $this->uuid;
}
public function getIdentifiedAs(): string
{
return $this->identifiedAs;
}
public function isAuthenticated(): bool
{
return $this->authenticated;
}
public function getFlags(): ?array
{
return $this->flags;
}
public function getCreated(): DateTime
{
return $this->created;
}
public static function fromArray(array $data): SessionState
{
return new self($data);
}
public function toArray(): array
{
return [
'uuid' => $this->uuid,
'identified_as' => $this->identifiedAs,
'authenticated' => $this->authenticated,
'flags' => $this->flags,
'created' => $this->created->getTimestamp()
];
}
}