Remove unused StandardMethods and improve session logic

This commit is contained in:
netkas 2024-12-12 04:33:10 -05:00
parent 86435a3d0b
commit 701acfde35
30 changed files with 1032 additions and 704 deletions

View file

@ -13,15 +13,6 @@ enum PeerFlags : string
// General Flags
case VERIFIED = 'VERIFIED';
// Verification Flags
case VER_SET_PASSWORD = 'VER_SET_PASSWORD';
case VER_SET_OTP = 'VER_SET_OTP';
case VER_SET_DISPLAY_NAME = 'VER_SET_DISPLAY_NAME';
case VER_EMAIL = 'VER_EMAIL';
case VER_SMS = 'VER_SMS';
case VER_PHONE_CALL = 'VER_PHONE_CALL';
case VER_SOLVE_IMAGE_CAPTCHA = 'VER_SOLVE_IMAGE_CAPTCHA';
/**
* Converts an array of PeerFlags enums to a string representation
*
@ -48,20 +39,4 @@ enum PeerFlags : string
return array_map(fn(string $value) => PeerFlags::from(trim($value)), explode(',', $flagString));
}
/**
* Returns whether the flag is public. Public flags can be seen by other peers.
*
* @return bool
*/
public function isPublic(): bool
{
return match($this)
{
self::VER_SET_PASSWORD,
self::VER_SET_OTP,
self::VER_SOLVE_IMAGE_CAPTCHA => false,
default => true,
};
}
}

View file

@ -5,9 +5,9 @@
enum SessionFlags : string
{
// Verification, require fields
case VER_SET_PASSWORD = 'VER_SET_PASSWORD'; // Peer has to set a password
case VER_SET_OTP = 'VER_SET_OTP'; // Peer has to set an OTP
case VER_SET_DISPLAY_NAME = 'VER_SET_DISPLAY_NAME'; // Peer has to set a display name
case SET_PASSWORD = 'SET_PASSWORD'; // Peer has to set a password
case SET_OTP = 'SET_OTP'; // Peer has to set an OTP
case SET_DISPLAY_NAME = 'SET_DISPLAY_NAME'; // Peer has to set a display name
// Verification, verification requirements
case VER_EMAIL = 'VER_EMAIL'; // Peer has to verify their email
@ -18,4 +18,31 @@
// Login, require fields
case VER_PASSWORD = 'VER_PASSWORD'; // Peer has to enter their password
case VER_OTP = 'VER_OTP'; // Peer has to enter their OTP
/**
* Converts an array of SessionFlags to a comma-separated string of their values.
*
* @param array $flags An array of SessionFlags objects to be converted.
* @return string A comma-separated string of the values of the provided SessionFlags.
*/
public static function toString(array $flags): string
{
return implode(',', array_map(fn(SessionFlags $flag) => $flag->value, $flags));
}
/**
* Converts a comma-separated string of flag values into an array of SessionFlags objects.
*
* @param string $flagString A comma-separated string representing flag values.
* @return array An array of SessionFlags objects created from the provided string.
*/
public static function fromString(string $flagString): array
{
if (empty($flagString))
{
return [];
}
return array_map(fn(string $value) => SessionFlags::from(trim($value)), explode(',', $flagString));
}
}

View file

