Add signing key management functionality
This commit is contained in:
parent
d732c89632
commit
e4b9a08972
7 changed files with 693 additions and 1 deletions
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\StandardMethods;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Classes\Configuration;
|
||||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Exceptions\StandardException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\SigningKeysManager;
|
||||
use Socialbox\Objects\ClientRequest;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
|
||||
class SettingsAddSigningKey extends Method
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||
{
|
||||
if(!$rpcRequest->containsParameter('public_key'))
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'public_key' parameter");
|
||||
}
|
||||
|
||||
$expires = null;
|
||||
if($rpcRequest->containsParameter('expires'))
|
||||
{
|
||||
$expires = (int)$rpcRequest->getParameter('expires');
|
||||
}
|
||||
|
||||
$name = null;
|
||||
if($rpcRequest->containsParameter('name'))
|
||||
{
|
||||
$name = $rpcRequest->getParameter('name');
|
||||
}
|
||||
|
||||
$peerUuid = $request->getPeer()->getUuid();
|
||||
|
||||
try
|
||||
{
|
||||
if(SigningKeysManager::getSigningKeyCount($peerUuid) >= Configuration::getPoliciesConfiguration()->getMaxSigningKeys())
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The maximum number of signing keys has been reached');
|
||||
}
|
||||
|
||||
$uuid = SigningKeysManager::addSigningKey($peerUuid, $rpcRequest->getParameter('public_key'), $expires, $name);
|
||||
}
|
||||
catch(InvalidArgumentException $e)
|
||||
{
|
||||
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, $e->getMessage());
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
throw new StandardException('Failed to add the signing key', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
return $rpcRequest->produceResponse($uuid);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\StandardMethods;
|
||||
|
||||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Exceptions\DatabaseOperationException;
|
||||
use Socialbox\Exceptions\StandardException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\SigningKeysManager;
|
||||
use Socialbox\Objects\ClientRequest;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
|
||||
class SettingsGetSigningKeys extends Method
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||
{
|
||||
try
|
||||
{
|
||||
$keys = SigningKeysManager::getSigningKeys($request->getPeer()->getUuid());
|
||||
}
|
||||
catch (DatabaseOperationException $e)
|
||||
{
|
||||
throw new StandardException('Failed to get the signing keys', StandardError::INTERNAL_SERVER_ERROR, $e);
|
||||
}
|
||||
|
||||
if(empty($keys))
|
||||
{
|
||||
// Return an empty array if the results are empty
|
||||
return $rpcRequest->produceResponse([]);
|
||||
}
|
||||
|
||||
// Return the signing keys as an array of standard objects
|
||||
return $rpcRequest->produceResponse(array_map(fn($key) => $key->toStandard(), $keys));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue