Refactor authentication handling by replacing getAuthenticatedOperator with requireAuthenticatedOperator in multiple entity methods

This commit is contained in:
netkas 2025-06-06 13:39:32 -04:00
parent 1eb5b83eb2
commit 079c1a9428
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
6 changed files with 14 additions and 11 deletions

View file

@ -122,11 +122,10 @@
* This method retrieves the currently authenticated operator, if any. * This method retrieves the currently authenticated operator, if any.
* If no operator is authenticated, it returns null. * If no operator is authenticated, it returns null.
* *
* @param bool $requireAuthentication Whether to require authentication. Defaults to true.
* @return OperatorRecord|null The authenticated operator record or null if not authenticated. * @return OperatorRecord|null The authenticated operator record or null if not authenticated.
* @throws RequestException If authentication is provided but is invalid/operator is disabled. * @throws RequestException If authentication is provided but is invalid/operator is disabled.
*/ */
public static function getAuthenticatedOperator(bool $requireAuthentication=true): ?OperatorRecord public static function getAuthenticatedOperator(): ?OperatorRecord
{ {
return parent::getAuthenticatedOperator(); return parent::getAuthenticatedOperator();
} }

View file

@ -47,8 +47,6 @@
$page = 1; $page = 1;
} }
$results = [];
if($authenticatedOperator === null) if($authenticatedOperator === null)
{ {
// Public audit logs are enabled, filter by public entries // Public audit logs are enabled, filter by public entries

View file

@ -18,7 +18,7 @@
*/ */
public static function handleRequest(): void public static function handleRequest(): void
{ {
$authenticatedOperator = FederationServer::getAuthenticatedOperator(false); $authenticatedOperator = FederationServer::getAuthenticatedOperator();
if(!Configuration::getServerConfiguration()->isBlacklistPublic() && $authenticatedOperator === null) if(!Configuration::getServerConfiguration()->isBlacklistPublic() && $authenticatedOperator === null)
{ {
throw new RequestException('Unauthorized: You must be authenticated to list blacklist records', 401); throw new RequestException('Unauthorized: You must be authenticated to list blacklist records', 401);

View file

@ -17,7 +17,7 @@
*/ */
public static function handleRequest(): void public static function handleRequest(): void
{ {
$authenticatedOperator = FederationServer::getAuthenticatedOperator(false); $authenticatedOperator = FederationServer::getAuthenticatedOperator();
$includeConfidential = false; $includeConfidential = false;
if(!Configuration::getServerConfiguration()->isEvidencePublic() && $authenticatedOperator === null) if(!Configuration::getServerConfiguration()->isEvidencePublic() && $authenticatedOperator === null)
@ -70,8 +70,7 @@
throw new RequestException('Internal Server Error: Unable to retrieve evidence', 500, $e); throw new RequestException('Internal Server Error: Unable to retrieve evidence', 500, $e);
} }
$result = array_map(fn($evidence) => $evidence->toArray(), $evidenceRecords); self::successResponse(array_map(fn($evidence) => $evidence->toArray(), $evidenceRecords));
self::successResponse($result);
} }
} }

View file

@ -15,7 +15,7 @@
*/ */
public static function handleRequest(): void public static function handleRequest(): void
{ {
$authenticatedOperator = FederationServer::getAuthenticatedOperator(); $authenticatedOperator = FederationServer::requireAuthenticatedOperator();
if(!$authenticatedOperator->isClient() && !$authenticatedOperator->canManageOperators()) if(!$authenticatedOperator->isClient() && !$authenticatedOperator->canManageOperators())
{ {
throw new RequestException('Unauthorized: Insufficient permissions to push entities', 403); throw new RequestException('Unauthorized: Insufficient permissions to push entities', 403);

View file

@ -2,6 +2,7 @@
namespace FederationServer\Methods\Entities; namespace FederationServer\Methods\Entities;
use FederationServer\Classes\Configuration;
use FederationServer\Classes\Managers\EntitiesManager; use FederationServer\Classes\Managers\EntitiesManager;
use FederationServer\Classes\RequestHandler; use FederationServer\Classes\RequestHandler;
use FederationServer\Exceptions\DatabaseOperationException; use FederationServer\Exceptions\DatabaseOperationException;
@ -15,6 +16,12 @@
*/ */
public static function handleRequest(): void public static function handleRequest(): void
{ {
$authenticatedOperator = FederationServer::getAuthenticatedOperator();
if(!Configuration::getServerConfiguration()->isEntitiesPublic() && $authenticatedOperator === null)
{
throw new RequestException('Unauthorized: You must be authenticated to view entity records', 401);
}
$id = FederationServer::getParameter('id'); $id = FederationServer::getParameter('id');
$domain = FederationServer::getParameter('domain') ?? null; $domain = FederationServer::getParameter('domain') ?? null;
@ -30,14 +37,14 @@
try try
{ {
$entitiy = EntitiesManager::getEntity($id, $domain); $entity = EntitiesManager::getEntity($id, $domain);
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {
throw new RequestException('Internal Server Error: Unable to retrieve entity', 500, $e); throw new RequestException('Internal Server Error: Unable to retrieve entity', 500, $e);
} }
self::successResponse($entitiy->toArray()); self::successResponse($entity->toArray());
} }
} }