Implement client session management and refactoring
This commit is contained in:
parent
cad2ea3419
commit
3a10e01bd8
11 changed files with 234 additions and 23 deletions
117
src/Socialbox/Classes/ClientCommands/ConnectCommand.php
Normal file
117
src/Socialbox/Classes/ClientCommands/ConnectCommand.php
Normal file
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\ClientCommands;
|
||||
|
||||
use Socialbox\Classes\Cryptography;
|
||||
use Socialbox\Classes\Logger;
|
||||
use Socialbox\Classes\Utilities;
|
||||
use Socialbox\Exceptions\CryptographyException;
|
||||
use Socialbox\Exceptions\DatabaseOperationException;
|
||||
use Socialbox\Exceptions\ResolutionException;
|
||||
use Socialbox\Exceptions\RpcException;
|
||||
use Socialbox\Interfaces\CliCommandInterface;
|
||||
use Socialbox\Objects\ClientSession;
|
||||
use Socialbox\SocialClient;
|
||||
|
||||
class ConnectCommand implements CliCommandInterface
|
||||
{
|
||||
public static function execute(array $args): int
|
||||
{
|
||||
if(!isset($args['name']))
|
||||
{
|
||||
Logger::getLogger()->error('The name argument is required, this is the name of the session');
|
||||
}
|
||||
|
||||
$workingDirectory = getcwd();
|
||||
|
||||
if(isset($args['directory']))
|
||||
{
|
||||
if(!is_dir($args['directory']))
|
||||
{
|
||||
Logger::getLogger()->error('The directory provided does not exist');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$workingDirectory = $args['directory'];
|
||||
}
|
||||
|
||||
$sessionFile = $workingDirectory . DIRECTORY_SEPARATOR . Utilities::sanitizeFileName($args['name']) . '.json';
|
||||
|
||||
if(!file_exists($sessionFile))
|
||||
{
|
||||
return self::createSession($args, $sessionFile);
|
||||
}
|
||||
|
||||
Logger::getLogger()->info(sprintf('Session file already exists at %s', $sessionFile));
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static function createSession(array $args, string $sessionFile): int
|
||||
{
|
||||
if(!isset($args['domain']))
|
||||
{
|
||||
Logger::getLogger()->error('The domain argument is required, this is the domain of the socialbox instance');
|
||||
return 1;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$client = new SocialClient($args['domain']);
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
Logger::getLogger()->error('Failed to create the client session', $e);
|
||||
return 1;
|
||||
}
|
||||
catch (ResolutionException $e)
|
||||
{
|
||||
Logger::getLogger()->error('Failed to resolve the domain', $e);
|
||||
return 1;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$keyPair = Cryptography::generateKeyPair();
|
||||
$session = $client->createSession($keyPair);
|
||||
}
|
||||
catch (CryptographyException | RpcException $e)
|
||||
{
|
||||
Logger::getLogger()->error('Failed to create the session', $e);
|
||||
return 1;
|
||||
}
|
||||
|
||||
$sessionData = new ClientSession([
|
||||
'domain' => $args['domain'],
|
||||
'session_uuid' => $session,
|
||||
'public_key' => $keyPair->getPublicKey(),
|
||||
'private_key' => $keyPair->getPrivateKey()
|
||||
]);
|
||||
|
||||
$sessionData->save($sessionFile);
|
||||
Logger::getLogger()->info(sprintf('Session created and saved to %s', $sessionFile));
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function getHelpMessage(): string
|
||||
{
|
||||
return <<<HELP
|
||||
Usage: socialbox connect --name <name> --domain <domain> [--directory <directory>]
|
||||
|
||||
Creates a new session with the specified name and domain. The session will be saved to the current working directory by default, or to the specified directory if provided.
|
||||
|
||||
Options:
|
||||
--name The name of the session to create.
|
||||
--domain The domain of the socialbox instance.
|
||||
--directory The directory where the session file should be saved.
|
||||
|
||||
Example:
|
||||
socialbox connect --name mysession --domain socialbox.example.com
|
||||
HELP;
|
||||
|
||||
}
|
||||
|
||||
public static function getShortHelpMessage(): string
|
||||
{
|
||||
return 'Connect Command - Creates a new session with the specified name and domain';
|
||||
}
|
||||
}
|
|
@ -27,8 +27,8 @@
|
|||
* Constructor for initializing the server connection with a given domain.
|
||||
*
|
||||
* @param string $domain The domain used to resolve the server's endpoint and public key.
|
||||
* @throws DatabaseOperationException
|
||||
* @throws ResolutionException
|
||||
* @noinspection PhpUnhandledExceptionInspection
|
||||
*/
|
||||
public function __construct(string $domain)
|
||||
{
|
||||
|
|
|
@ -75,8 +75,8 @@ class VerificationGetImageCaptcha extends Method
|
|||
|
||||
// Build the captcha
|
||||
return $rpcRequest->produceResponse(new ImageCaptcha([
|
||||
'expires' => $captchaRecord->getExpires()->getTimestamp(),
|
||||
'image' => (new CaptchaBuilder($answer))->build()->inline()] // Returns HTML base64 encoded image of the captcha
|
||||
));
|
||||
'expires' => $captchaRecord->getExpires(),
|
||||
'image' => (new CaptchaBuilder($answer))->build()->inline()
|
||||
])); // Returns HTML base64 encoded image of the captcha
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Socialbox\Classes;
|
||||
|
||||
use DateTime;
|
||||
use InvalidArgumentException;
|
||||
use JsonException;
|
||||
use RuntimeException;
|
||||
|
@ -170,4 +171,25 @@ class Utilities
|
|||
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random CRC32 hash.
|
||||
*
|
||||
* @return string The generated CRC32 hash as a string.
|
||||
*/
|
||||
public static function randomCrc32(): string
|
||||
{
|
||||
return hash('crc32b', uniqid());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes a file name by removing any characters that are not alphanumeric, hyphen, or underscore.
|
||||
*
|
||||
* @param string $name The file name to be sanitized.
|
||||
* @return string The sanitized file name.
|
||||
*/
|
||||
public static function sanitizeFileName(string $name): string
|
||||
{
|
||||
return preg_replace('/[^a-zA-Z0-9-_]/', '', $name);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue