Add expiration handling to sessions and refactor state conversion
This commit is contained in:
parent
f2ae3100de
commit
9ebf3f641f
3 changed files with 65 additions and 16 deletions
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace Socialbox\Managers;
|
namespace Socialbox\Managers;
|
||||||
|
|
||||||
|
use DateMalformedStringException;
|
||||||
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
|
@ -140,7 +142,7 @@
|
||||||
|
|
||||||
return new RegisteredPeerRecord($result);
|
return new RegisteredPeerRecord($result);
|
||||||
}
|
}
|
||||||
catch(PDOException | \DateMalformedStringException $e)
|
catch(Exception $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException('Failed to get the peer from the database', $e);
|
throw new DatabaseOperationException('Failed to get the peer from the database', $e);
|
||||||
}
|
}
|
||||||
|
@ -175,7 +177,7 @@
|
||||||
|
|
||||||
return new RegisteredPeerRecord($result);
|
return new RegisteredPeerRecord($result);
|
||||||
}
|
}
|
||||||
catch(PDOException | \DateMalformedStringException $e)
|
catch(PDOException | DateMalformedStringException $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException('Failed to get the peer from the database', $e);
|
throw new DatabaseOperationException('Failed to get the peer from the database', $e);
|
||||||
}
|
}
|
||||||
|
@ -446,7 +448,7 @@
|
||||||
|
|
||||||
return new SecurePasswordRecord($result);
|
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);
|
throw new DatabaseOperationException('Failed to get the secure password record from the database', $e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
use Socialbox\Classes\Configuration;
|
use Socialbox\Classes\Configuration;
|
||||||
use Socialbox\Enums\Flags\SessionFlags;
|
use Socialbox\Enums\Flags\SessionFlags;
|
||||||
use Socialbox\Enums\SessionState;
|
use Socialbox\Enums\SessionState;
|
||||||
|
use Socialbox\Exceptions\DatabaseOperationException;
|
||||||
use Socialbox\Interfaces\SerializableInterface;
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
use Socialbox\Managers\RegisteredPeerManager;
|
use Socialbox\Managers\RegisteredPeerManager;
|
||||||
|
|
||||||
|
@ -185,13 +186,26 @@
|
||||||
return $this->created;
|
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.
|
* Retrieves the list of flags associated with the current instance.
|
||||||
*
|
*
|
||||||
* @return array Returns an array of flags.
|
* @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;
|
return $this->flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,16 +266,9 @@
|
||||||
*/
|
*/
|
||||||
public function toStandardSessionState(): \Socialbox\Objects\Standard\SessionState
|
public function toStandardSessionState(): \Socialbox\Objects\Standard\SessionState
|
||||||
{
|
{
|
||||||
return new \Socialbox\Objects\Standard\SessionState([
|
return \Socialbox\Objects\Standard\SessionState::fromSessionRecord($this);
|
||||||
'uuid' => $this->uuid,
|
|
||||||
'identified_as' => RegisteredPeerManager::getPeer($this->peerUuid)->getAddress(),
|
|
||||||
'authenticated' => $this->authenticated,
|
|
||||||
'flags' => $this->flags,
|
|
||||||
'created' => $this->created
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Socialbox\Enums\Flags\SessionFlags;
|
use Socialbox\Enums\Flags\SessionFlags;
|
||||||
use Socialbox\Interfaces\SerializableInterface;
|
use Socialbox\Interfaces\SerializableInterface;
|
||||||
|
use Socialbox\Managers\RegisteredPeerManager;
|
||||||
|
use Socialbox\Objects\Database\SessionRecord;
|
||||||
|
|
||||||
class SessionState implements SerializableInterface
|
class SessionState implements SerializableInterface
|
||||||
{
|
{
|
||||||
|
@ -16,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
private ?array $flags;
|
private ?array $flags;
|
||||||
private int $created;
|
private int $created;
|
||||||
|
private int $expires;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for initializing the object with the provided data.
|
* Constructor for initializing the object with the provided data.
|
||||||
|
@ -57,6 +60,19 @@
|
||||||
{
|
{
|
||||||
$this->created = time();
|
$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;
|
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.
|
* Creates a new instance of SessionState from the provided array.
|
||||||
*
|
*
|
||||||
|
@ -142,9 +168,22 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the current instance into an associative array.
|
* @inheritDoc
|
||||||
*
|
*/
|
||||||
* @return array An associative array representation of the instance, including UUID, identification, authentication status, flags, and creation date.
|
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
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
|
@ -154,6 +193,7 @@
|
||||||
'authenticated' => $this->authenticated,
|
'authenticated' => $this->authenticated,
|
||||||
'flags' => $this->flags,
|
'flags' => $this->flags,
|
||||||
'created' => $this->created,
|
'created' => $this->created,
|
||||||
|
'expires' => $this->expires
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue