Add EncryptionChannelRejectMessage class to handle message rejection in encryption channels
This commit is contained in:
parent
ec44e85a5e
commit
f9185b5969
1 changed files with 221 additions and 0 deletions
|
@ -0,0 +1,221 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\StandardMethods\EncryptionChannel;
|
||||
|
||||
use Exception;
|
||||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Classes\Logger;
|
||||
use Socialbox\Classes\Validator;
|
||||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Enums\Status\EncryptionChannelStatus;
|
||||
use Socialbox\Exceptions\DatabaseOperationException;
|
||||
use Socialbox\Exceptions\RpcException;
|
||||
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
|
||||
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
|
||||
use Socialbox\Exceptions\Standard\StandardRpcException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\EncryptionChannelManager;
|
||||
use Socialbox\Objects\ClientRequest;
|
||||
use Socialbox\Objects\Database\EncryptionChannelRecord;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
use Socialbox\Socialbox;
|
||||
|
||||
class EncryptionChannelRejectMessage extends Method
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||
{
|
||||
if(!$rpcRequest->containsParameter('channel_uuid'))
|
||||
{
|
||||
throw new MissingRpcArgumentException('channel_uuid');
|
||||
}
|
||||
elseif(!Validator::validateUuid($rpcRequest->getParameter('channel_uuid')))
|
||||
{
|
||||
throw new InvalidRpcArgumentException('channel_uuid', 'The given channel uuid is not a valid UUID V4');
|
||||
}
|
||||
|
||||
if(!$rpcRequest->containsParameter('message_uuid'))
|
||||
{
|
||||
throw new MissingRpcArgumentException('message_uuid');
|
||||
}
|
||||
elseif(!is_string($rpcRequest->getParameter('message_uuid')))
|
||||
{
|
||||
throw new InvalidRpcArgumentException('message_uuid', 'Must be type string');
|
||||
}
|
||||
elseif(!Validator::validateUuid($rpcRequest->getParameter('message_uuid')))
|
||||
{
|
||||
throw new InvalidRpcArgumentException('message_uuid', 'Invalid message UUID V4');
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$channelUuid = $rpcRequest->getParameter('channel_uuid');
|
||||
$encryptionChannel = EncryptionChannelManager::getChannel($channelUuid);
|
||||
}
|
||||
catch(DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to retrieve the encryption channel', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
if($encryptionChannel === null)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The encryption channel does not exist');
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if ($request->isExternal())
|
||||
{
|
||||
return self::handleExternal($request, $rpcRequest, $encryptionChannel);
|
||||
}
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to reject the message', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
return self::handleInternal($request, $rpcRequest, $encryptionChannel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the external execution of the method.
|
||||
*
|
||||
* @param ClientRequest $request The client request instance.
|
||||
* @param RpcRequest $rpcRequest The RPC request instance.
|
||||
* @param EncryptionChannelRecord $encryptionChannel The encryption channel record.
|
||||
* @return SerializableInterface|null The response to the request.
|
||||
* @throws StandardRpcException If an error occurs.
|
||||
*/
|
||||
public static function handleExternal(ClientRequest $request, RpcRequest $rpcRequest, EncryptionChannelRecord $encryptionChannel): ?SerializableInterface
|
||||
{
|
||||
if($request->getIdentifyAs() === null)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::BAD_REQUEST, 'The IdentifyAs header is missing');
|
||||
}
|
||||
|
||||
$requestingPeerAddress = $request->getIdentifyAs();
|
||||
if(!$encryptionChannel->isParticipant($requestingPeerAddress))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The encryption channel is not accessible');
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$message = EncryptionChannelManager::getMessageRecord($rpcRequest->getParameter('channel_uuid'), $rpcRequest->getParameter('message_uuid'));
|
||||
|
||||
if($message === null)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The message does not exist');
|
||||
}
|
||||
|
||||
if($message->getReceiver($encryptionChannel)->getAddress() !== $requestingPeerAddress)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The message is not for the requesting peer');
|
||||
}
|
||||
|
||||
EncryptionChannelManager::rejectMessage(
|
||||
$rpcRequest->getParameter('channel_uuid'), $rpcRequest->getParameter('message_uuid')
|
||||
);
|
||||
}
|
||||
catch(DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to reject the message', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
return $rpcRequest->produceResponse(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the internal execution of the method.
|
||||
*
|
||||
* @param ClientRequest $request The client request instance.
|
||||
* @param RpcRequest $rpcRequest The RPC request instance.
|
||||
* @param EncryptionChannelRecord $encryptionChannel The encryption channel record.
|
||||
* @return SerializableInterface|null The response to the request.
|
||||
* @throws StandardRpcException If an error occurs.
|
||||
*/
|
||||
public static function handleInternal(ClientRequest $request, RpcRequest $rpcRequest, EncryptionChannelRecord $encryptionChannel): ?SerializableInterface
|
||||
{
|
||||
try
|
||||
{
|
||||
$requestingPeer = $request->getPeer();
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to retrieve the peer', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
if($requestingPeer === null)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The peer is not authorized');
|
||||
}
|
||||
|
||||
if(!$encryptionChannel->isParticipant($requestingPeer->getAddress()))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The encryption channel is not accessible');
|
||||
}
|
||||
elseif($encryptionChannel->getStatus() !== EncryptionChannelStatus::OPENED)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The encryption channel is not opened');
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$message = EncryptionChannelManager::getMessageRecord($rpcRequest->getParameter('channel_uuid'), $rpcRequest->getParameter('message_uuid'));
|
||||
|
||||
if($message === null)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The message does not exist');
|
||||
}
|
||||
|
||||
if($message->getReceiver($encryptionChannel)->getAddress() !== $requestingPeer->getAddress())
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The message is not for the requesting peer');
|
||||
}
|
||||
|
||||
EncryptionChannelManager::acknowledgeMessage(
|
||||
$rpcRequest->getParameter('channel_uuid'), $rpcRequest->getParameter('message_uuid')
|
||||
);
|
||||
}
|
||||
catch(DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to acknowledge the message', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
if($message->getOwner($encryptionChannel)->isExternal())
|
||||
{
|
||||
try
|
||||
{
|
||||
$rpcClient = Socialbox::getExternalSession($message->getOwner($encryptionChannel)->getDomain());
|
||||
$rpcClient->encryptionChannelRejectMessage(
|
||||
channelUuid: $rpcRequest->getParameter('channel_uuid'),
|
||||
messageUuid: $rpcRequest->getParameter('message_uuid'),
|
||||
identifiedAs: $requestingPeer->getAddress()
|
||||
);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
try
|
||||
{
|
||||
EncryptionChannelManager::rejectMessage($rpcRequest->getParameter('channel_uuid'), $rpcRequest->getParameter('message_uuid'), true);
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
Logger::getLogger()->error('Error rejecting message as server', $e);
|
||||
}
|
||||
|
||||
if($e instanceof RpcException)
|
||||
{
|
||||
throw StandardRpcException::fromRpcException($e);
|
||||
}
|
||||
|
||||
throw new StandardRpcException('Failed to acknowledge the message with the external server', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
}
|
||||
|
||||
return $rpcRequest->produceResponse(true);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue