socialbox-php/src/Socialbox/Classes/StandardMethods/AddressBook/AddressBookTrustSignature.php

106 lines
3.8 KiB
PHP
Raw Normal View History

2025-01-29 15:23:16 -05:00
<?php
2025-01-29 15:52:38 -05:00
namespace Socialbox\Classes\StandardMethods\AddressBook;
2025-01-29 15:23:16 -05:00
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
2025-01-29 15:23:16 -05:00
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
use Socialbox\Exceptions\Standard\StandardRpcException;
2025-01-29 15:23:16 -05:00
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\ContactManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\RpcRequest;
use Socialbox\Socialbox;
2025-01-29 15:23:16 -05:00
use Symfony\Component\Uid\Uuid;
class AddressBookTrustSignature extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('peer'))
{
throw new MissingRpcArgumentException('peer');
2025-01-29 15:23:16 -05:00
}
try
{
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('peer', $e->getMessage());
2025-01-29 15:23:16 -05:00
}
if(!$rpcRequest->containsParameter('uuid'))
{
throw new MissingRpcArgumentException('uuid');
2025-01-29 15:23:16 -05:00
}
try
{
$uuid = Uuid::fromString($rpcRequest->getParameter('uuid'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('uuid', $e->getMessage());
2025-01-29 15:23:16 -05:00
}
$signingKey = Socialbox::resolvePeerSignature($address, $uuid);
2025-01-29 15:23:16 -05:00
try
{
// Check if the contact already exists
$peer = $request->getPeer();
if(!ContactManager::isContact($peer, $address))
{
ContactManager::createContact($peer, $address);
}
$contact = ContactManager::getContact($peer, $address);
if(ContactManager::contactGetSigningKeysCount($contact) > Configuration::getPoliciesConfiguration()->getMaxContactSigningKeys())
2025-01-29 15:23:16 -05:00
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The contact has exceeded the maximum amount of trusted signatures');
}
}
catch (DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to check contact state with calling peer', StandardError::INTERNAL_SERVER_ERROR, $e);
}
2025-01-29 15:23:16 -05:00
if($signingKey === null)
{
return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The requested signature key was not found');
}
try
{
if(ContactManager::contactSigningKeyUuidExists($contact, $signingKey->getUuid()))
{
return $rpcRequest->produceResponse(false);
}
if(ContactManager::contactSigningKeyExists($contact, $signingKey->getPublicKey()))
{
return $rpcRequest->produceResponse(false);
2025-01-29 15:23:16 -05:00
}
ContactManager::addContactSigningKey($contact, $signingKey);
2025-01-29 15:23:16 -05:00
}
catch (DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to trust contact signature', StandardError::INTERNAL_SERVER_ERROR, $e);
2025-01-29 15:23:16 -05:00
}
// Return success
return $rpcRequest->produceResponse(true);
}
}