Major changes, revamp required

This commit is contained in:
netkas 2025-02-20 00:34:07 -05:00
parent 1f9890bba0
commit 29a3d42538
20 changed files with 523 additions and 662 deletions

View file

@ -0,0 +1,129 @@
<?php
namespace Socialbox\Objects\Client;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\PeerAddress;
class EncryptionChannelSecret implements SerializableInterface
{
private string $channelUuid;
private PeerAddress $receiver;
private string $signatureUuid;
private string $publicEncryptionKey;
private string $privateEncryptionKey;
private string $transportEncryptionAlgorithm;
private ?string $transportEncryptionKey;
/**
* Public constructor
*
* @param array $data The data to create the object
*/
public function __construct(array $data)
{
$this->channelUuid = $data['uuid'];
$this->receiver = PeerAddress::fromAddress($data['receiver']);
$this->signatureUuid = $data['signature_uuid'];
$this->publicEncryptionKey = $data['public_encryption_key'];
$this->privateEncryptionKey = $data['private_encryption_key'];
$this->transportEncryptionAlgorithm = $data['transport_encryption_algorithm'];
$this->transportEncryptionKey = $data['transport_encryption_key'] ?? null;
}
/**
* Returns the UUID of the key pair
*
* @return string The UUID of the key pair
*/
public function getChannelUuid(): string
{
return $this->channelUuid;
}
/**
* @return PeerAddress
*/
public function getReceiver(): PeerAddress
{
return $this->receiver;
}
/**
* Returns the UUID of the signature
*
* @return string The UUID of the signature
*/
public function getSignatureUuid(): string
{
return $this->signatureUuid;
}
/**
* Returns the public key of the key pair
*
* @return string The public key of the key pair
*/
public function getPublicEncryptionKey(): string
{
return $this->publicEncryptionKey;
}
/**
* Returns the private key of the key pair
*
* @return string The private key of the key pair
*/
public function getPrivateEncryptionKey(): string
{
return $this->privateEncryptionKey;
}
/**
* @return string
*/
public function getTransportEncryptionAlgorithm(): string
{
return $this->transportEncryptionAlgorithm;
}
/**
* @return string|null
*/
public function getTransportEncryptionKey(): ?string
{
return $this->transportEncryptionKey;
}
/**
* @param string|null $transportEncryptionKey
*/
public function setTransportEncryptionKey(?string $transportEncryptionKey): void
{
$this->transportEncryptionKey = $transportEncryptionKey;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): EncryptionChannelSecret
{
return new self($data);
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'uuid' => $this->channelUuid,
'receiver' => $this->receiver->getAddress(),
'signature_uuid' => $this->signatureUuid,
'public_key' => $this->publicEncryptionKey,
'private_key' => $this->privateEncryptionKey,
'transport_encryption_algorithm' => $this->transportEncryptionAlgorithm,
'transport_encryption_key' => $this->transportEncryptionKey
];
}
}

View file

@ -29,6 +29,10 @@
* @var SignatureKeyPair[]
*/
private array $signingKeys;
/**
* @var EncryptionChannelSecret[]
*/
private array $encryptionChannelSecrets;
/**
* Constructor method to initialize class properties from the provided data array.
@ -62,6 +66,7 @@
$this->serverTransportEncryptionKey = $data['server_transport_encryption_key'];
$this->defaultSigningKey = $data['default_signing_key'] ?? null;
$this->signingKeys = array_map(fn($key) => SignatureKeyPair::fromArray($key), $data['signing_keys']);
$this->encryptionChannelSecrets = array_map(fn($key) => EncryptionChannelSecret::fromArray($key), $data['encryption_channel_secrets']);
}
/**
@ -234,6 +239,60 @@
return $this->signingKeys;
}
/**
* Retrieves the encrypted channel keys associated with the current instance.
*
* @return EncryptionChannelSecret[] The encrypted channel keys.
*/
public function getEncryptionChannelSecrets(): array
{
return $this->encryptionChannelSecrets;
}
/**
* Retrieves the signing key associated with the provided UUID.
*
* @param string $uuid The UUID of the signing key.
* @return SignatureKeyPair|null The signing key.
*/
public function getEncryptionChannelSecret(string $uuid): ?EncryptionChannelSecret
{
return $this->encryptionChannelSecrets[$uuid] ?? null;
}
/**
* Adds a new signing key to the current instance.
*
* @param EncryptionChannelSecret $key The signing key to add.
* @return void
*/
public function addEncryptionChannelSecret(EncryptionChannelSecret $key): void
{
$this->encryptionChannelSecrets[$key->getChannelUuid()] = $key;
}
/**
* Removes the signing key associated with the provided UUID.
*
* @param string $uuid The UUID of the signing key to remove.
* @return void
*/
public function removeEncryptionChannelSecret(string $uuid): void
{
unset($this->encryptionChannelSecrets[$uuid]);
}
/**
* Checks if a signing key exists for the provided UUID.
*
* @param string $uuid The UUID of the signing key.
* @return bool True if the signing key exists, false otherwise.
*/
public function encryptionChannelSecretExists(string $uuid): bool
{
return isset($this->encryptionChannelSecrets[$uuid]);
}
/**
* @inheritDoc
*/
@ -256,7 +315,8 @@
'client_transport_encryption_key' => $this->clientTransportEncryptionKey,
'server_transport_encryption_key' => $this->serverTransportEncryptionKey,
'default_signing_key' => $this->defaultSigningKey,
'signing_keys' => array_map(fn($key) => $key->toArray(), $this->signingKeys)
'signing_keys' => array_map(fn($key) => $key->toArray(), $this->signingKeys),
'encryption_channel_secrets' => array_map(fn($key) => $key->toArray(), $this->encryptionChannelSecrets),
];
}

