Rename and add session update methods

This commit is contained in:
netkas 2024-10-30 18:33:38 -04:00
parent a9a13f186c
commit cd7be1c3b2
5 changed files with 58 additions and 21 deletions

View file

@ -31,13 +31,13 @@ class GetMe extends Method
{ {
// Get the session and check if it's already authenticated // Get the session and check if it's already authenticated
$session = SessionManager::getSession($request->getSessionUuid()); $session = SessionManager::getSession($request->getSessionUuid());
if($session->getAuthenticatedPeerUuid() === null) if($session->getPeerUuid() === null)
{ {
return $rpcRequest->produceError(StandardError::AUTHENTICATION_REQUIRED); return $rpcRequest->produceError(StandardError::AUTHENTICATION_REQUIRED);
} }
// Get the peer and return it // Get the peer and return it
return $rpcRequest->produceResponse(RegisteredPeerManager::getPeer($session->getAuthenticatedPeerUuid())->toSelfUser()); return $rpcRequest->produceResponse(RegisteredPeerManager::getPeer($session->getPeerUuid())->toSelfUser());
} }
catch(DatabaseOperationException $e) catch(DatabaseOperationException $e)
{ {

View file

@ -61,13 +61,13 @@ class Register extends Method
{ {
// Get the session and check if it's already authenticated // Get the session and check if it's already authenticated
$session = SessionManager::getSession($request->getSessionUuid()); $session = SessionManager::getSession($request->getSessionUuid());
if($session->getAuthenticatedPeerUuid() !== null) if($session->getPeerUuid() !== null)
{ {
return $rpcRequest->produceError(StandardError::ALREADY_AUTHENTICATED); return $rpcRequest->produceError(StandardError::ALREADY_AUTHENTICATED);
} }
// Create the peer & set the current's session authenticated peer as the newly created peer // Create the peer & set the current's session authenticated peer as the newly created peer
SessionManager::updateAuthenticatedPeer($session->getUuid(), RegisteredPeerManager::createPeer($rpcRequest->getParameter('username'))); SessionManager::updatePeer($session->getUuid(), RegisteredPeerManager::createPeer($rpcRequest->getParameter('username')));
} }
catch(DatabaseOperationException $e) catch(DatabaseOperationException $e)
{ {

View file

@ -138,7 +138,7 @@
* @return void * @return void
* @throws DatabaseOperationException * @throws DatabaseOperationException
*/ */
public static function updateAuthenticatedPeer(string $uuid, RegisteredPeerRecord|string $registeredPeerUuid): void public static function updatePeer(string $uuid, RegisteredPeerRecord|string $registeredPeerUuid): void
{ {
if($registeredPeerUuid instanceof RegisteredPeerRecord) if($registeredPeerUuid instanceof RegisteredPeerRecord)
{ {
@ -149,7 +149,7 @@
try try
{ {
$statement = Database::getConnection()->prepare("UPDATE sessions SET authenticated_peer_uuid=? WHERE uuid=?"); $statement = Database::getConnection()->prepare("UPDATE sessions SET peer_uuid=? WHERE uuid=?");
$statement->bindParam(1, $registeredPeerUuid); $statement->bindParam(1, $registeredPeerUuid);
$statement->bindParam(2, $uuid); $statement->bindParam(2, $uuid);
$statement->execute(); $statement->execute();
@ -160,6 +160,23 @@
} }
} }
public static function updateAuthentication(string $uuid, bool $authenticated): void
{
Logger::getLogger()->verbose(sprintf("Marking session %s as authenticated: %s", $uuid, $authenticated ? 'true' : 'false'));
try
{
$statement = Database::getConnection()->prepare("UPDATE sessions SET authenticated=? WHERE uuid=?");
$statement->bindParam(1, $authenticated);
$statement->bindParam(2, $uuid);
$statement->execute();
}
catch (PDOException $e)
{
throw new DatabaseOperationException('Failed to update authenticated peer', $e);
}
}
/** /**
* Updates the last request timestamp for a given session by its UUID. * Updates the last request timestamp for a given session by its UUID.
* *

View file

@ -9,7 +9,8 @@ use Socialbox\Interfaces\SerializableInterface;
class SessionRecord implements SerializableInterface class SessionRecord implements SerializableInterface
{ {
private string $uuid; private string $uuid;
private ?string $authenticatedPeerUuid; private ?string $peerUuid;
private bool $authenticated;
private string $publicKey; private string $publicKey;
private SessionState $state; private SessionState $state;
private DateTime $created; private DateTime $created;
@ -18,7 +19,8 @@ class SessionRecord implements SerializableInterface
public function __construct(array $data) public function __construct(array $data)
{ {
$this->uuid = $data['uuid']; $this->uuid = $data['uuid'];
$this->authenticatedPeerUuid = $data['authenticated_peer_uuid'] ?? null; $this->peerUuid = $data['peer_uuid'] ?? null;
$this->authenticated = $data['authenticated'] ?? false;
$this->publicKey = $data['public_key']; $this->publicKey = $data['public_key'];
$this->created = $data['created']; $this->created = $data['created'];
$this->lastRequest = $data['last_request']; $this->lastRequest = $data['last_request'];
@ -38,9 +40,19 @@ class SessionRecord implements SerializableInterface
return $this->uuid; return $this->uuid;
} }
public function getAuthenticatedPeerUuid(): ?string public function getPeerUuid(): ?string
{ {
return $this->authenticatedPeerUuid; return $this->peerUuid;
}
public function isAuthenticated(): bool
{
if($this->peerUuid === null)
{
return false;
}
return $this->authenticated;
} }
public function getPublicKey(): string public function getPublicKey(): string
@ -72,7 +84,8 @@ class SessionRecord implements SerializableInterface
{ {
return [ return [
'uuid' => $this->uuid, 'uuid' => $this->uuid,
'authenticated_peer_uuid' => $this->authenticatedPeerUuid, 'peer_uuid' => $this->peerUuid,
'authenticated' => $this->authenticated,
'public_key' => $this->publicKey, 'public_key' => $this->publicKey,
'state' => $this->state->value, 'state' => $this->state->value,
'created' => $this->created, 'created' => $this->created,

View file

@ -10,6 +10,7 @@ use Socialbox\Objects\Database\RegisteredPeerRecord;
class SelfUser implements SerializableInterface class SelfUser implements SerializableInterface
{ {
private string $uuid; private string $uuid;
private bool $enabled;
private string $address; private string $address;
private string $username; private string $username;
private ?string $displayName; private ?string $displayName;
@ -29,8 +30,9 @@ class SelfUser implements SerializableInterface
if($data instanceof RegisteredPeerRecord) if($data instanceof RegisteredPeerRecord)
{ {
$this->uuid = $data->getUuid(); $this->uuid = $data->getUuid();
$this->enabled = $data->isEnabled();
$this->username = $data->getUsername(); $this->username = $data->getUsername();
$this->address = $this->address = $data->getAddress();
$this->displayName = $data->getDisplayName(); $this->displayName = $data->getDisplayName();
$this->flags = $data->getFlags(); $this->flags = $data->getFlags();
$this->created = $data->getCreated()->getTimestamp(); $this->created = $data->getCreated()->getTimestamp();
@ -39,7 +41,9 @@ class SelfUser implements SerializableInterface
} }
$this->uuid = $data['uuid']; $this->uuid = $data['uuid'];
$this->enabled = $data['enabled'];
$this->username = $data['username']; $this->username = $data['username'];
$this->address = $data['address'];
$this->displayName = $data['display_name'] ?? null; $this->displayName = $data['display_name'] ?? null;
if(is_string($data['flags'])) if(is_string($data['flags']))
@ -77,6 +81,11 @@ class SelfUser implements SerializableInterface
return $this->uuid; return $this->uuid;
} }
public function isEnabled(): bool
{
return $this->enabled;
}
/** /**
* *
* @return string The username of the user. * @return string The username of the user.
@ -86,6 +95,11 @@ class SelfUser implements SerializableInterface
return $this->username; return $this->username;
} }
public function getAddress(): string
{
return $this->address;
}
/** /**
* *
* @return string|null The display name. * @return string|null The display name.
@ -104,15 +118,6 @@ class SelfUser implements SerializableInterface
return $this->flags; return $this->flags;
} }
/**
*
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/** /**
* *
* @return int The timestamp when the object was created. * @return int The timestamp when the object was created.
@ -143,7 +148,9 @@ class SelfUser implements SerializableInterface
return [ return [
'uuid' => $this->uuid, 'uuid' => $this->uuid,
'enabled' => $this->enabled,
'username' => $this->username, 'username' => $this->username,
'address' => $this->address,
'display_name' => $this->displayName, 'display_name' => $this->displayName,
'flags' => $flags, 'flags' => $flags,
'created' => $this->created 'created' => $this->created