Add session flags management and encryption key support

This commit is contained in:
netkas 2024-12-10 13:30:08 -05:00
parent 790262db08
commit 1d452bc71b
8 changed files with 209 additions and 76 deletions

View file

@ -3,7 +3,6 @@
namespace Socialbox\Classes\CliCommands;
use Exception;
use LogLib\Log;
use PDOException;
use Socialbox\Abstracts\CacheLayer;
use Socialbox\Classes\Configuration;
@ -97,22 +96,28 @@ class InitializeCommand implements CliCommandInterface
}
}
if(!Configuration::getInstanceConfiguration()->getPublicKey() || !Configuration::getInstanceConfiguration()->getPrivateKey())
if(
!Configuration::getInstanceConfiguration()->getPublicKey() ||
!Configuration::getInstanceConfiguration()->getPrivateKey() ||
!Configuration::getInstanceConfiguration()->getEncryptionKey()
)
{
try
{
Logger::getLogger()->info('Generating new key pair...');
$keyPair = Cryptography::generateKeyPair();
$encryptionKey = Cryptography::randomBytes(230, 314);
}
catch (CryptographyException $e)
{
Logger::getLogger()->error('Failed to generate keypair', $e);
Logger::getLogger()->error('Failed to generate cryptography values', $e);
return 1;
}
Logger::getLogger()->info('Updating configuration...');
Configuration::getConfigurationLib()->set('instance.private_key', $keyPair->getPrivateKey());
Configuration::getConfigurationLib()->set('instance.public_key', $keyPair->getPublicKey());
Configuration::getConfigurationLib()->set('instance.encryption_key', $encryptionKey);
Configuration::getConfigurationLib()->save();
Logger::getLogger()->info(sprintf('Set the DNS TXT record for the domain %s to the following value:', Configuration::getInstanceConfiguration()->getDomain()));

View file

@ -36,6 +36,7 @@ class Configuration
$config->setDefault('instance.rpc_endpoint', null);
$config->setDefault('instance.private_key', null);
$config->setDefault('instance.public_key', null);
$config->setDefault('instance.encryption_key', null);
// Security Configuration
$config->setDefault('security.display_internal_exceptions', false);

View file

@ -1,76 +1,87 @@
<?php
namespace Socialbox\Classes\Configuration;
namespace Socialbox\Classes\Configuration;
class InstanceConfiguration
{
private bool $enabled;
private ?string $domain;
private ?string $rpcEndpoint;
private ?string $privateKey;
private ?string $publicKey;
/**
* Constructor that initializes object properties with the provided data.
*
* @param array $data An associative array with keys 'enabled', 'domain', 'private_key', and 'public_key'.
* @return void
*/
public function __construct(array $data)
class InstanceConfiguration
{
$this->enabled = (bool)$data['enabled'];
$this->domain = $data['domain'];
$this->rpcEndpoint = $data['rpc_endpoint'];
$this->privateKey = $data['private_key'];
$this->publicKey = $data['public_key'];
}
private bool $enabled;
private ?string $domain;
private ?string $rpcEndpoint;
private ?string $privateKey;
private ?string $publicKey;
private ?string $encryptionKey;
/**
* Checks if the current object is enabled.
*
* @return bool True if the object is enabled, false otherwise.
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* Constructor that initializes object properties with the provided data.
*
* @param array $data An associative array with keys 'enabled', 'domain', 'private_key', and 'public_key'.
* @return void
*/
public function __construct(array $data)
{
$this->enabled = (bool)$data['enabled'];
$this->domain = $data['domain'];
$this->rpcEndpoint = $data['rpc_endpoint'];
$this->privateKey = $data['private_key'];
$this->publicKey = $data['public_key'];
$this->encryptionKey = $data['encryption_key'];
}
/**
* Retrieves the domain.
*
* @return string|null The domain.
*/
public function getDomain(): ?string
{
return $this->domain;
}
/**
* Checks if the current object is enabled.
*
* @return bool True if the object is enabled, false otherwise.
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* @return string|null
*/
public function getRpcEndpoint(): ?string
{
return $this->rpcEndpoint;
}
/**
* Retrieves the domain.
*
* @return string|null The domain.
*/
public function getDomain(): ?string
{
return $this->domain;
}
/**
* Retrieves the private key.
*
* @return string|null The private key.
*/
public function getPrivateKey(): ?string
{
return $this->privateKey;
}
/**
* @return string|null
*/
public function getRpcEndpoint(): ?string
{
return $this->rpcEndpoint;
}
/**
* Retrieves the public key.
*
* @return string|null The public key.
*/
public function getPublicKey(): ?string
{
return $this->publicKey;
}
}
/**
* Retrieves the private key.
*
* @return string|null The private key.
*/
public function getPrivateKey(): ?string
{
return $this->privateKey;
}
/**
* Retrieves the public key.
*
* @return string|null The public key.
*/
public function getPublicKey(): ?string
{
return $this->publicKey;
}
/**
* Retrieves the encryption key.
*
* @return string|null The encryption key.
*/
public function getEncryptionKey(): ?string
{
return $this->encryptionKey;
}
}

View file

@ -3,6 +3,7 @@
namespace Socialbox\Classes;
use InvalidArgumentException;
use Random\RandomException;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Objects\KeyPair;
@ -266,4 +267,24 @@ class Cryptography
return true;
}
/**
* Generates a random sequence of bytes with a length determined between the specified minimum and maximum.
*
* @param int $minLength The minimum length of the generated byte sequence.
* @param int $maxLength The maximum length of the generated byte sequence.
* @return string A hexadecimal string representing the random byte sequence.
* @throws CryptographyException If the random byte generation fails.
*/
public static function randomBytes(int $minLength, int $maxLength): string
{
try
{
return bin2hex(random_bytes(random_int($minLength, $maxLength)));
}
catch(RandomException $e)
{
throw new CryptographyException('Failed to generate random bytes: ' . $e->getMessage());
}
}
}