Made message signing in Cryptography use SHA512 as the message content for... #1
6 changed files with 251 additions and 81 deletions
40
src/Socialbox/Classes/StandardMethods/GetSession.php
Normal file
40
src/Socialbox/Classes/StandardMethods/GetSession.php
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -192,4 +192,26 @@ class Utilities
|
|||
{
|
||||
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);
|
||||
}
|
||||
}
|
21
src/Socialbox/Enums/Flags/SessionFlags.php
Normal file
21
src/Socialbox/Enums/Flags/SessionFlags.php
Normal 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
|
||||
}
|
|
@ -5,10 +5,8 @@
|
|||
use DateMalformedStringException;
|
||||
use DateTime;
|
||||
use InvalidArgumentException;
|
||||
use LogLib\Log;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Socialbox\Classes\Configuration;
|
||||
use Socialbox\Classes\Cryptography;
|
||||
use Socialbox\Classes\Database;
|
||||
use Socialbox\Classes\Logger;
|
||||
|
@ -106,7 +104,7 @@
|
|||
|
||||
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
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
namespace Socialbox\Objects\Database;
|
||||
|
||||
use DateTime;
|
||||
use Socialbox\Classes\Utilities;
|
||||
use Socialbox\Enums\Flags\SessionFlags;
|
||||
use Socialbox\Enums\SessionState;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
|
||||
|
@ -13,9 +15,23 @@ class SessionRecord implements SerializableInterface
|
|||
private bool $authenticated;
|
||||
private string $publicKey;
|
||||
private SessionState $state;
|
||||
/**
|
||||
* @var SessionFlags[]
|
||||
*/
|
||||
private array $flags;
|
||||
private DateTime $created;
|
||||
private ?DateTime $lastRequest;
|
||||
|
||||
/**
|
||||
* 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->uuid = $data['uuid'];
|
||||
|
@ -24,6 +40,7 @@ class SessionRecord implements SerializableInterface
|
|||
$this->publicKey = $data['public_key'];
|
||||
$this->created = $data['created'];
|
||||
$this->lastRequest = $data['last_request'];
|
||||
$this->flags = Utilities::unserializeList($data['flags']);
|
||||
|
||||
if(SessionState::tryFrom($data['state']) == null)
|
||||
{
|
||||
|
@ -33,18 +50,34 @@ class SessionRecord implements SerializableInterface
|
|||
{
|
||||
$this->state = SessionState::from($data['state']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the UUID.
|
||||
*
|
||||
* @return string The UUID of the object.
|
||||
*/
|
||||
public function getUuid(): string
|
||||
{
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the UUID of the peer.
|
||||
*
|
||||
* @return string|null The UUID of the peer or null if not set.
|
||||
*/
|
||||
public function getPeerUuid(): ?string
|
||||
{
|
||||
return $this->peerUuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the user is authenticated.
|
||||
*
|
||||
* @return bool Returns true if the user is authenticated; otherwise, false.
|
||||
*/
|
||||
public function isAuthenticated(): bool
|
||||
{
|
||||
if($this->peerUuid === null)
|
||||
|
@ -52,34 +85,81 @@ class SessionRecord implements SerializableInterface
|
|||
return false;
|
||||
}
|
||||
|
||||
if(in_array(SessionFlags::AUTHENTICATED, $this->flags))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->authenticated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the public key associated with the instance.
|
||||
*
|
||||
* @return string Returns the public key as a string.
|
||||
*/
|
||||
public function getPublicKey(): string
|
||||
{
|
||||
return $this->publicKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the current session state.
|
||||
*
|
||||
* @return SessionState Returns the current state of the session.
|
||||
*/
|
||||
public function getState(): SessionState
|
||||
{
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 [
|
||||
|
@ -88,6 +168,7 @@ class SessionRecord implements SerializableInterface
|
|||
'authenticated' => $this->authenticated,
|
||||
'public_key' => $this->publicKey,
|
||||
'state' => $this->state->value,
|
||||
'flags' => Utilities::serializeList($this->flags),
|
||||
'created' => $this->created,
|
||||
'last_request' => $this->lastRequest,
|
||||
];
|
||||
|
|
8
src/Socialbox/Objects/Standard/SessionState.php
Normal file
8
src/Socialbox/Objects/Standard/SessionState.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Objects\Standard;
|
||||
|
||||
class SessionState
|
||||
{
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue