diff --git a/src/Socialbox/Classes/StandardMethods/GetMe.php b/src/Socialbox/Classes/StandardMethods/GetMe.php index 9621320..fbd9826 100644 --- a/src/Socialbox/Classes/StandardMethods/GetMe.php +++ b/src/Socialbox/Classes/StandardMethods/GetMe.php @@ -31,13 +31,13 @@ class GetMe extends Method { // Get the session and check if it's already authenticated $session = SessionManager::getSession($request->getSessionUuid()); - if($session->getAuthenticatedPeerUuid() === null) + if($session->getPeerUuid() === null) { return $rpcRequest->produceError(StandardError::AUTHENTICATION_REQUIRED); } // 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) { diff --git a/src/Socialbox/Classes/StandardMethods/Register.php b/src/Socialbox/Classes/StandardMethods/Register.php index 40c1904..6bedc2d 100644 --- a/src/Socialbox/Classes/StandardMethods/Register.php +++ b/src/Socialbox/Classes/StandardMethods/Register.php @@ -61,13 +61,13 @@ class Register extends Method { // Get the session and check if it's already authenticated $session = SessionManager::getSession($request->getSessionUuid()); - if($session->getAuthenticatedPeerUuid() !== null) + if($session->getPeerUuid() !== null) { return $rpcRequest->produceError(StandardError::ALREADY_AUTHENTICATED); } // 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) { diff --git a/src/Socialbox/Managers/SessionManager.php b/src/Socialbox/Managers/SessionManager.php index 811e855..bb3eb6a 100644 --- a/src/Socialbox/Managers/SessionManager.php +++ b/src/Socialbox/Managers/SessionManager.php @@ -138,7 +138,7 @@ * @return void * @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) { @@ -149,7 +149,7 @@ 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(2, $uuid); $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. * diff --git a/src/Socialbox/Objects/Database/SessionRecord.php b/src/Socialbox/Objects/Database/SessionRecord.php index ab149ef..fa0ce42 100644 --- a/src/Socialbox/Objects/Database/SessionRecord.php +++ b/src/Socialbox/Objects/Database/SessionRecord.php @@ -9,7 +9,8 @@ use Socialbox\Interfaces\SerializableInterface; class SessionRecord implements SerializableInterface { private string $uuid; - private ?string $authenticatedPeerUuid; + private ?string $peerUuid; + private bool $authenticated; private string $publicKey; private SessionState $state; private DateTime $created; @@ -18,7 +19,8 @@ class SessionRecord implements SerializableInterface public function __construct(array $data) { $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->created = $data['created']; $this->lastRequest = $data['last_request']; @@ -38,9 +40,19 @@ class SessionRecord implements SerializableInterface 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 @@ -72,7 +84,8 @@ class SessionRecord implements SerializableInterface { return [ 'uuid' => $this->uuid, - 'authenticated_peer_uuid' => $this->authenticatedPeerUuid, + 'peer_uuid' => $this->peerUuid, + 'authenticated' => $this->authenticated, 'public_key' => $this->publicKey, 'state' => $this->state->value, 'created' => $this->created, diff --git a/src/Socialbox/Objects/Standard/SelfUser.php b/src/Socialbox/Objects/Standard/SelfUser.php index 109c21e..443f8dc 100644 --- a/src/Socialbox/Objects/Standard/SelfUser.php +++ b/src/Socialbox/Objects/Standard/SelfUser.php @@ -10,6 +10,7 @@ use Socialbox\Objects\Database\RegisteredPeerRecord; class SelfUser implements SerializableInterface { private string $uuid; + private bool $enabled; private string $address; private string $username; private ?string $displayName; @@ -29,8 +30,9 @@ class SelfUser implements SerializableInterface if($data instanceof RegisteredPeerRecord) { $this->uuid = $data->getUuid(); + $this->enabled = $data->isEnabled(); $this->username = $data->getUsername(); - $this->address = + $this->address = $data->getAddress(); $this->displayName = $data->getDisplayName(); $this->flags = $data->getFlags(); $this->created = $data->getCreated()->getTimestamp(); @@ -39,7 +41,9 @@ class SelfUser implements SerializableInterface } $this->uuid = $data['uuid']; + $this->enabled = $data['enabled']; $this->username = $data['username']; + $this->address = $data['address']; $this->displayName = $data['display_name'] ?? null; if(is_string($data['flags'])) @@ -77,6 +81,11 @@ class SelfUser implements SerializableInterface return $this->uuid; } + public function isEnabled(): bool + { + return $this->enabled; + } + /** * * @return string The username of the user. @@ -86,6 +95,11 @@ class SelfUser implements SerializableInterface return $this->username; } + public function getAddress(): string + { + return $this->address; + } + /** * * @return string|null The display name. @@ -104,15 +118,6 @@ class SelfUser implements SerializableInterface return $this->flags; } - /** - * - * @return bool - */ - public function isEnabled(): bool - { - return $this->enabled; - } - /** * * @return int The timestamp when the object was created. @@ -143,7 +148,9 @@ class SelfUser implements SerializableInterface return [ 'uuid' => $this->uuid, + 'enabled' => $this->enabled, 'username' => $this->username, + 'address' => $this->address, 'display_name' => $this->displayName, 'flags' => $flags, 'created' => $this->created