Made message signing in Cryptography use SHA512 as the message content for... #1

Closed
netkas wants to merge 421 commits from master into dev
6 changed files with 251 additions and 81 deletions
Showing only changes of commit 790262db08 - Show all commits

View file

@ -0,0 +1,40 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class GetSession extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if($request->getSessionUuid() === null)
{
return $rpcRequest->produceError(StandardError::SESSION_REQUIRED);
}
try
{
// Get the session
$session = SessionManager::getSession($request->getSessionUuid());
}
catch(DatabaseOperationException $e)
{
throw new StandardException("There was an unexpected error while trying to retrieve the session", StandardError::INTERNAL_SERVER_ERROR, $e);
}
}
}

View file

@ -192,4 +192,26 @@ class Utilities
{ {
return preg_replace('/[^a-zA-Z0-9-_]/', '', $name); return preg_replace('/[^a-zA-Z0-9-_]/', '', $name);
} }
/**
* Converts an array into a serialized string by joining the elements with a comma.
*
* @param array $list An array of elements that need to be converted to a comma-separated string.
* @return string A string representation of the array elements, joined by commas.
*/
public static function serializeList(array $list): string
{
return implode(',', $list);
}
/**
* Converts a serialized string into an array by splitting the string at each comma.
*
* @param string $list A comma-separated string that needs to be converted to an array.
* @return array An array of string values obtained by splitting the input string.
*/
public static function unserializeList(string $list): array
{
return explode(',', $list);
}
} }

View file

@ -0,0 +1,21 @@
<?php
namespace Socialbox\Enums\Flags;
enum SessionFlags : string
{
// Verification, require fields
case VER_SET_PASSWORD = 'VER_SET_PASSWORD'; // Peer has to set a password
case VER_SET_OTP = 'VER_SET_OTP'; // Peer has to set an OTP
case VER_SET_DISPLAY_NAME = 'VER_SET_DISPLAY_NAME'; // Peer has to set a display name
// Verification, verification requirements
case VER_EMAIL = 'VER_EMAIL'; // Peer has to verify their email
case VER_SMS = 'VER_SMS'; // Peer has to verify their phone number
case VER_PHONE_CALL = 'VER_PHONE_CALL'; // Peer has to verify their phone number via a phone call
case VER_IMAGE_CAPTCHA = 'VER_IMAGE_CAPTCHA'; // Peer has to solve an image captcha
// Login, require fields
case VER_PASSWORD = 'VER_PASSWORD'; // Peer has to enter their password
case VER_OTP = 'VER_OTP'; // Peer has to enter their OTP
}

View file

@ -5,10 +5,8 @@
use DateMalformedStringException; use DateMalformedStringException;
use DateTime; use DateTime;
use InvalidArgumentException; use InvalidArgumentException;
use LogLib\Log;
use PDO; use PDO;
use PDOException; use PDOException;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Cryptography; use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Database; use Socialbox\Classes\Database;
use Socialbox\Classes\Logger; use Socialbox\Classes\Logger;
@ -106,7 +104,7 @@
if ($data === false) if ($data === false)
{ {
throw new StandardException(sprintf("The requested session '%s' does not exist"), StandardError::SESSION_NOT_FOUND); throw new StandardException(sprintf("The requested session '%s' does not exist", $uuid), StandardError::SESSION_NOT_FOUND);
} }
// Convert the timestamp fields to DateTime objects // Convert the timestamp fields to DateTime objects

View file

