Compare commits

...

7 commits

Author SHA1 Message Date
604f75f5c8
Update addressBookAddContact method to return contact UUID instead of boolean
Some checks are pending
CI / check-phpunit (push) Waiting to run
CI / check-phpdoc (push) Waiting to run
CI / generate-phpdoc (push) Blocked by required conditions
CI / test (push) Blocked by required conditions
CI / release-documentation (push) Blocked by required conditions
CI / release (push) Waiting to run
CI / debug (push) Waiting to run
CI / release_executable (push) Waiting to run
CI / debug_executable (push) Waiting to run
CI / release-artifacts (push) Blocked by required conditions
2025-03-25 12:59:09 -04:00
4a7fbd9970
Handle null expiration in KnownSigningKey to set default value 2025-03-25 12:59:03 -04:00
e4f3d50852
Fix indentation in Helper.php for improved code readability 2025-03-25 12:58:58 -04:00
52985b6308
Enhance logging in ContactManager and improve error messages for invalid peer addresses 2025-03-25 12:58:53 -04:00
97ca09d7dd
Increase signature_key length in contact_known_keys.sql to accommodate larger keys 2025-03-25 12:58:48 -04:00
db9fa3f92e
Add signature management methods to Contact class for enhanced key handling 2025-03-25 12:58:42 -04:00
c981ed3d9d
Refactor AddressBookAddContact to improve relationship parameter handling and return contact UUID on success 2025-03-25 12:58:21 -04:00
7 changed files with 110 additions and 21 deletions

View file

@ -3,7 +3,7 @@ create table contacts_known_keys
contact_uuid varchar(36) not null comment 'The Unique Universal Identifier of the personal contact that this record is associated with',
signature_uuid varchar(36) not null comment 'The Unique Universal Identifier for the signature key',
signature_name varchar(64) not null comment 'The name of the signing key',
signature_key varchar(32) not null comment 'The public signing key',
signature_key varchar(64) not null comment 'The public signing key',
expires timestamp null comment 'The Timestamp for when this key expires, null means never',
created timestamp not null comment 'The Timestamp for when this key was created',
trusted_on timestamp default current_timestamp() not null comment 'The Timestamp for when the peer trusted this key',

View file

@ -2,7 +2,6 @@
namespace Socialbox\Classes\StandardMethods\AddressBook;
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\ReservedUsernames;
use Socialbox\Enums\StandardError;
@ -35,7 +34,7 @@
$peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
if($rpcRequest->containsParameter('relationship') && $rpcRequest->getParameter('relationship') !== null)
if($rpcRequest->containsParameter('relationship'))
{
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
if($relationship === null)
@ -66,11 +65,11 @@
// Check if the contact already exists
if(ContactManager::isContact($peer, $peerAddress))
{
return $rpcRequest->produceResponse(false);
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact already exists');
}
// Create the contact
ContactManager::createContact($peer, $peerAddress, $relationship);
$contactUuid = ContactManager::createContact($peer, $peerAddress, $relationship);
}
catch (DatabaseOperationException $e)
{
@ -78,6 +77,6 @@
}
// Return success
return $rpcRequest->produceResponse(true);
return $rpcRequest->produceResponse($contactUuid);
}
}

View file

@ -9,6 +9,7 @@
use PDOException;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Database;
use Socialbox\Classes\Logger;
use Socialbox\Classes\Validator;
use Socialbox\Enums\Types\ContactRelationshipType;
use Socialbox\Exceptions\DatabaseOperationException;
@ -50,6 +51,8 @@
throw new InvalidArgumentException('The given peer internal UUID is not a valid UUID V4');
}
Logger::getLogger()->debug(sprintf('Querying if %s is a contact of %s', $contactAddress, $peerUuid));
try
{
// Check if the contact is already in the database
@ -89,7 +92,7 @@
}
elseif(!Validator::validatePeerAddress($contactAddress))
{
throw new InvalidArgumentException('The given contact address is not a valid peer address');
throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
}
if(!Validator::validateUuid($peerUuid))
@ -98,6 +101,7 @@
}
$uuid = UuidV4::v4()->toRfc4122();
Logger::getLogger()->debug(sprintf('Creating new contact (%s) for %s as UUID %s', $contactAddress, $peerUuid, $uuid));
try
{
@ -132,6 +136,8 @@
$peerUuid = $peerUuid->getUuid();
}
Logger::getLogger()->debug(sprintf('Querying contact count for %s', $peerUuid));
try
{
// Get the contact count from the database
@ -167,7 +173,7 @@
}
elseif(!Validator::validatePeerAddress($contactAddress))
{
throw new InvalidArgumentException('The given contact address is not a valid peer address');
throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
}
if(!Validator::validateUuid($peerUuid))
@ -175,6 +181,8 @@
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
}
Logger::getLogger()->debug(sprintf('Querying contact %s for %s', $contactAddress, $peerUuid));
try
{
// Get the contact from the database
@ -226,6 +234,8 @@
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
}
Logger::getLogger()->debug(sprintf('Deleting contact %s for %s', $contactAddress, $peerUuid));
try
{
$statement = Database::getConnection()->prepare('DELETE FROM contacts WHERE peer_uuid=:peer AND contact_peer_address=:address');
@ -261,7 +271,7 @@
}
elseif(!Validator::validatePeerAddress($contactAddress))
{
throw new InvalidArgumentException('The given contact address is not a valid peer address');
throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
}
if(!Validator::validateUuid($peerUuid))
@ -411,7 +421,7 @@
try
{
$statement = Database::getConnection()->prepare("SELECT uuid FROM contacts WHERE peer_uuid=:peer ORDER BY created DESC LIMIT :limit OFFSET :offset");
$statement = Database::getConnection()->prepare("SELECT contact_peer_address FROM contacts WHERE peer_uuid=:peer ORDER BY created DESC LIMIT :limit OFFSET :offset");
$offset = ($page - 1) * $limit;
$statement->bindParam(':peer', $peerUuid);
$statement->bindParam(':limit', $limit, PDO::PARAM_INT);
@ -424,7 +434,7 @@
// Convert results to ContactRecord instances
foreach ($results as $result)
{
$contacts[] = self::getStandardContact($peerUuid, $result['uuid']);
$contacts[] = self::getStandardContact($peerUuid, $result['contact_peer_address']);
}
}
catch (PDOException $e)
@ -465,11 +475,21 @@
$signatureKey = $signingKey->getPublicKey();
$statement->bindParam(':signature_key', $signatureKey);
$expires = $signingKey->getExpires();
if($expires === 0)
{
$expires = null;
}
else
{
$expires = (new DateTime())->setTimestamp($expires)->format('Y-m-d H:i:s');
}
$statement->bindParam(':expires', $expires);
$created = $signingKey->getCreated();
$created = (new DateTime())->setTimestamp($signingKey->getCreated())->format('Y-m-d H:i:s');
$statement->bindParam(':created', $created);
$trustedOn = (new DateTime())->format('Y-m-d H:i:s');
$statement->bindParam(':trusted_on', $trustedOn);
$statement->execute();
}
catch(PDOException $e)
{

View file

@ -5,6 +5,7 @@
use InvalidArgumentException;
use Socialbox\Enums\Types\ContactRelationshipType;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Database\ContactKnownKeyRecord;
use Socialbox\Objects\PeerAddress;
class Contact implements SerializableInterface
@ -51,9 +52,13 @@
{
$this->knownKeys[] = $key;
}
elseif($key instanceof ContactKnownKeyRecord)
{
$this->knownKeys[] = $key->toStandard();
}
else
{
throw new InvalidArgumentException('Invalid known key data');
throw new InvalidArgumentException('Invalid known key data, got ' . gettype($key));
}
}
$this->addedTimestamp = $data['added_timestamp'];
@ -89,6 +94,13 @@
return $this->knownKeys;
}
/**
* Checks if the contact has a signature with the given UUID.
*
* @param string $signatureUuid The UUID of the signature to check for.
*
* @return bool Returns true if the signature exists, otherwise false.
*/
public function signatureExists(string $signatureUuid): bool
{
foreach($this->knownKeys as $key)
@ -101,6 +113,60 @@
return false;
}
/**
* Retrieves the signature by the UUID.
*
* @param string $signatureUuid The UUID of the signature to retrieve.
* @return KnownSigningKey|null Returns the signature if found, otherwise null.
*/
public function getSignature(string $signatureUuid): ?KnownSigningKey
{
foreach($this->knownKeys as $key)
{
if($key->getUuid() === $signatureUuid)
{
return $key;
}
}
return null;
}
/**
* Checks if the contact has a signature with the given public key.
*
* @param string $publicSignatureKey The public key of the signature to check for.
* @return bool Returns true if the signature exists, otherwise false.
*/
public function signatureKeyExists(string $publicSignatureKey): bool
{
foreach($this->knownKeys as $key)
{
if($key->getPublicKey() === $publicSignatureKey)
{
return true;
}
}
return false;
}
/**
* Retrieves the signature key by the public key.
*
* @param string $publicSignatureKey The public key of the signature key to retrieve.
* @return KnownSigningKey|null Returns the signature key if found, otherwise null.
*/
public function getSignatureByPublicKey(string $publicSignatureKey): ?KnownSigningKey
{
foreach($this->knownKeys as $key)
{
if($key->getPublicKey() === $publicSignatureKey)
{
return $key;
}
}
return null;
}
/**
* Retrieves the timestamp when the contact was added.
*

View file

@ -44,6 +44,10 @@
{
$this->expires = $data['expires']->getTimestamp();
}
elseif($data['expires'] === null)
{
$this->expires = 0;
}
else
{
throw new InvalidArgumentException('Invalid expires value');

View file

@ -194,10 +194,10 @@
*
* @param PeerAddress|string $peer The address of the peer to add as a contact
* @param string|ContactRelationshipType|null $relationship Optional. The relationship for the peer
* @return bool Returns True if the contact was created, False if it already exists
* @return string Returns the contact uuid if the contact was created, False if it already exists
* @throws RpcException Thrown if there was an error with the RPC request
*/
public function addressBookAddContact(PeerAddress|string $peer, null|string|ContactRelationshipType $relationship=ContactRelationshipType::MUTUAL): bool
public function addressBookAddContact(PeerAddress|string $peer, null|string|ContactRelationshipType $relationship=ContactRelationshipType::MUTUAL): string
{
if($peer instanceof PeerAddress)
{

View file

@ -1,11 +1,11 @@
<?php
use Random\RandomException;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\ResolutionException;
use Socialbox\Exceptions\RpcException;
use Socialbox\SocialClient;
use Random\RandomException;
use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\ResolutionException;
use Socialbox\Exceptions\RpcException;
use Socialbox\SocialClient;
class Helper
{