2025-01-06 01:37:51 -05:00
|
|
|
<?php
|
|
|
|
|
2025-01-29 15:52:38 -05:00
|
|
|
namespace Socialbox\Classes\StandardMethods\Verification;
|
2025-01-06 01:37:51 -05:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Socialbox\Abstracts\Method;
|
|
|
|
use Socialbox\Classes\Cryptography;
|
2025-01-06 14:58:53 -05:00
|
|
|
use Socialbox\Enums\Flags\SessionFlags;
|
2025-01-06 01:37:51 -05:00
|
|
|
use Socialbox\Enums\StandardError;
|
|
|
|
use Socialbox\Exceptions\CryptographyException;
|
2025-01-30 00:27:06 -05:00
|
|
|
use Socialbox\Exceptions\Standard\StandardException;
|
2025-01-06 01:37:51 -05:00
|
|
|
use Socialbox\Interfaces\SerializableInterface;
|
|
|
|
use Socialbox\Managers\PasswordManager;
|
2025-01-06 14:58:53 -05:00
|
|
|
use Socialbox\Managers\SessionManager;
|
2025-01-06 01:37:51 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2025-01-06 14:58:53 -05:00
|
|
|
$session = $request->getSession();
|
|
|
|
if(!$session->flagExists(SessionFlags::VER_PASSWORD))
|
|
|
|
{
|
|
|
|
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Password verification is not required at this time');
|
|
|
|
}
|
|
|
|
|
2025-01-06 01:37:51 -05:00
|
|
|
try
|
|
|
|
{
|
2025-01-06 14:58:53 -05:00
|
|
|
$result = PasswordManager::verifyPassword($request->getPeer()->getUuid(), $rpcRequest->getParameter('password'));
|
2025-01-07 14:16:29 -05:00
|
|
|
|
|
|
|
if($result)
|
|
|
|
{
|
|
|
|
SessionManager::updateFlow($request->getSession(), [SessionFlags::VER_PASSWORD]);
|
|
|
|
}
|
2025-01-06 01:37:51 -05:00
|
|
|
}
|
2025-01-06 01:44:16 -05:00
|
|
|
catch (CryptographyException)
|
2025-01-06 01:37:51 -05:00
|
|
|
{
|
|
|
|
return $rpcRequest->produceResponse(false);
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
throw new StandardException('Failed to verify password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
|
|
|
|
}
|
2025-01-06 14:58:53 -05:00
|
|
|
|
|
|
|
return $rpcRequest->produceResponse($result);
|
2025-01-06 01:37:51 -05:00
|
|
|
}
|
|
|
|
}
|