@ -4,6 +4,11 @@ namespace Socialbox\Enums;
enum SessionState : string
{
/**
* The session is awaiting a Diffie-Hellman exchange to be completed
*/
case AWAITING_DHE = 'AWAITING_DHE';
/**
* The session is currently active and usable
*/

View file

@ -17,15 +17,19 @@ enum StandardError : int
// Authentication/Cryptography Errors
case INVALID_PUBLIC_KEY = -3000;
case UNSUPPORTED_AUTHENTICATION_TYPE = -3001;
case ALREADY_AUTHENTICATED = -3002;
case AUTHENTICATION_REQUIRED = -3003;
case SESSION_NOT_FOUND = -3004;
case SESSION_REQUIRED = -3005;
case REGISTRATION_DISABLED = -3006;
case CAPTCHA_NOT_AVAILABLE = -3007;
case INCORRECT_CAPTCHA_ANSWER = -3008;
case CAPTCHA_EXPIRED = -3009;
case SESSION_REQUIRED = -3001;
case SESSION_NOT_FOUND = -3002;
case SESSION_EXPIRED = -3003;
case SESSION_DHE_REQUIRED = -3004;
case ALREADY_AUTHENTICATED = -3005;
case UNSUPPORTED_AUTHENTICATION_TYPE = -3006;
case AUTHENTICATION_REQUIRED = -3007;
case REGISTRATION_DISABLED = -3008;
case CAPTCHA_NOT_AVAILABLE = -3009;
case INCORRECT_CAPTCHA_ANSWER = -3010;
case CAPTCHA_EXPIRED = -3011;
// General Error Messages
case PEER_NOT_FOUND = -4000;

View file

@ -1,68 +1,32 @@
<?php
namespace Socialbox\Enums;
/**
* Enumeration of standard header names used in HTTP communication.
*/
enum StandardHeaders : string
{
case CONTENT_TYPE = 'Content-Type';
case CLIENT_NAME = 'Client-Name';
case CLIENT_VERSION = 'Client-Version';
case SESSION_UUID = 'Session-UUID';
case FROM_PEER = 'From-Peer';
case SIGNATURE = 'Signature';
case PUBLIC_KEY = 'Public-Key';
namespace Socialbox\Enums;
/**
* Determines if the current instance is required based on its type.
*
* @return bool Returns true if the instance is of type CONTENT_TYPE, CLIENT_VERSION, or CLIENT_NAME; otherwise, false.
* Enumeration of standard header names used in HTTP communication.
*/
public function isRequired(): bool
enum StandardHeaders : string
{
return match($this)
{
self::CONTENT_TYPE,
self::CLIENT_VERSION,
self::CLIENT_NAME
=> true,
case REQUEST_TYPE = 'Request-Type';
case IDENTIFY_AS = 'Identify-As';
case CLIENT_NAME = 'Client-Name';
case CLIENT_VERSION = 'Client-Version';
case PUBLIC_KEY = 'Public-Key';
default => false,
};
}
case SESSION_UUID = 'Session-UUID';
case SIGNATURE = 'Signature';
/**
* Retrieves an array of required headers.
*
* @return array An array containing only the headers that are marked as required.
*/
public static function getRequiredHeaders(): array
{
$results = [];
foreach(StandardHeaders::cases() as $header)
/**
* @return array
*/
public static function toArray(): array
{
if($header->isRequired())
$results = [];
foreach(StandardHeaders::cases() as $header)
{
$results[] = $header->value;
}
return $results;
}
return $results;
}
/**
* @return array
*/
public static function toArray(): array
{
$results = [];
foreach(StandardHeaders::cases() as $header)
{
$results[] = $header->value;
}
return $results;
}
}
}

View file

@ -12,17 +12,12 @@ use Socialbox\Classes\StandardMethods\Register;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\ClientRequestOld;
use Socialbox\Objects\RpcRequest;
enum StandardMethods : string
{
case PING = 'ping';
case CREATE_SESSION = 'createSession';
case REGISTER = 'register';
case IDENTIFY = 'identify';
case GET_ME = 'getMe';
case VERIFICATION_GET_IMAGE_CAPTCHA = 'verificationGetImageCaptcha';
case VERIFICATION_ANSWER_IMAGE_CAPTCHA = 'verificationAnswerImageCaptcha';
/**
* @param ClientRequest $request
@ -35,12 +30,6 @@ enum StandardMethods : string
return match ($this)
{
self::PING => Ping::execute($request, $rpcRequest),
self::CREATE_SESSION => CreateSession::execute($request, $rpcRequest),
self::REGISTER => Register::execute($request, $rpcRequest),
self::IDENTIFY => Identify::execute($request, $rpcRequest),
self::GET_ME => GetMe::execute($request, $rpcRequest),
self::VERIFICATION_GET_IMAGE_CAPTCHA => VerificationGetImageCaptcha::execute($request, $rpcRequest),
self::VERIFICATION_ANSWER_IMAGE_CAPTCHA => VerificationAnswerImageCaptcha::execute($request, $rpcRequest),
};
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Socialbox\Enums\Types;
enum RequestType : string
{
/**
* Represents the action of initiating a session.
*/
case INITIATE_SESSION = 'init';
/**
* Represents the action of performing a Diffie-Hellman key exchange.
*/
case DHE_EXCHANGE = 'dhe';
/**
* Represents the action of performing a remote procedure call.
*/
case RPC = 'rpc';
}