From 9ebf3f641f3e4dc6164705e46aadaf575ef7bcda Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 3 Jan 2025 21:38:33 -0500 Subject: [PATCH] Add expiration handling to sessions and refactor state conversion --- .../Managers/RegisteredPeerManager.php | 8 ++-- .../Objects/Database/SessionRecord.php | 27 +++++++---- .../Objects/Standard/SessionState.php | 46 +++++++++++++++++-- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/Socialbox/Managers/RegisteredPeerManager.php b/src/Socialbox/Managers/RegisteredPeerManager.php index 0595ae5..960ba4d 100644 --- a/src/Socialbox/Managers/RegisteredPeerManager.php +++ b/src/Socialbox/Managers/RegisteredPeerManager.php @@ -2,6 +2,8 @@ namespace Socialbox\Managers; + use DateMalformedStringException; + use Exception; use InvalidArgumentException; use PDO; use PDOException; @@ -140,7 +142,7 @@ return new RegisteredPeerRecord($result); } - catch(PDOException | \DateMalformedStringException $e) + catch(Exception $e) { throw new DatabaseOperationException('Failed to get the peer from the database', $e); } @@ -175,7 +177,7 @@ return new RegisteredPeerRecord($result); } - catch(PDOException | \DateMalformedStringException $e) + catch(PDOException | DateMalformedStringException $e) { throw new DatabaseOperationException('Failed to get the peer from the database', $e); } @@ -446,7 +448,7 @@ return new SecurePasswordRecord($result); } - catch(PDOException | \DateMalformedStringException $e) + catch(PDOException | DateMalformedStringException $e) { throw new DatabaseOperationException('Failed to get the secure password record from the database', $e); } diff --git a/src/Socialbox/Objects/Database/SessionRecord.php b/src/Socialbox/Objects/Database/SessionRecord.php index 642fbe5..c4ca58d 100644 --- a/src/Socialbox/Objects/Database/SessionRecord.php +++ b/src/Socialbox/Objects/Database/SessionRecord.php @@ -6,6 +6,7 @@ use Socialbox\Classes\Configuration; use Socialbox\Enums\Flags\SessionFlags; use Socialbox\Enums\SessionState; + use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Interfaces\SerializableInterface; use Socialbox\Managers\RegisteredPeerManager; @@ -185,13 +186,26 @@ return $this->created; } + /** + * @return DateTime + */ + public function getExpires(): DateTime + { + return new DateTime('@' . time() + Configuration::getPoliciesConfiguration()->getSessionInactivityExpires()); + } + /** * Retrieves the list of flags associated with the current instance. * * @return array Returns an array of flags. */ - public function getFlags(): array + public function getFlags(bool $asString): array { + if($asString) + { + return array_map(fn(SessionFlags $flag) => $flag->value, $this->flags); + } + return $this->flags; } @@ -252,16 +266,9 @@ */ public function toStandardSessionState(): \Socialbox\Objects\Standard\SessionState { - return new \Socialbox\Objects\Standard\SessionState([ - 'uuid' => $this->uuid, - 'identified_as' => RegisteredPeerManager::getPeer($this->peerUuid)->getAddress(), - 'authenticated' => $this->authenticated, - 'flags' => $this->flags, - 'created' => $this->created - ]); + return \Socialbox\Objects\Standard\SessionState::fromSessionRecord($this); } - - + /** * @inheritDoc */ diff --git a/src/Socialbox/Objects/Standard/SessionState.php b/src/Socialbox/Objects/Standard/SessionState.php index d2352ae..735aac5 100644 --- a/src/Socialbox/Objects/Standard/SessionState.php +++ b/src/Socialbox/Objects/Standard/SessionState.php @@ -5,6 +5,8 @@ use DateTime; use Socialbox\Enums\Flags\SessionFlags; use Socialbox\Interfaces\SerializableInterface; + use Socialbox\Managers\RegisteredPeerManager; + use Socialbox\Objects\Database\SessionRecord; class SessionState implements SerializableInterface { @@ -16,6 +18,7 @@ */ private ?array $flags; private int $created; + private int $expires; /** * Constructor for initializing the object with the provided data. @@ -57,6 +60,19 @@ { $this->created = time(); } + + if(is_int($data['expires'])) + { + $this->expires = $data['expires']; + } + elseif($data['expires'] instanceof DateTime) + { + $this->expires = $data['expires']->getTimestamp(); + } + else + { + $this->expires = time(); + } } /** @@ -130,6 +146,16 @@ return $this->created; } + /** + * Retrieves the expiration timestamp of the current instance. + * + * @return int The expiration timestamp as an integer. + */ + public function getExpires(): int + { + return $this->expires; + } + /** * Creates a new instance of SessionState from the provided array. * @@ -142,9 +168,22 @@ } /** - * Converts the current instance into an associative array. - * - * @return array An associative array representation of the instance, including UUID, identification, authentication status, flags, and creation date. + * @inheritDoc + */ + public static function fromSessionRecord(SessionRecord $sessionRecord): SessionState + { + return new self([ + 'uuid' => $sessionRecord->getUuid(), + 'identified_as' => RegisteredPeerManager::getPeer($sessionRecord->getPeerUuid())->getAddress(), + 'authenticated' => $sessionRecord->isAuthenticated(), + 'flags' => $sessionRecord->getFlags(true), + 'created' => $sessionRecord->getCreated()->getTimestamp(), + 'expires' => $sessionRecord->getExpires()->getTimestamp() + ]); + } + + /** + * @inheritDoc */ public function toArray(): array { @@ -154,6 +193,7 @@ 'authenticated' => $this->authenticated, 'flags' => $this->flags, 'created' => $this->created, + 'expires' => $this->expires ]; } } \ No newline at end of file