Restructured StandardMethod Namespaces

This commit is contained in:
netkas 2025-01-29 15:52:38 -05:00
parent d6e397247a
commit 1e10e761db
38 changed files with 72 additions and 82 deletions

View file

@ -0,0 +1,44 @@
<?php
namespace Socialbox\Classes\StandardMethods\Verification;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class Authenticate extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
try
{
if(!$request->getPeer()->isExternal())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Only external peers can authenticate using this method');
}
if($request->getSession()->isAuthenticated())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'External host is already authenticated');
}
SessionManager::updateFlow($request->getSession(), [SessionFlags::AUTHENTICATION_REQUIRED]);
}
catch(Exception $e)
{
throw new StandardException('An error occurred while authenticating the peer', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,59 @@
<?php
namespace Socialbox\Classes\StandardMethods\Verification;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\CaptchaManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class VerificationAnswerImageCaptcha extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('answer'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The answer parameter is required');
}
$session = $request->getSession();
try
{
if(CaptchaManager::getCaptcha($session->getPeerUuid())?->isExpired())
{
return $rpcRequest->produceError(StandardError::CAPTCHA_EXPIRED, 'The captcha has expired');
}
}
catch(DatabaseOperationException $e)
{
throw new StandardException("There was an unexpected error while trying to get the captcha", StandardError::INTERNAL_SERVER_ERROR, $e);
}
try
{
$result = CaptchaManager::answerCaptcha($session->getPeerUuid(), $rpcRequest->getParameter('answer'));
if($result)
{
SessionManager::updateFlow($session, [SessionFlags::VER_IMAGE_CAPTCHA]);
}
}
catch (DatabaseOperationException $e)
{
throw new StandardException("There was an unexpected error while trying to answer the captcha", StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse($result);
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace Socialbox\Classes\StandardMethods\Verification;
use Gregwar\Captcha\CaptchaBuilder;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\CaptchaManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
use Socialbox\Objects\Standard\ImageCaptchaVerification;
class VerificationGetImageCaptcha extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
$session = $request->getSession();
// Check for session conditions
if(!$session->flagExists(SessionFlags::VER_IMAGE_CAPTCHA))
{
return $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, 'Solving an image captcha is not required at this time');
}
$peer = $request->getPeer();
try
{
if(CaptchaManager::captchaExists($peer))
{
$captchaRecord = CaptchaManager::getCaptcha($peer);
if($captchaRecord->isExpired())
{
$answer = CaptchaManager::createCaptcha($peer);
$captchaRecord = CaptchaManager::getCaptcha($peer);
}
else
{
$answer = $captchaRecord->getAnswer();
}
}
else
{
$answer = CaptchaManager::createCaptcha($peer);
$captchaRecord = CaptchaManager::getCaptcha($peer);
}
}
catch (DatabaseOperationException $e)
{
throw new StandardException("There was an unexpected error while trying create the captcha", StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Build the captcha
// Returns HTML base64 encoded image of the captcha
// Important note: Must always be HTML-BASE64 which means it must be prefixed with `data:image/jpeg;base64,`
return $rpcRequest->produceResponse(new ImageCaptchaVerification([
'expires' => $captchaRecord->getExpires(),
'content' => (new CaptchaBuilder($answer))->build()->inline()
]));
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace Socialbox\Classes\StandardMethods\Verification;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\OneTimePasswordManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class VerificationOtpAuthentication extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('code'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'code' parameter");
}
if(strlen($rpcRequest->getParameter('code')) !== Configuration::getSecurityConfiguration()->getOtpDigits())
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'code' parameter length");
}
$session = $request->getSession();
if(!$session->flagExists(SessionFlags::VER_OTP))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'One Time Password verification is not required at this time');
}
try
{
$result = OneTimePasswordManager::verifyOtp($request->getPeer(), $rpcRequest->getParameter('code'));
if($result)
{
// If the OTP is verified, remove the OTP verification flag
SessionManager::updateFlow($session, [SessionFlags::VER_OTP]);
}
}
catch (CryptographyException)
{
return $rpcRequest->produceResponse(false);
}
catch (Exception $e)
{
throw new StandardException('Failed to verify password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse($result);
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Socialbox\Classes\StandardMethods\Verification;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Cryptography;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\PasswordManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class VerificationPasswordAuthentication extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('password'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'password' parameter");
}
if(!Cryptography::validateSha512($rpcRequest->getParameter('password')))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'password' parameter, must be a valid SHA-512 hash");
}
$session = $request->getSession();
if(!$session->flagExists(SessionFlags::VER_PASSWORD))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Password verification is not required at this time');
}
try
{
$result = PasswordManager::verifyPassword($request->getPeer()->getUuid(), $rpcRequest->getParameter('password'));
if($result)
{
SessionManager::updateFlow($request->getSession(), [SessionFlags::VER_PASSWORD]);
}
}
catch (CryptographyException)
{
return $rpcRequest->produceResponse(false);
}
catch (Exception $e)
{
throw new StandardException('Failed to verify password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse($result);
}
}