Made message signing in Cryptography use SHA512 as the message content for... #1
1 changed files with 133 additions and 46 deletions
|
@ -2,11 +2,14 @@
|
|||
|
||||
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;
|
||||
|
@ -15,8 +18,9 @@
|
|||
use Socialbox\Objects\ClientRequest;
|
||||
use Socialbox\Objects\Database\EncryptionChannelRecord;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
use Socialbox\Socialbox;
|
||||
|
||||
class EncryptionChannelAcknowledge extends Method
|
||||
class EncryptionChannelAcknowledgeMessage extends Method
|
||||
{
|
||||
|
||||
/**
|
||||
|
@ -33,6 +37,19 @@
|
|||
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');
|
||||
|
@ -48,6 +65,80 @@
|
|||
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 acknowledge 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::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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
@ -71,64 +162,60 @@
|
|||
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The encryption channel is not opened');
|
||||
}
|
||||
|
||||
if(!$rpcRequest->containsParameter('message_uuid'))
|
||||
{
|
||||
throw new MissingRpcArgumentException('message_uuid');
|
||||
}
|
||||
|
||||
if(is_array($rpcRequest->getParameter('message_uuid')))
|
||||
{
|
||||
return self::handleMultipleMessages($rpcRequest, $encryptionChannel);
|
||||
}
|
||||
elseif(is_string($rpcRequest->getParameter('message_uuid')))
|
||||
{
|
||||
return self::handleSingleMessage($rpcRequest, $encryptionChannel);
|
||||
}
|
||||
|
||||
return $rpcRequest->produceError(StandardError::BAD_REQUEST, 'The message_uuid parameter must be a string or an array of strings');
|
||||
}
|
||||
|
||||
private static function handleSingleMessage(RpcRequest $rpcRequest, EncryptionChannelRecord $encryptionChannel)
|
||||
{
|
||||
if(!Validator::validateUuid($rpcRequest->getParameter('message_uuid')))
|
||||
{
|
||||
throw new InvalidRpcArgumentException('message_uuid', 'The given message uuid is not a valid UUID V4');
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
EncryptionChannelManager::acknowledgeMessage($encryptionChannel->getUuid(), $rpcRequest->getParameter('message_uuid'));
|
||||
$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);
|
||||
}
|
||||
|
||||
return $rpcRequest->produceResponse(true);
|
||||
}
|
||||
|
||||
private static function handleMultipleMessages(RpcRequest $rpcRequest, EncryptionChannelRecord $encryptionChannel)
|
||||
{
|
||||
$messageUuids = $rpcRequest->getParameter('message_uuid');
|
||||
|
||||
foreach($messageUuids as $messageUuid)
|
||||
if($message->getOwner($encryptionChannel)->isExternal())
|
||||
{
|
||||
if(!Validator::validateUuid($messageUuid))
|
||||
try
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::BAD_REQUEST, sprintf('The message uuid %s is not a valid UUID V4', $messageUuid));
|
||||
$rpcClient = Socialbox::getExternalSession($message->getOwner($encryptionChannel)->getDomain());
|
||||
$rpcClient->encryptionChannelAcknowledgeMessage(
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
EncryptionChannelManager::acknowledgeMessagesBatch($encryptionChannel->getUuid(), $messageUuids);
|
||||
}
|
||||
catch(DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to acknowledge the messages', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
|
||||
return $rpcRequest->produceResponse(true);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue