From cad2ea3419ef0fc9dd0d6cbcad9c5a85fd05b76e Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 31 Oct 2024 19:13:05 -0400 Subject: [PATCH] Add image captcha verification methods --- .../VerificationAnswerImageCaptcha.php | 96 +++++++++++++++++++ ...ha.php => VerificationGetImageCaptcha.php} | 2 +- src/Socialbox/Enums/StandardMethods.php | 9 +- 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 src/Socialbox/Classes/StandardMethods/VerificationAnswerImageCaptcha.php rename src/Socialbox/Classes/StandardMethods/{VerificationGetCaptcha.php => VerificationGetImageCaptcha.php} (98%) diff --git a/src/Socialbox/Classes/StandardMethods/VerificationAnswerImageCaptcha.php b/src/Socialbox/Classes/StandardMethods/VerificationAnswerImageCaptcha.php new file mode 100644 index 0000000..5cb8524 --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/VerificationAnswerImageCaptcha.php @@ -0,0 +1,96 @@ +getSessionUuid() === null) + { + return $rpcRequest->produceError(StandardError::SESSION_REQUIRED); + } + + // Get the session and check if it's already authenticated + try + { + $session = SessionManager::getSession($request->getSessionUuid()); + } + catch(DatabaseOperationException $e) + { + throw new StandardException("There was an unexpected error while trying to get the session", StandardError::INTERNAL_SERVER_ERROR, $e); + } + + // Check for session conditions + if($session->getPeerUuid() === null) + { + return $rpcRequest->produceError(StandardError::AUTHENTICATION_REQUIRED); + } + + // Get the peer + try + { + $peer = RegisteredPeerManager::getPeer($session->getPeerUuid()); + } + catch(DatabaseOperationException $e) + { + throw new StandardException("There was unexpected error while trying to get the peer", StandardError::INTERNAL_SERVER_ERROR, $e); + } + + // Check if the VER_SOLVE_IMAGE_CAPTCHA flag exists. + if(!$peer->flagExists(PeerFlags::VER_SOLVE_IMAGE_CAPTCHA)) + { + return $rpcRequest->produceError(StandardError::CAPTCHA_NOT_AVAILABLE, 'You are not required to complete a captcha at this time'); + } + + if(!$rpcRequest->containsParameter('answer')) + { + return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The answer parameter is required'); + } + + 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) + { + RegisteredPeerManager::removeFlag($session->getPeerUuid(), PeerFlags::VER_SOLVE_IMAGE_CAPTCHA); + } + + return $rpcRequest->produceResponse($result); + } + catch (DatabaseOperationException $e) + { + throw new StandardException("There was an unexpected error while trying to answer the captcha", StandardError::INTERNAL_SERVER_ERROR, $e); + } + } +} \ No newline at end of file diff --git a/src/Socialbox/Classes/StandardMethods/VerificationGetCaptcha.php b/src/Socialbox/Classes/StandardMethods/VerificationGetImageCaptcha.php similarity index 98% rename from src/Socialbox/Classes/StandardMethods/VerificationGetCaptcha.php rename to src/Socialbox/Classes/StandardMethods/VerificationGetImageCaptcha.php index 854be86..b99a358 100644 --- a/src/Socialbox/Classes/StandardMethods/VerificationGetCaptcha.php +++ b/src/Socialbox/Classes/StandardMethods/VerificationGetImageCaptcha.php @@ -17,7 +17,7 @@ use Socialbox\Objects\ClientRequest; use Socialbox\Objects\RpcRequest; use Socialbox\Objects\Standard\ImageCaptcha; -class VerificationGetCaptcha extends Method +class VerificationGetImageCaptcha extends Method { /** * @inheritDoc diff --git a/src/Socialbox/Enums/StandardMethods.php b/src/Socialbox/Enums/StandardMethods.php index c13b9c4..6eb44cc 100644 --- a/src/Socialbox/Enums/StandardMethods.php +++ b/src/Socialbox/Enums/StandardMethods.php @@ -3,7 +3,8 @@ namespace Socialbox\Enums; use Socialbox\Classes\StandardMethods\CreateSession; -use Socialbox\Classes\StandardMethods\VerificationGetCaptcha; +use Socialbox\Classes\StandardMethods\VerificationAnswerImageCaptcha; +use Socialbox\Classes\StandardMethods\VerificationGetImageCaptcha; use Socialbox\Classes\StandardMethods\GetMe; use Socialbox\Classes\StandardMethods\Ping; use Socialbox\Classes\StandardMethods\Register; @@ -18,7 +19,8 @@ enum StandardMethods : string case CREATE_SESSION = 'createSession'; case REGISTER = 'register'; case GET_ME = 'getMe'; - case VERIFICATION_GET_CAPTCHA = 'verificationGetCaptcha'; + case VERIFICATION_GET_IMAGE_CAPTCHA = 'verificationGetImageCaptcha'; + case VERIFICATION_ANSWER_IMAGE_CAPTCHA = 'verificationAnswerImageCaptcha'; /** * @param ClientRequest $request @@ -34,7 +36,8 @@ enum StandardMethods : string self::CREATE_SESSION => CreateSession::execute($request, $rpcRequest), self::REGISTER => Register::execute($request, $rpcRequest), self::GET_ME => GetMe::execute($request, $rpcRequest), - self::VERIFICATION_GET_CAPTCHA => VerificationGetCaptcha::execute($request, $rpcRequest), + self::VERIFICATION_GET_IMAGE_CAPTCHA => VerificationGetImageCaptcha::execute($request, $rpcRequest), + self::VERIFICATION_ANSWER_IMAGE_CAPTCHA => VerificationAnswerImageCaptcha::execute($request, $rpcRequest), }; } } \ No newline at end of file