diff --git a/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelSend.php b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelSend.php new file mode 100644 index 0000000..14f7a83 --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelSend.php @@ -0,0 +1,237 @@ +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'); + } + + try + { + if ($request->isExternal()) + { + return self::executeExternal($request, $rpcRequest); + } + } + catch (DatabaseOperationException $e) + { + throw new StandardRpcException('An error occurred while checking the request type', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + return self::executeInternal($request, $rpcRequest); + } + + /** + * @param ClientRequest $request + * @param RpcRequest $rpcRequest + * @return SerializableInterface + * @throws StandardRpcException + */ + private static function executeInternal(ClientRequest $request, RpcRequest $rpcRequest): SerializableInterface + { + 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 + { + $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'); + } + + if(!$rpcRequest->containsParameter('checksum')) + { + throw new MissingRpcArgumentException('checksum'); + } + elseif(!Cryptography::validateSha512($rpcRequest->getParameter('checksum'))) + { + throw new InvalidRpcArgumentException('checksum', 'The given checksum is not a valid SHA-512 checksum'); + } + + if(!$rpcRequest->containsParameter('data')) + { + throw new MissingRpcArgumentException('data'); + } + + try + { + $messageUuid = Uuid::v4()->toRfc4122(); + $messageTimestamp = time(); + + EncryptionChannelManager::sendMessage( + channelUuid: $channelUuid, + recipient: $encryptionChannel->determineRecipient($requestingPeer->getAddress()), + checksum: $rpcRequest->getParameter('checksum'), + data: $rpcRequest->getParameter('data'), + messageUuid: $messageUuid, + messageTimestamp: $messageTimestamp + ); + } + catch(DatabaseOperationException $e) + { + throw new StandardRpcException('Failed to send the message', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + if($encryptionChannel->determineReceiver($requestingPeer->getAddress())->isExternal()) + { + try + { + $rpcClient = Socialbox::getExternalSession($encryptionChannel->determineReceiver($requestingPeer->getAddress())->getDomain()); + $rpcClient->encryptionChannelSend( + channelUuid: $rpcRequest->getParameter('channel_uuid'), + checksum: $rpcRequest->getParameter('checksum'), + data: $rpcRequest->getParameter('data'), + identifiedAs: $requestingPeer->getAddress(), + messageUuid: $messageUuid, + timestamp: $messageTimestamp + ); + } + catch(Exception $e) + { + if($e instanceof RpcException) + { + throw StandardRpcException::fromRpcException($e); + } + + throw new StandardRpcException('There was an error while trying to notify the external server of the encryption channel', StandardError::INTERNAL_SERVER_ERROR, $e); + } + } + + return $rpcRequest->produceResponse(); + } + + /** + * @param ClientRequest $request + * @param RpcRequest $rpcRequest + * @return SerializableInterface + * @throws StandardRpcException + */ + private static function executeExternal(ClientRequest $request, RpcRequest $rpcRequest): SerializableInterface + { + if($request->getIdentifyAs() === null) + { + return $rpcRequest->produceError(StandardError::BAD_REQUEST, 'The IdentifyAs header is required'); + } + + 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'); + } + elseif(!$encryptionChannel->isParticipant($request->getIdentifyAs())) + { + return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The encryption channel is not accessible'); + } + + if(!$rpcRequest->containsParameter('checksum')) + { + throw new MissingRpcArgumentException('checksum'); + } + + if(!$rpcRequest->containsParameter('data')) + { + throw new MissingRpcArgumentException('data'); + } + + if(!$rpcRequest->containsParameter('message_uuid')) + { + throw new MissingRpcArgumentException('message_uuid'); + } + + if(!$rpcRequest->containsParameter('timestamp')) + { + throw new MissingRpcArgumentException('timestamp'); + } + elseif(!is_int($rpcRequest->getParameter('timestamp'))) + { + throw new InvalidRpcArgumentException('timestamp', 'The given timestamp must be type integer'); + } + + try + { + EncryptionChannelManager::sendMessage( + channelUuid: $channelUuid, + recipient: $encryptionChannel->determineRecipient($request->getIdentifyAs()), + checksum: $rpcRequest->getParameter('checksum'), + data: $rpcRequest->getParameter('data'), + messageUuid: $rpcRequest->getParameter('message_uuid'), + messageTimestamp: (int)$rpcRequest->getParameter('timestamp') + ); + } + catch(DatabaseOperationException $e) + { + throw new StandardRpcException('Failed to send the message', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + + return $rpcRequest->produceResponse(true); + } + } \ No newline at end of file