@ -1,95 +1,176 @@
<?php <?php
namespace Socialbox\Objects\Database; namespace Socialbox\Objects\Database;
use DateTime; use DateTime;
use Socialbox\Enums\SessionState; use Socialbox\Classes\Utilities;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\SessionState;
use Socialbox\Interfaces\SerializableInterface;
class SessionRecord implements SerializableInterface class SessionRecord implements SerializableInterface
{
private string $uuid;
private ?string $peerUuid;
private bool $authenticated;
private string $publicKey;
private SessionState $state;
private DateTime $created;
private ?DateTime $lastRequest;
public function __construct(array $data)
{ {
$this->uuid = $data['uuid']; private string $uuid;
$this->peerUuid = $data['peer_uuid'] ?? null; private ?string $peerUuid;
$this->authenticated = $data['authenticated'] ?? false; private bool $authenticated;
$this->publicKey = $data['public_key']; private string $publicKey;
$this->created = $data['created']; private SessionState $state;
$this->lastRequest = $data['last_request']; /**
* @var SessionFlags[]
*/
private array $flags;
private DateTime $created;
private ?DateTime $lastRequest;
if(SessionState::tryFrom($data['state']) == null) /**
* Constructs a new instance using the provided data array.
*
* @param array $data An associative array containing the initialization data,
* which should include keys such as 'uuid', 'peer_uuid',
* 'authenticated', 'public_key', 'created', 'last_request',
* 'flags', and 'state'.
*
* @return void
*/
public function __construct(array $data)
{ {
$this->state = SessionState::CLOSED; $this->uuid = $data['uuid'];
} $this->peerUuid = $data['peer_uuid'] ?? null;
else $this->authenticated = $data['authenticated'] ?? false;
{ $this->publicKey = $data['public_key'];
$this->state = SessionState::from($data['state']); $this->created = $data['created'];
} $this->lastRequest = $data['last_request'];
} $this->flags = Utilities::unserializeList($data['flags']);
public function getUuid(): string if(SessionState::tryFrom($data['state']) == null)
{ {
return $this->uuid; $this->state = SessionState::CLOSED;
} }
else
{
$this->state = SessionState::from($data['state']);
}
public function getPeerUuid(): ?string
{
return $this->peerUuid;
}
public function isAuthenticated(): bool
{
if($this->peerUuid === null)
{
return false;
} }
return $this->authenticated; /**
} * Retrieves the UUID.
*
* @return string The UUID of the object.
*/
public function getUuid(): string
{
return $this->uuid;
}
public function getPublicKey(): string /**
{ * Retrieves the UUID of the peer.
return $this->publicKey; *
} * @return string|null The UUID of the peer or null if not set.
*/
public function getPeerUuid(): ?string
{
return $this->peerUuid;
}
public function getState(): SessionState /**
{ * Checks whether the user is authenticated.
return $this->state; *
} * @return bool Returns true if the user is authenticated; otherwise, false.
*/
public function isAuthenticated(): bool
{
if($this->peerUuid === null)
{
return false;
}
public function getCreated(): DateTime if(in_array(SessionFlags::AUTHENTICATED, $this->flags))
{ {
return $this->created; return true;
} }
public function getLastRequest(): ?DateTime return $this->authenticated;
{ }
return $this->lastRequest;
}
public static function fromArray(array $data): object /**
{ * Retrieves the public key associated with the instance.
return new self($data); *
} * @return string Returns the public key as a string.
*/
public function getPublicKey(): string
{
return $this->publicKey;
}
public function toArray(): array /**
{ * Retrieves the current session state.
return [ *
'uuid' => $this->uuid, * @return SessionState Returns the current state of the session.
'peer_uuid' => $this->peerUuid, */
'authenticated' => $this->authenticated, public function getState(): SessionState
'public_key' => $this->publicKey, {
'state' => $this->state->value, return $this->state;
'created' => $this->created, }
'last_request' => $this->lastRequest,
]; /**
* Retrieves the creation date and time of the object.
*
* @return DateTime Returns a DateTime object representing when the object was created.
*/
public function getCreated(): DateTime
{
return $this->created;
}
/**
* Retrieves the list of flags associated with the current instance.
*
* @return array Returns an array of flags.
*/
public function getFlags(): array
{
return $this->flags;
}
/**
* Retrieves the timestamp of the last request made.
*
* @return DateTime|null The DateTime object representing the last request time, or null if no request has been made.
*/
public function getLastRequest(): ?DateTime
{
return $this->lastRequest;
}
/**
* Creates a new instance of the class using the provided array data.
*
* @param array $data An associative array of data used to initialize the object properties.
* @return object Returns a newly created object instance.
*/
public static function fromArray(array $data): object
{
return new self($data);
}
/**
* Converts the object's properties to an associative array.
*
* @return array An associative array representing the object's data, including keys 'uuid', 'peer_uuid',
* 'authenticated', 'public_key', 'state', 'flags', 'created', and 'last_request'.
*/
public function toArray(): array
{
return [
'uuid' => $this->uuid,
'peer_uuid' => $this->peerUuid,
'authenticated' => $this->authenticated,
'public_key' => $this->publicKey,
'state' => $this->state->value,
'flags' => Utilities::serializeList($this->flags),
'created' => $this->created,
'last_request' => $this->lastRequest,
];
}
} }
}

View file

@ -0,0 +1,8 @@
<?php
namespace Socialbox\Objects\Standard;
class SessionState
{
}