Add Authenticate method to handle external peer authentication
This commit is contained in:
parent
bed4e10fe2
commit
9d9a4b46b3
3 changed files with 70 additions and 5 deletions
45
src/Socialbox/Classes/StandardMethods/Authenticate.php
Normal file
45
src/Socialbox/Classes/StandardMethods/Authenticate.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\StandardMethods;
|
||||
|
||||
use Exception;
|
||||
use Socialbox\Abstracts\Method;
|
||||
use Socialbox\Enums\Flags\SessionFlags;
|
||||
use Socialbox\Enums\StandardError;
|
||||
use Socialbox\Exceptions\StandardException;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Managers\SessionManager;
|
||||
use Socialbox\Objects\ClientRequest;
|
||||
use Socialbox\Objects\RpcRequest;
|
||||
|
||||
class Authenticate extends Method
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
|
||||
{
|
||||
try
|
||||
{
|
||||
if(!$request->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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue