Compare commits

..

No commits in common. "604f75f5c81ba62ffa5edc83c0ae3c3db03d0c05" and "631689b489209f7bc592f613945473a82f3d9386" have entirely different histories.

7 changed files with 21 additions and 110 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(64) not null comment 'The public signing key',
signature_key varchar(32) 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,6 +2,7 @@
namespace Socialbox\Classes\StandardMethods\AddressBook;
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\ReservedUsernames;
use Socialbox\Enums\StandardError;
@ -34,7 +35,7 @@
$peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
if($rpcRequest->containsParameter('relationship'))
if($rpcRequest->containsParameter('relationship') && $rpcRequest->getParameter('relationship') !== null)
{
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
if($relationship === null)
@ -65,11 +66,11 @@
// Check if the contact already exists
if(ContactManager::isContact($peer, $peerAddress))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact already exists');
return $rpcRequest->produceResponse(false);
}
// Create the contact
$contactUuid = ContactManager::createContact($peer, $peerAddress, $relationship);
ContactManager::createContact($peer, $peerAddress, $relationship);
}
catch (DatabaseOperationException $e)
{
@ -77,6 +78,6 @@
}
// Return success
return $rpcRequest->produceResponse($contactUuid);
return $rpcRequest->produceResponse(true);
}
}

View file

@ -9,7 +9,6 @@
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;
@ -51,8 +50,6 @@
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
@ -92,7 +89,7 @@
}
elseif(!Validator::validatePeerAddress($contactAddress))
{
throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
throw new InvalidArgumentException('The given contact address is not a valid peer address');
}
if(!Validator::validateUuid($peerUuid))
@ -101,7 +98,6 @@
}
$uuid = UuidV4::v4()->toRfc4122();
Logger::getLogger()->debug(sprintf('Creating new contact (%s) for %s as UUID %s', $contactAddress, $peerUuid, $uuid));
try
{
@ -136,8 +132,6 @@
$peerUuid = $peerUuid->getUuid();
}
Logger::getLogger()->debug(sprintf('Querying contact count for %s', $peerUuid));
try
{
// Get the contact count from the database
@ -173,7 +167,7 @@
}
elseif(!Validator::validatePeerAddress($contactAddress))
{
throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
throw new InvalidArgumentException('The given contact address is not a valid peer address');
}
if(!Validator::validateUuid($peerUuid))
@ -181,8 +175,6 @@
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
@ -234,8 +226,6 @@
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');
@ -271,7 +261,7 @@
}
elseif(!Validator::validatePeerAddress($contactAddress))
{
throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
throw new InvalidArgumentException('The given contact address is not a valid peer address');
}
if(!Validator::validateUuid($peerUuid))
@ -421,7 +411,7 @@
try
{
$statement = Database::getConnection()->prepare("SELECT contact_peer_address FROM contacts WHERE peer_uuid=:peer ORDER BY created DESC LIMIT :limit OFFSET :offset");
$statement = Database::getConnection()->prepare("SELECT uuid 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);
@ -434,7 +424,7 @@
// Convert results to ContactRecord instances
foreach ($results as $result)
{
$contacts[] = self::getStandardContact($peerUuid, $result['contact_peer_address']);
$contacts[] = self::getStandardContact($peerUuid, $result['uuid']);
}
}
catch (PDOException $e)
@ -475,21 +465,11 @@
$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 = (new DateTime())->setTimestamp($signingKey->getCreated())->format('Y-m-d H:i:s');
$created = $signingKey->getCreated();
$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,7 +5,6 @@
use InvalidArgumentException;
use Socialbox\Enums\Types\ContactRelationshipType;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Database\ContactKnownKeyRecord;
use Socialbox\Objects\PeerAddress;
class Contact implements SerializableInterface
@ -52,13 +51,9 @@
{
$this->knownKeys[] = $key;
}
elseif($key instanceof ContactKnownKeyRecord)
{
$this->knownKeys[] = $key->toStandard();
}
else
{
throw new InvalidArgumentException('Invalid known key data, got ' . gettype($key));
throw new InvalidArgumentException('Invalid known key data');
}
}
$this->addedTimestamp = $data['added_timestamp'];
@ -94,13 +89,6 @@
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)
@ -113,60 +101,6 @@
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,10 +44,6 @@
{
$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 string Returns the contact uuid if the contact was created, False if it already exists
* @return bool Returns True 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): string
public function addressBookAddContact(PeerAddress|string $peer, null|string|ContactRelationshipType $relationship=ContactRelationshipType::MUTUAL): bool
{
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
{