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

@ -5,41 +5,33 @@ 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 $address;
private string $username; private string $username;
private ?string $displayName;
/** /**
* @var PeerFlags[] * @var PeerFlags[]
*/ */
private array $flags; private array $flags;
private int $created; /**
* @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. * Constructor for initializing the object with provided data.
* *
* @param array|PeerRecord $data Data array containing initial values for object properties. * @param array $data Data array containing initial values for object properties.
*/ */
public function __construct(array|PeerRecord $data) public function __construct(array $data)
{ {
if($data instanceof PeerRecord)
{
$this->uuid = $data->getUuid();
$this->enabled = $data->isEnabled();
$this->username = $data->getUsername();
$this->address = $data->getAddress();
$this->displayName = $data->getDisplayName();
$this->flags = $data->getFlags();
$this->created = $data->getCreated()->getTimestamp();
return;
}
$this->uuid = $data['uuid']; $this->uuid = $data['uuid'];
$this->enabled = $data['enabled']; $this->enabled = $data['enabled'];
$this->username = $data['username']; $this->username = $data['username'];
@ -61,11 +53,11 @@ class SelfUser implements SerializableInterface
if($data['created'] instanceof DateTime) if($data['created'] instanceof DateTime)
{ {
$this->created = $data['created']->getTimestamp(); $this->registered = $data['created']->getTimestamp();
} }
else else
{ {
$this->created = $data['created']; $this->registered = $data['created'];
} }
return; return;
@ -122,9 +114,9 @@ class SelfUser implements SerializableInterface
* *
* @return int The timestamp when the object was created. * @return int The timestamp when the object was created.
*/ */
public function getCreated(): int public function getRegistered(): int
{ {
return $this->created; return $this->registered;
} }
/** /**
@ -153,7 +145,7 @@ class SelfUser implements SerializableInterface
'address' => $this->address, 'address' => $this->address,
'display_name' => $this->displayName, 'display_name' => $this->displayName,
'flags' => $flags, 'flags' => $flags,
'created' => $this->created 'created' => $this->registered
]; ];
} }
} }

View file

@ -891,17 +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)
{
throw new StandardRpcException('Failed to resolve the peer: ' . $e->getMessage(), StandardError::RESOLUTION_FAILED, $e);
}
// 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)
{
try
{
RegisteredPeerManager::synchronizeExternalPeer($peer);
}
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;
}
// if we're not identifying as a personal peer and If the peer exists, but it's outdated, synchronize it
if($identifiedAs === null && $existingPeer->getUpdated()->getTimestamp() < time() - Configuration::getPoliciesConfiguration()->getPeerSyncInterval())
{
try
{
$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);
@ -913,38 +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, but it's outdated, synchronize it // If the peer exists and is up to date, return it from our local database instead. (Quicker)
if($existingPeer->getUpdated()->getTimestamp() < time() - Configuration::getPoliciesConfiguration()->getPeerSyncInterval())
{
try try
{ {
$peer = self::getExternalSession($peerAddress->getDomain())->resolvePeer($peerAddress, $identifiedAs); $informationFields = PeerInformationManager::getFields($existingPeer);
}
catch(Exception $e)
{
throw new StandardRpcException('Failed to resolve the peer: ' . $e->getMessage(), StandardError::RESOLUTION_FAILED, $e);
}
try
{
RegisteredPeerManager::synchronizeExternalPeer($peer);
} }
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 obtain local information fields about an external peer locally due to an internal server error', StandardError::INTERNAL_SERVER_ERROR, $e);
} }
return $peer; return new Peer([
} 'address' => $existingPeer->getAddress(),
'information_fields' => $informationFields,
// If the peer exists and is up to date, return it 'flags' => $existingPeer->getFlags(),
return $existingPeer->toStandardPeer(); 'registered' => $existingPeer->getCreated()->getTimestamp()
]);
} }
/** /**