Compare commits
7 commits
631689b489
...
604f75f5c8
Author | SHA1 | Date | |
---|---|---|---|
604f75f5c8 | |||
4a7fbd9970 | |||
e4f3d50852 | |||
52985b6308 | |||
97ca09d7dd | |||
db9fa3f92e | |||
c981ed3d9d |
7 changed files with 110 additions and 21 deletions
|
@ -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',
|
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_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_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',
|
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',
|
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',
|
trusted_on timestamp default current_timestamp() not null comment 'The Timestamp for when the peer trusted this key',
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Socialbox\Classes\StandardMethods\AddressBook;
|
namespace Socialbox\Classes\StandardMethods\AddressBook;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use Socialbox\Abstracts\Method;
|
use Socialbox\Abstracts\Method;
|
||||||
use Socialbox\Enums\ReservedUsernames;
|
use Socialbox\Enums\ReservedUsernames;
|
||||||
use Socialbox\Enums\StandardError;
|
use Socialbox\Enums\StandardError;
|
||||||
|
@ -35,7 +34,7 @@
|
||||||
|
|
||||||
$peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
|
$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')));
|
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
|
||||||
if($relationship === null)
|
if($relationship === null)
|
||||||
|
@ -66,11 +65,11 @@
|
||||||
// Check if the contact already exists
|
// Check if the contact already exists
|
||||||
if(ContactManager::isContact($peer, $peerAddress))
|
if(ContactManager::isContact($peer, $peerAddress))
|
||||||
{
|
{
|
||||||
return $rpcRequest->produceResponse(false);
|
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact already exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the contact
|
// Create the contact
|
||||||
ContactManager::createContact($peer, $peerAddress, $relationship);
|
$contactUuid = ContactManager::createContact($peer, $peerAddress, $relationship);
|
||||||
}
|
}
|
||||||
catch (DatabaseOperationException $e)
|
catch (DatabaseOperationException $e)
|
||||||
{
|
{
|
||||||
|
@ -78,6 +77,6 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return success
|
// Return success
|
||||||
return $rpcRequest->produceResponse(true);
|
return $rpcRequest->produceResponse($contactUuid);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,6 +9,7 @@
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use Socialbox\Classes\Configuration;
|
use Socialbox\Classes\Configuration;
|
||||||
use Socialbox\Classes\Database;
|
use Socialbox\Classes\Database;
|
||||||
|
use Socialbox\Classes\Logger;
|
||||||
use Socialbox\Classes\Validator;
|
use Socialbox\Classes\Validator;
|
||||||
use Socialbox\Enums\Types\ContactRelationshipType;
|
use Socialbox\Enums\Types\ContactRelationshipType;
|
||||||
use Socialbox\Exceptions\DatabaseOperationException;
|
use Socialbox\Exceptions\DatabaseOperationException;
|
||||||
|
@ -50,6 +51,8 @@
|
||||||
throw new InvalidArgumentException('The given peer internal UUID is not a valid UUID V4');
|
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
|
try
|
||||||
{
|
{
|
||||||
// Check if the contact is already in the database
|
// Check if the contact is already in the database
|
||||||
|
@ -89,7 +92,7 @@
|
||||||
}
|
}
|
||||||
elseif(!Validator::validatePeerAddress($contactAddress))
|
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))
|
if(!Validator::validateUuid($peerUuid))
|
||||||
|
@ -98,6 +101,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$uuid = UuidV4::v4()->toRfc4122();
|
$uuid = UuidV4::v4()->toRfc4122();
|
||||||
|
Logger::getLogger()->debug(sprintf('Creating new contact (%s) for %s as UUID %s', $contactAddress, $peerUuid, $uuid));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -132,6 +136,8 @@
|
||||||
$peerUuid = $peerUuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logger::getLogger()->debug(sprintf('Querying contact count for %s', $peerUuid));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Get the contact count from the database
|
// Get the contact count from the database
|
||||||
|
@ -167,7 +173,7 @@
|
||||||
}
|
}
|
||||||
elseif(!Validator::validatePeerAddress($contactAddress))
|
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))
|
if(!Validator::validateUuid($peerUuid))
|
||||||
|
@ -175,6 +181,8 @@
|
||||||
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
|
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
|
try
|
||||||
{
|
{
|
||||||
// Get the contact from the database
|
// Get the contact from the database
|
||||||
|
@ -226,6 +234,8 @@
|
||||||
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
|
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
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare('DELETE FROM contacts WHERE peer_uuid=:peer AND contact_peer_address=:address');
|
$statement = Database::getConnection()->prepare('DELETE FROM contacts WHERE peer_uuid=:peer AND contact_peer_address=:address');
|
||||||
|
@ -261,7 +271,7 @@
|
||||||
}
|
}
|
||||||
elseif(!Validator::validatePeerAddress($contactAddress))
|
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))
|
if(!Validator::validateUuid($peerUuid))
|
||||||
|
@ -411,7 +421,7 @@
|
||||||
|
|
||||||
try
|
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;
|
$offset = ($page - 1) * $limit;
|
||||||
$statement->bindParam(':peer', $peerUuid);
|
$statement->bindParam(':peer', $peerUuid);
|
||||||
$statement->bindParam(':limit', $limit, PDO::PARAM_INT);
|
$statement->bindParam(':limit', $limit, PDO::PARAM_INT);
|
||||||
|
@ -424,7 +434,7 @@
|
||||||
// Convert results to ContactRecord instances
|
// Convert results to ContactRecord instances
|
||||||
foreach ($results as $result)
|
foreach ($results as $result)
|
||||||
{
|
{
|
||||||
$contacts[] = self::getStandardContact($peerUuid, $result['uuid']);
|
$contacts[] = self::getStandardContact($peerUuid, $result['contact_peer_address']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (PDOException $e)
|
catch (PDOException $e)
|
||||||
|
@ -465,11 +475,21 @@
|
||||||
$signatureKey = $signingKey->getPublicKey();
|
$signatureKey = $signingKey->getPublicKey();
|
||||||
$statement->bindParam(':signature_key', $signatureKey);
|
$statement->bindParam(':signature_key', $signatureKey);
|
||||||
$expires = $signingKey->getExpires();
|
$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);
|
$statement->bindParam(':expires', $expires);
|
||||||
$created = $signingKey->getCreated();
|
$created = (new DateTime())->setTimestamp($signingKey->getCreated())->format('Y-m-d H:i:s');
|
||||||
$statement->bindParam(':created', $created);
|
$statement->bindParam(':created', $created);
|
||||||
$trustedOn = (new DateTime())->format('Y-m-d H:i:s');
|
$trustedOn = (new DateTime())->format('Y-m-d H:i:s');
|
||||||
$statement->bindParam(':trusted_on', $trustedOn);
|
$statement->bindParam(':trusted_on', $trustedOn);
|
||||||
|
|
||||||
|
$statement->execute();
|
||||||
}
|
}
|
||||||
catch(PDOException $e)
|
catch(PDOException $e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Socialbox\Enums\Types\ContactRelationshipType;
|
use Socialbox\Enums\Types\ContactRelationshipType;
|
||||||
use Socialbox\Interfaces\SerializableInterface;
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
|
use Socialbox\Objects\Database\ContactKnownKeyRecord;
|
||||||
use Socialbox\Objects\PeerAddress;
|
use Socialbox\Objects\PeerAddress;
|
||||||
|
|
||||||
class Contact implements SerializableInterface
|
class Contact implements SerializableInterface
|
||||||
|
@ -51,9 +52,13 @@
|
||||||
{
|
{
|
||||||
$this->knownKeys[] = $key;
|
$this->knownKeys[] = $key;
|
||||||
}
|
}
|
||||||
|
elseif($key instanceof ContactKnownKeyRecord)
|
||||||
|
{
|
||||||
|
$this->knownKeys[] = $key->toStandard();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InvalidArgumentException('Invalid known key data');
|
throw new InvalidArgumentException('Invalid known key data, got ' . gettype($key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->addedTimestamp = $data['added_timestamp'];
|
$this->addedTimestamp = $data['added_timestamp'];
|
||||||
|
@ -89,6 +94,13 @@
|
||||||
return $this->knownKeys;
|
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
|
public function signatureExists(string $signatureUuid): bool
|
||||||
{
|
{
|
||||||
foreach($this->knownKeys as $key)
|
foreach($this->knownKeys as $key)
|
||||||
|
@ -101,6 +113,60 @@
|
||||||
return false;
|
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.
|
* Retrieves the timestamp when the contact was added.
|
||||||
*
|
*
|
||||||
|
|
|
@ -44,6 +44,10 @@
|
||||||
{
|
{
|
||||||
$this->expires = $data['expires']->getTimestamp();
|
$this->expires = $data['expires']->getTimestamp();
|
||||||
}
|
}
|
||||||
|
elseif($data['expires'] === null)
|
||||||
|
{
|
||||||
|
$this->expires = 0;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InvalidArgumentException('Invalid expires value');
|
throw new InvalidArgumentException('Invalid expires value');
|
||||||
|
|
|
@ -194,10 +194,10 @@
|
||||||
*
|
*
|
||||||
* @param PeerAddress|string $peer The address of the peer to add as a contact
|
* @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
|
* @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
|
* @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)
|
if($peer instanceof PeerAddress)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Random\RandomException;
|
use Random\RandomException;
|
||||||
use Socialbox\Exceptions\CryptographyException;
|
use Socialbox\Exceptions\CryptographyException;
|
||||||
use Socialbox\Exceptions\DatabaseOperationException;
|
use Socialbox\Exceptions\DatabaseOperationException;
|
||||||
use Socialbox\Exceptions\ResolutionException;
|
use Socialbox\Exceptions\ResolutionException;
|
||||||
use Socialbox\Exceptions\RpcException;
|
use Socialbox\Exceptions\RpcException;
|
||||||
use Socialbox\SocialClient;
|
use Socialbox\SocialClient;
|
||||||
|
|
||||||
class Helper
|
class Helper
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue