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

Closed
netkas wants to merge 421 commits from master into dev
5 changed files with 209 additions and 155 deletions
Showing only changes of commit 8c2cbf48d5 - Show all commits

View file

@ -0,0 +1,28 @@
<?php
namespace Socialbox\Classes\StandardMethods\Core;
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\ReservedUsernames;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
use Socialbox\Exceptions\Standard\StandardRpcException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\RpcRequest;
use Socialbox\Socialbox;
class GetSelf extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
}
}

View file

@ -6,6 +6,7 @@
use Socialbox\Classes\Configuration; use Socialbox\Classes\Configuration;
use Socialbox\Classes\Utilities; use Socialbox\Classes\Utilities;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Objects\RpcError; use Socialbox\Objects\RpcError;
use Socialbox\Objects\RpcRequest; use Socialbox\Objects\RpcRequest;
use Throwable; use Throwable;
@ -38,4 +39,9 @@
return $request->produceError(StandardError::from($this->code), $this->message); return $request->produceError(StandardError::from($this->code), $this->message);
} }
public static function fromRpcException(RpcException $e): StandardRpcException
{
return new self($e->getMessage(), StandardError::tryFrom($e->getCode()) ?? StandardError::UNKNOWN, $e);
}
} }

View file

@ -7,6 +7,7 @@
use Socialbox\Classes\Configuration; use Socialbox\Classes\Configuration;
use Socialbox\Enums\Flags\PeerFlags; use Socialbox\Enums\Flags\PeerFlags;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Standard\InformationFieldState;
use Socialbox\Objects\Standard\Peer; use Socialbox\Objects\Standard\Peer;
class PeerDatabaseRecord implements SerializableInterface class PeerDatabaseRecord implements SerializableInterface
@ -197,13 +198,14 @@
/** /**
* Converts the current instance to a Peer object. * Converts the current instance to a Peer object.
* *
* @param PeerInformationFieldRecord[]|InformationFieldState[]|array $informationFields
* @return Peer The Peer representation of the current instance. * @return Peer The Peer representation of the current instance.
*/ */
public function toStandardPeer(): Peer public function toStandardPeer(array $informationFields): Peer
{ {
// TODO: TO be updated
return Peer::fromArray([ return Peer::fromArray([
'address' => $this->getAddress(), 'address' => $this->getAddress(),
'information_fields' => $informationFields,
'flags' => array_map(fn(PeerFlags $flag) => $flag->value, $this->flags), 'flags' => array_map(fn(PeerFlags $flag) => $flag->value, $this->flags),
'registered' => $this->created->getTimestamp() 'registered' => $this->created->getTimestamp()
]); ]);

View file

@ -1,159 +1,151 @@
<?php <?php
namespace Socialbox\Objects\Standard; namespace Socialbox\Objects\Standard;
use DateTime; use DateTime;
use Socialbox\Enums\Flags\PeerFlags; use Socialbox\Enums\Flags\PeerFlags;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Database\PeerRecord; use Socialbox\Objects\Database\PeerDatabaseRecord;
class SelfUser implements SerializableInterface class SelfUser implements SerializableInterface
{
private string $uuid;
private bool $enabled;
private string $address;
private string $username;
private ?string $displayName;
/**
* @var PeerFlags[]
*/
private array $flags;
private int $created;
/**
* Constructor for initializing the object with provided data.
*
* @param array|PeerRecord $data Data array containing initial values for object properties.
*/
public function __construct(array|PeerRecord $data)
{ {
if($data instanceof PeerRecord) private string $address;
private string $username;
/**
* @var PeerFlags[]
*/
private array $flags;
/**
* @var InformationFieldState[]
*/
private array $informationFieldStates;
private bool $passwordEnabled;
private ?int $passwordLastUpdated;
private bool $otpEnabled;
private ?int $otpLastUpdated;
private int $registered;
/**
* Constructor for initializing the object with provided data.
*
* @param array $data Data array containing initial values for object properties.
*/
public function __construct(array $data)
{ {
$this->uuid = $data->getUuid(); $this->uuid = $data['uuid'];
$this->enabled = $data->isEnabled(); $this->enabled = $data['enabled'];
$this->username = $data->getUsername(); $this->username = $data['username'];
$this->address = $data->getAddress(); $this->address = $data['address'];
$this->displayName = $data->getDisplayName(); $this->displayName = $data['display_name'] ?? null;
$this->flags = $data->getFlags();
$this->created = $data->getCreated()->getTimestamp(); if(is_string($data['flags']))
{
$this->flags = PeerFlags::fromString($data['flags']);
}
elseif(is_array($data['flags']))
{
$this->flags = $data['flags'];
}
else
{
$this->flags = [];
}
if($data['created'] instanceof DateTime)
{
$this->registered = $data['created']->getTimestamp();
}
else
{
$this->registered = $data['created'];
}
return; return;
} }
$this->uuid = $data['uuid']; /**
$this->enabled = $data['enabled']; * Retrieves the UUID of the object.
$this->username = $data['username']; *
$this->address = $data['address']; * @return string The UUID of the object.
$this->displayName = $data['display_name'] ?? null; */
public function getUuid(): string
if(is_string($data['flags']))
{ {
$this->flags = PeerFlags::fromString($data['flags']); return $this->uuid;
}
elseif(is_array($data['flags']))
{
$this->flags = $data['flags'];
}
else
{
$this->flags = [];
} }
if($data['created'] instanceof DateTime) public function isEnabled(): bool
{ {
$this->created = $data['created']->getTimestamp(); return $this->enabled;
}
else
{
$this->created = $data['created'];
} }
return; /**
} *
* @return string The username of the user.
/** */
* Retrieves the UUID of the object. public function getUsername(): string
*
* @return string The UUID of the object.
*/
public function getUuid(): string
{
return $this->uuid;
}
public function isEnabled(): bool
{
return $this->enabled;
}
/**
*
* @return string The username of the user.
*/
public function getUsername(): string
{
return $this->username;
}
public function getAddress(): string
{
return $this->address;
}
/**
*
* @return string|null The display name.
*/
public function getDisplayName(): ?string
{
return $this->displayName;
}
/**
*
* @return array
*/
public function getFlags(): array
{
return $this->flags;
}
/**
*
* @return int The timestamp when the object was created.
*/
public function getCreated(): int
{
return $this->created;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): SelfUser
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
$flags = [];
foreach($this->flags as $flag)
{ {
$flags[] = $flag->value; return $this->username;
} }
return [ public function getAddress(): string
'uuid' => $this->uuid, {
'enabled' => $this->enabled, return $this->address;
'username' => $this->username, }
'address' => $this->address,
'display_name' => $this->displayName, /**
'flags' => $flags, *
'created' => $this->created * @return string|null The display name.
]; */
} public function getDisplayName(): ?string
} {
return $this->displayName;
}
/**
*
* @return array
*/
public function getFlags(): array
{
return $this->flags;
}
/**
*
* @return int The timestamp when the object was created.
*/
public function getRegistered(): int
{
return $this->registered;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): SelfUser
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
$flags = [];
foreach($this->flags as $flag)
{
$flags[] = $flag->value;
}
return [
'uuid' => $this->uuid,
'enabled' => $this->enabled,
'username' => $this->username,
'address' => $this->address,
'display_name' => $this->displayName,
'flags' => $flags,
'created' => $this->registered
];
}
}

View file

@ -891,41 +891,53 @@
} }
catch(DatabaseOperationException $e) catch(DatabaseOperationException $e)
{ {
throw new StandardRpcException('Failed to resolve the peer: ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e); throw new StandardRpcException('Failed to resolve the peer due to an internal server error', StandardError::INTERNAL_SERVER_ERROR, $e);
} }
if($existingPeer === null) if($existingPeer === null)
{ {
// if the peer doesn't exist, resolve it externally and synchronize it // if the peer doesn't exist, resolve it externally and synchronize it
try try
{ {
$peer = self::getExternalSession($peerAddress->getDomain())->resolvePeer($peerAddress, $identifiedAs); $peer = self::getExternalSession($peerAddress->getDomain())->resolvePeer($peerAddress, $identifiedAs);
} }
catch(RpcException $e)
{
throw StandardRpcException::fromRpcException($e);
}
catch(Exception $e) catch(Exception $e)
{ {
throw new StandardRpcException('Failed to resolve the peer: ' . $e->getMessage(), StandardError::RESOLUTION_FAILED, $e); throw new StandardRpcException('Failed to resolve the peer: ' . $e->getMessage(), StandardError::RESOLUTION_FAILED, $e);
} }
try // Do not synchronize if this is a personal request, there may be information fields that
// the peer does not want to share with the server
if($identifiedAs !== null)
{ {
RegisteredPeerManager::synchronizeExternalPeer($peer); try
} {
catch(DatabaseOperationException $e) RegisteredPeerManager::synchronizeExternalPeer($peer);
{ }
throw new StandardRpcException('Failed to synchronize the external peer: ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e); catch(DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to synchronize the external peer due to an internal server error', StandardError::INTERNAL_SERVER_ERROR, $e);
}
} }
return $peer; return $peer;
} }
// If the peer exists, but it's outdated, synchronize it // if we're not identifying as a personal peer and If the peer exists, but it's outdated, synchronize it
if($existingPeer->getUpdated()->getTimestamp() < time() - Configuration::getPoliciesConfiguration()->getPeerSyncInterval()) if($identifiedAs === null && $existingPeer->getUpdated()->getTimestamp() < time() - Configuration::getPoliciesConfiguration()->getPeerSyncInterval())
{ {
try try
{ {
$peer = self::getExternalSession($peerAddress->getDomain())->resolvePeer($peerAddress, $identifiedAs); $peer = self::getExternalSession($peerAddress->getDomain())->resolvePeer($peerAddress, $identifiedAs);
} }
catch(RpcException $e)
{
throw StandardRpcException::fromRpcException($e);
}
catch(Exception $e) catch(Exception $e)
{ {
throw new StandardRpcException('Failed to resolve the peer: ' . $e->getMessage(), StandardError::RESOLUTION_FAILED, $e); throw new StandardRpcException('Failed to resolve the peer: ' . $e->getMessage(), StandardError::RESOLUTION_FAILED, $e);
@ -937,14 +949,28 @@
} }
catch(DatabaseOperationException $e) catch(DatabaseOperationException $e)
{ {
throw new StandardRpcException('Failed to synchronize the external peer: ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e); throw new StandardRpcException('Failed to synchronize the external peer due to an internal server error', StandardError::INTERNAL_SERVER_ERROR, $e);
} }
return $peer; return $peer;
} }
// If the peer exists and is up to date, return it // If the peer exists and is up to date, return it from our local database instead. (Quicker)
return $existingPeer->toStandardPeer(); try
{
$informationFields = PeerInformationManager::getFields($existingPeer);
}
catch(DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to obtain local information fields about an external peer locally due to an internal server error', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return new Peer([
'address' => $existingPeer->getAddress(),
'information_fields' => $informationFields,
'flags' => $existingPeer->getFlags(),
'registered' => $existingPeer->getCreated()->getTimestamp()
]);
} }
/** /**