View file

@ -170,7 +170,6 @@
*
* @return SessionRecord|null Returns the associated SessionRecord if the session UUID exists, or null if no session UUID is set.
* @throws DatabaseOperationException Thrown if an error occurs while retrieving the session.
* @throws StandardRpcException Thrown if the session UUID is invalid.
*/
public function getSession(): ?SessionRecord
{
@ -187,7 +186,6 @@
*
* @return PeerDatabaseRecord|null Returns the associated RegisteredPeerRecord if available, or null if no session exists.
* @throws DatabaseOperationException Thrown if an error occurs while retrieving the peer.
* @throws StandardRpcException Thrown if the session UUID is invalid.
*/
public function getPeer(): ?PeerDatabaseRecord
{
@ -201,6 +199,36 @@
return RegisteredPeerManager::getPeer($session->getPeerUuid());
}
/**
* Returns the Peer Database Record of the identified peer of the request
*
* @return PeerDatabaseRecord|null The Peer Database Record of the identified peer or null if not set
* @throws DatabaseOperationException Thrown if an error occurs while retrieving the peer.
*/
public function getIdentifiedAsPeer(): ?PeerDatabaseRecord
{
$identifiedAs = $this->getIdentifyAs();
if($identifiedAs === null)
{
return null;
}
return RegisteredPeerManager::getPeerByAddress($identifiedAs);
}
/**
* Returns whether the request is external or not. As in, if the request is coming from server rather than
* a client.
*
* @return bool True if the request is external, false otherwise.
* @throws DatabaseOperationException Thrown if an error occurs while retrieving the peer.
* @throws StandardRpcException Thrown if the session UUID is invalid.
*/
public function isExternal(): bool
{
return $this->getPeer()->isExternal();
}
/**
* Retrieves the signature value.
*

View file

@ -30,7 +30,6 @@
* - 'signature' (string): The signature.
* - 'received' (bool): Whether the message has been received.
* - 'timestamp' (int|string|\DateTime): The timestamp of the message.
* @throws DateMalformedStringException If the timestamp is a string that cannot be parsed.
*/
public function __construct(array $data)
{

View file

@ -17,7 +17,6 @@
private string $callingEncryptionPublicKey;
private PeerAddress $receivingPeer;
private ?string $receivingSignatureUuid;
private ?string $receivingSignaturePublicKey;
private ?string $receivingEncryptionPublicKey;
private string $transportEncryptionAlgorithm;
private ?string $transportEncryptionKey;
@ -28,7 +27,6 @@
* Public Constructor for the encryption channel record
*
* @param array $data
* @throws \DateMalformedStringException
*/
public function __construct(array $data)
{
@ -78,7 +76,6 @@
}
$this->receivingSignatureUuid = $data['receiving_signature_uuid'] ?? null;
$this->receivingSignaturePublicKey = $data['receiving_signature_public_key'] ?? null;
$this->receivingEncryptionPublicKey = $data['receiving_encryption_public_key'] ?? null;
$this->transportEncryptionAlgorithm = $data['transport_encryption_algorithm'];
$this->transportEncryptionKey = $data['transport_encryption_key'] ?? null;
@ -169,16 +166,6 @@
return $this->receivingSignatureUuid;
}
/**
* Returns the public key of the signing keypair that the receiver is using
*
* @return string|null
*/
public function getReceivingSignaturePublicKey(): ?string
{
return $this->receivingSignaturePublicKey;
}
/**
* Returns the public key of the encryption keypair that the receiver is using
*
@ -249,7 +236,6 @@
'calling_encryption_public_key' => $this->callingEncryptionPublicKey,
'receiving_peer' => $this->receivingPeer->getAddress(),
'receiving_signature_uuid' => $this->receivingSignatureUuid,
'receiving_signature_public_key' => $this->receivingSignaturePublicKey,
'receiving_encryption_public_key' => $this->receivingEncryptionPublicKey,
'transport_encryption_algorithm' => $this->transportEncryptionAlgorithm,
'transport_encryption_key' => $this->transportEncryptionKey,

View file

@ -83,7 +83,7 @@
return false;
}
return $this->username === ReservedUsernames::HOST->value;
return $this->domain !== Configuration::getInstanceConfiguration()->getDomain();
}
/**

View file

@ -87,6 +87,26 @@
return isset($this->parameters[$parameter]);
}
/**
* Checks if the parameters exist within the RPC request
*
* @param array $parameters The parameters to check
* @param bool $nullAllowed True if the parameter value can be null, False otherwise.
* @return bool True if the parameters exist, False otherwise.
*/
public function containsParameters(array $parameters, bool $nullAllowed=false): bool
{
foreach($parameters as $parameter)
{
if(!$this->containsParameter($parameter, $nullAllowed))
{
return false;
}
}
return true;
}
/**
* Returns the parameter value from the RPC request
*