Added the ability to trust signing keys & resolve signing keys for peers, minor improvements and added new standard error "CONFLICT"

This commit is contained in:
netkas 2025-01-30 15:20:11 -05:00
parent 4e22a8bacd
commit 330e7f876b
14 changed files with 427 additions and 42 deletions

View file

@ -2,17 +2,141 @@
namespace Socialbox\Objects\Database;
use DateTime;
use InvalidArgumentException;
use Socialbox\Interfaces\SerializableInterface;
class ContactKnownKeyRecord implements SerializableInterface
{
private string $contactUuid;
private string $signatureUuid;
private string $signatureName;
private string $signatureKey;
private ?DateTime $expires;
private DateTime $created;
private DateTime $trustedOn;
public function __construct(array $data)
{
$this->contactUuid = $data['contact_uuid'];
$this->signatureUuid = $data['signature_uuid'];
$this->signatureName = $data['signature_name'];
$this->signatureKey = $data['signature_key'];
if(!isset($data['expires']))
{
$this->expires = null;
}
else
{
if(is_string($data['expires']))
{
$this->expires = new DateTime($data['expires']);
}
elseif(is_int($data['expires']))
{
$this->expires = (new DateTime())->setTimestamp($data['expires']);
}
elseif($data['expires'] instanceof DateTime)
{
$this->expires = $data['expires'];
}
else
{
throw new InvalidArgumentException('Invalid expires property, got type ' . gettype($data['expires']));
}
}
if(!isset($data['created']))
{
throw new InvalidArgumentException('Missing created property');
}
else
{
if(is_string($data['created']))
{
$this->created = new DateTime($data['created']);
}
elseif(is_int($data['created']))
{
$this->created = (new DateTime())->setTimestamp($data['created']);
}
elseif($data['created'] instanceof DateTime)
{
$this->created = $data['created'];
}
else
{
throw new InvalidArgumentException('Invalid created property, got type ' . gettype($data['created']));
}
}
if(!isset($data['trusted_on']))
{
throw new InvalidArgumentException('Missing trusted_on property');
}
else
{
if(is_string($data['trusted_on']))
{
$this->trustedOn = new DateTime($data['trusted_on']);
}
elseif(is_int($data['trusted_on']))
{
$this->trustedOn = (new DateTime())->setTimestamp($data['trusted_on']);
}
elseif($data['trusted_on'] instanceof DateTime)
{
$this->trustedOn = $data['trusted_on'];
}
else
{
throw new InvalidArgumentException('Invalid trusted_on property, got type ' . gettype($data['trusted_on']));
}
}
}
public function getContactUuid(): string
{
return $this->contactUuid;
}
public function getSignatureUuid(): string
{
return $this->signatureUuid;
}
public function getSignatureName(): string
{
return $this->signatureName;
}
public function getSignatureKey(): string
{
return $this->signatureKey;
}
public function getExpires(): ?DateTime
{
return $this->expires;
}
public function getCreated(): DateTime
{
return $this->created;
}
public function getTrustedOn(): DateTime
{
return $this->trustedOn;
}
/**
* @inheritDoc
*/
public static function fromArray(array $data): object
public static function fromArray(array $data): ContactKnownKeyRecord
{
// TODO: Implement fromArray() method.
return new self($data);
}
/**
@ -20,6 +144,14 @@
*/
public function toArray(): array
{
// TODO: Implement toArray() method.
return [
'contact_uuid' => $this->contactUuid,
'signature_uuid' => $this->signatureUuid,
'signature_name' => $this->signatureName,
'signature_key' => $this->signatureKey,
'expires' => $this->expires?->getTimestamp(),
'created' => $this->created->getTimestamp(),
'trusted_on' => $this->trustedOn->getTimestamp()
];
}
}

View file

@ -12,7 +12,7 @@
{
private string $peerUuid;
private string $uuid;
private ?string $name;
private string $name;
private string $publicKey;
private SigningKeyState $state;
private int $expires;
@ -35,7 +35,7 @@
{
$this->peerUuid = $data['peer_uuid'];
$this->uuid = $data['uuid'];
$this->name = $data['name'] ?? null;
$this->name = $data['name'];
$this->publicKey = $data['public_key'];
$this->state = SigningKeyState::tryFrom($data['state']);
@ -115,9 +115,9 @@
/**
* Retrieves the name.
*
* @return string|null The name, or null if not set.
* @return string The name, or null if not set.
*/
public function getName(): ?string
public function getName(): string
{
return $this->name;
}

View file

@ -11,7 +11,7 @@
class SigningKey implements SerializableInterface
{
private string $uuid;
private ?string $name;
private string $name;
private string $publicKey;
private SigningKeyState $state;
private int $expires;
@ -35,7 +35,7 @@
public function __construct(array $data)
{
$this->uuid = $data['uuid'];
$this->name = $data['name'] ?? null;
$this->name = $data['name'];
$this->publicKey = $data['public_key'];
$this->state = SigningKeyState::from($data['state']);