Refactor SettingsSignatureExists and SigningKeysManager to improve UUID validation and update parameter naming for clarity

https://github.com/nosial/Socialbox-PHP/issues/67
This commit is contained in:
netkas 2025-03-12 15:10:07 -04:00
parent d47251c251
commit c3b1ee799a
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
3 changed files with 22 additions and 23 deletions

View file

@ -2,13 +2,9 @@
namespace Socialbox\Classes\StandardMethods\Settings;
use Exception;
use InvalidArgumentException;
use ncc\ThirdParty\Symfony\Uid\Uuid;
use Socialbox\Abstracts\Method;
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;
@ -23,28 +19,20 @@
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('uuid'))
if(!$rpcRequest->containsParameter('signature_uuid'))
{
throw new MissingRpcArgumentException('uuid');
throw new MissingRpcArgumentException('signature_uuid');
}
$signatureUuid = (string)$rpcRequest->getParameter('signature_uuid');
try
{
$uuid = Uuid::fromString($rpcRequest->getParameter('uuid'));
}
catch(InvalidArgumentException)
{
throw new InvalidRpcArgumentException('uuid');
}
try
{
return $rpcRequest->produceResponse(SigningKeysManager::signingKeyExists($request->getPeer()->getUuid(), $uuid));
return $rpcRequest->produceResponse(SigningKeysManager::signingKeyExists($request->getPeer()->getUuid(), $signatureUuid));
}
catch(DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to check the signing key existence', StandardError::INTERNAL_SERVER_ERROR, $e);
}
}
}

View file

@ -8,6 +8,7 @@
use PDOException;
use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Database;
use Socialbox\Classes\Validator;
use Socialbox\Enums\SigningKeyState;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\DatabaseOperationException;
@ -199,16 +200,26 @@
* Checks if a signing key exists in the database using the provided UUID.
*
* @param string $peerUuid The UUID of the peer associated with the signing key.
* @param string $uuid The UUID of the signing key to check.
* @param string $signatureUuid The UUID of the signing key to check.
* @return bool True if the signing key exists, false otherwise.
* @throws DatabaseOperationException If a database error occurs during the operation.
*/
public static function signingKeyExists(string $peerUuid, string $uuid): bool
public static function signingKeyExists(string $peerUuid, string $signatureUuid): bool
{
if(!Validator::validateUuid($peerUuid))
{
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
}
if(!Validator::validateUuid($signatureUuid))
{
throw new InvalidArgumentException('The given signature UUID is not a valid UUID V4');
}
try
{
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM signing_keys WHERE uuid=:uuid AND peer_uuid=:peer_uuid");
$statement->bindParam(':uuid', $uuid);
$statement->bindParam(':uuid', $signatureUuid);
$statement->bindParam(':peer_uuid', $peerUuid);
$statement->execute();

View file

@ -1168,15 +1168,15 @@
/**
* Checks if a signature exists in the peer's profile, returns True if the signature exists.
*
* @param string $uuid The UUID of the signature to check for it's existence
* @param string $signatureUuid The UUID of the signature to check for it's existence
* @return bool Returns True if the signature exists, False otherwise
* @throws RpcException Thrown if there was an error with the RPC request
*/
public function settingsSignatureExists(string $uuid): bool
public function settingsSignatureExists(string $signatureUuid): bool
{
return $this->sendRequest(
new RpcRequest(StandardMethods::SETTINGS_SIGNATURE_EXISTS, parameters: [
'uuid' => $uuid
'signature_uuid' => $signatureUuid
])
)->getResponse()->getResult();
}