Added the ability to trust signing keys & resolve signing keys for peers, minor improvements and added new standard error "CONFLICT"
This commit is contained in:
parent
4e22a8bacd
commit
330e7f876b
14 changed files with 427 additions and 42 deletions
|
@ -150,6 +150,7 @@
|
|||
// Server Policies
|
||||
// The maximum number of signing keys a peer can register onto the server at once
|
||||
$config->setDefault('policies.max_signing_keys', 20);
|
||||
$config->setDefault('policies.max_contact_signing_keys', 50);
|
||||
// The amount of time in seconds it takes before a session is considered expired due to inactivity
|
||||
// Default: 12hours
|
||||
$config->setDefault('policies.session_inactivity_expires', 43200);
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
class PoliciesConfiguration
|
||||
{
|
||||
private int $maxSigningKeys;
|
||||
private int $maxContactSigningKeys;
|
||||
private int $sessionInactivityExpires;
|
||||
private int $imageCaptchaExpires;
|
||||
private int $peerSyncInterval;
|
||||
|
@ -37,6 +38,7 @@
|
|||
public function __construct(array $data)
|
||||
{
|
||||
$this->maxSigningKeys = $data['max_signing_keys'];
|
||||
$this->maxContactSigningKeys = $data['max_contact_signing_keys'];
|
||||
$this->sessionInactivityExpires = $data['session_inactivity_expires'];
|
||||
$this->imageCaptchaExpires = $data['image_captcha_expires'];
|
||||
$this->peerSyncInterval = $data['peer_sync_interval'];
|
||||
|
@ -61,6 +63,11 @@
|
|||
return $this->maxSigningKeys;
|
||||
}
|
||||
|
||||
public function getMaxContactSigningKeys(): int
|
||||
{
|
||||
return $this->maxContactSigningKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum amount of seconds before the session is considered expired due to inactivity
|
||||
*
|
||||
|
|
|
@ -2,7 +2,7 @@ create table signing_keys
|
|||
(
|
||||
peer_uuid varchar(36) not null comment 'The UUID of the peer',
|
||||
uuid varchar(36) default uuid() not null comment 'The UUID of the key record',
|
||||
name varchar(64) null comment 'Optional. User provided name for the key',
|
||||
name varchar(64) not null comment 'Optional. User provided name for the key',
|
||||
public_key varchar(64) not null comment 'The Public Signature Key',
|
||||
state enum ('ACTIVE', 'EXPIRED') default 'ACTIVE' not null comment 'The state of the public key',
|
||||
expires timestamp null comment 'The Timestamp for when this key expires, null = Never expires',
|
||||
|
|
|
@ -4,14 +4,18 @@
|
|||
|
||||
use InvalidArgumentException;
|
||||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Classes\Configuration;
|
||||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Exceptions\DatabaseOperationException;
|
||||
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
|
||||
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
|
||||
use Socialbox\Exceptions\Standard\StandardRpcException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\ContactManager;
|
||||
use Socialbox\Objects\ClientRequest;
|
||||
use Socialbox\Objects\PeerAddress;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
use Socialbox\Socialbox;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
class AddressBookTrustSignature extends Method
|
||||
|
@ -23,7 +27,7 @@
|
|||
{
|
||||
if(!$rpcRequest->containsParameter('peer'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing required peer parameter');
|
||||
throw new MissingRpcArgumentException('peer');
|
||||
}
|
||||
|
||||
try
|
||||
|
@ -32,12 +36,12 @@
|
|||
}
|
||||
catch(InvalidArgumentException $e)
|
||||
{
|
||||
throw new StandardRpcException('Invalid peer address', StandardError::RPC_INVALID_ARGUMENTS, $e);
|
||||
throw new InvalidRpcArgumentException('peer', $e->getMessage());
|
||||
}
|
||||
|
||||
if(!$rpcRequest->containsParameter('uuid'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'uuid' parameter");
|
||||
throw new MissingRpcArgumentException('uuid');
|
||||
}
|
||||
|
||||
try
|
||||
|
@ -46,24 +50,54 @@
|
|||
}
|
||||
catch(InvalidArgumentException $e)
|
||||
{
|
||||
throw new StandardRpcException('Invalid UUID', StandardError::RPC_INVALID_ARGUMENTS, $e);
|
||||
throw new InvalidRpcArgumentException('uuid', $e->getMessage());
|
||||
}
|
||||
|
||||
$signingKey = Socialbox::resolvePeerSignature($address, $uuid);
|
||||
|
||||
try
|
||||
{
|
||||
// Check if the contact already exists
|
||||
$peer = $request->getPeer();
|
||||
if(ContactManager::isContact($peer, $address))
|
||||
if(!ContactManager::isContact($peer, $address))
|
||||
{
|
||||
|
||||
ContactManager::createContact($peer, $address);
|
||||
}
|
||||
|
||||
// Create the contact
|
||||
ContactManager::updateContactRelationship($peer, $address, $relationship);
|
||||
$contact = ContactManager::getContact($peer, $address);
|
||||
|
||||
if(ContactManager::contactGetSigningKeysCount($contact) > Configuration::getPoliciesConfiguration()->getMaxContactSigningKeys())
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The contact has exceeded the maximum amount of trusted signatures');
|
||||
}
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to update contact relationship', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
throw new StandardRpcException('Failed to check contact state with calling peer', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
ContactManager::addContactSigningKey($contact, $signingKey);
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardRpcException('Failed to trust contact signature', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
// Return success
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Enums\Types\ContactRelationshipType;
|
||||
use Socialbox\Exceptions\DatabaseOperationException;
|
||||
use Socialbox\Exceptions\Standard\InvalidRpcArgumentException;
|
||||
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
|
||||
use Socialbox\Exceptions\Standard\StandardRpcException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\ContactManager;
|
||||
|
@ -23,7 +25,7 @@
|
|||
{
|
||||
if(!$rpcRequest->containsParameter('peer'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing required peer parameter');
|
||||
throw new MissingRpcArgumentException('peer');
|
||||
}
|
||||
|
||||
try
|
||||
|
@ -32,17 +34,17 @@
|
|||
}
|
||||
catch(InvalidArgumentException $e)
|
||||
{
|
||||
throw new StandardRpcException('Invalid peer address', StandardError::RPC_INVALID_ARGUMENTS, $e);
|
||||
throw new InvalidRpcArgumentException('peer', 'Invalid peer address');
|
||||
}
|
||||
|
||||
if(!$rpcRequest->containsParameter('relationship'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing required relationship parameter');
|
||||
throw new MissingRpcArgumentException('relationship');
|
||||
}
|
||||
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
|
||||
if($relationship === null)
|
||||
{
|
||||
throw new StandardRpcException('Invalid relationship type', StandardError::RPC_INVALID_ARGUMENTS);
|
||||
throw new InvalidRpcArgumentException('relationship', 'Invalid relationship type');
|
||||
}
|
||||
|
||||
try
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Classes\Configuration;
|
||||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
|
||||
use Socialbox\Exceptions\Standard\StandardRpcException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\SigningKeysManager;
|
||||
|
@ -22,7 +23,7 @@
|
|||
{
|
||||
if(!$rpcRequest->containsParameter('public_key'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'public_key' parameter");
|
||||
throw new MissingRpcArgumentException('public_key');
|
||||
}
|
||||
|
||||
$expires = null;
|
||||
|
@ -31,6 +32,11 @@
|
|||
$expires = (int)$rpcRequest->getParameter('expires');
|
||||
}
|
||||
|
||||
if(!$rpcRequest->containsParameter('name'))
|
||||
{
|
||||
throw new MissingRpcArgumentException('name');
|
||||
}
|
||||
|
||||
$name = null;
|
||||
if($rpcRequest->containsParameter('name') && $rpcRequest->getParameter('name') !== null)
|
||||
{
|
||||
|
@ -46,7 +52,7 @@
|
|||
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The maximum number of signing keys has been reached');
|
||||
}
|
||||
|
||||
$uuid = SigningKeysManager::addSigningKey($peerUuid, $rpcRequest->getParameter('public_key'), $expires, $name);
|
||||
$uuid = SigningKeysManager::addSigningKey($peerUuid, $rpcRequest->getParameter('public_key'), $name, $expires);
|
||||
}
|
||||
catch(InvalidArgumentException $e)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue