diff --git a/src/Socialbox/Classes/StandardMethods/Authenticate.php b/src/Socialbox/Classes/StandardMethods/Authenticate.php new file mode 100644 index 0000000..87a52e3 --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/Authenticate.php @@ -0,0 +1,45 @@ +getPeer()->isExternal()) + { + return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Only external peers can authenticate'); + } + + if($request->getSession()->isAuthenticated()) + { + return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Peer is already authenticated'); + } + + SessionManager::removeFlags($request->getPeer()->getUuid(), [SessionFlags::AUTHENTICATION_REQUIRED]); + SessionManager::setAuthenticated($request->getPeer()->getUuid(), true); + } + catch(Exception $e) + { + throw new StandardException('An error occurred while authenticating the peer', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + + return $rpcRequest->produceResponse(true); + } + } \ No newline at end of file diff --git a/src/Socialbox/Enums/StandardMethods.php b/src/Socialbox/Enums/StandardMethods.php index f9ee7f5..c4e8cf9 100644 --- a/src/Socialbox/Enums/StandardMethods.php +++ b/src/Socialbox/Enums/StandardMethods.php @@ -6,6 +6,7 @@ use Socialbox\Classes\StandardMethods\AcceptCommunityGuidelines; use Socialbox\Classes\StandardMethods\AcceptPrivacyPolicy; use Socialbox\Classes\StandardMethods\AcceptTermsOfService; + use Socialbox\Classes\StandardMethods\Authenticate; use Socialbox\Classes\StandardMethods\GetAllowedMethods; use Socialbox\Classes\StandardMethods\GetCommunityGuidelines; use Socialbox\Classes\StandardMethods\GetPrivacyPolicy; @@ -95,6 +96,7 @@ case SETTINGS_ADD_SIGNING_KEY = 'settingsAddSigningKey'; case SETTINGS_GET_SIGNING_KEYS = 'settingsGetSigningKeys'; + case AUTHENTICATE = 'authenticate'; case RESOLVE_PEER = 'resolvePeer'; /** @@ -143,6 +145,7 @@ self::SETTINGS_ADD_SIGNING_KEY => SettingsAddSigningKey::execute($request, $rpcRequest), self::SETTINGS_GET_SIGNING_KEYS => SettingsGetSigningKeys::execute($request, $rpcRequest), + self::AUTHENTICATE => Authenticate::execute($request, $rpcRequest), self::RESOLVE_PEER => ResolvePeer::execute($request, $rpcRequest), default => $rpcRequest->produceError(StandardError::METHOD_NOT_ALLOWED, sprintf("The method %s is not supported by the server", $rpcRequest->getMethod())) @@ -229,12 +232,25 @@ } /** - **/ + * Retrieves a list of external methods based on the client's session state. + * + * @param ClientRequest + */ private static function getExternalMethods(ClientRequest $clientRequest): array { - return [ - self::RESOLVE_PEER - ]; + $methods = []; + + $session = $clientRequest->getSession(); + if(!$session->isAuthenticated() || $session->flagExists(SessionFlags::AUTHENTICATION_REQUIRED)) + { + $methods[] = self::AUTHENTICATE; + } + else + { + $methods[] = self::RESOLVE_PEER; + } + + return $methods; } /** diff --git a/src/Socialbox/Managers/SessionManager.php b/src/Socialbox/Managers/SessionManager.php index 0a86bef..4fd60dc 100644 --- a/src/Socialbox/Managers/SessionManager.php +++ b/src/Socialbox/Managers/SessionManager.php @@ -55,7 +55,11 @@ $flags = []; // TODO: Update this to support `host` peers - if($peer->isEnabled()) + if($peer->isExternal()) + { + $flags[] = SessionFlags::AUTHENTICATION_REQUIRED; + } + else if($peer->isEnabled()) { $flags[] = SessionFlags::AUTHENTICATION_REQUIRED;