pandorafms/pandora_console/api/v2/config/middleware.php

100 lines
3.3 KiB
PHP
Raw Normal View History

2024-01-12 08:22:17 +01:00
<?php
2024-01-24 09:23:01 +01:00
use PandoraFMS\Modules\Shared\Enums\HttpCodesEnum;
2024-02-07 08:53:54 +01:00
use PandoraFMS\Modules\Shared\Middlewares\AclListMiddleware;
use PandoraFMS\Modules\Shared\Middlewares\UserTokenMiddleware;
2024-01-12 08:22:17 +01:00
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Psr\Log\LoggerInterface;
use Slim\App;
return function (App $app, ContainerInterface $container) {
// Parse json, form data and xml.
$app->addBodyParsingMiddleware();
// Add the Slim built-in routing middleware.
$app->addRoutingMiddleware();
// Authenticate Integria.
$beforeMiddleware = function (
Request $request,
RequestHandler $handler
) use (
$app,
$container
2024-01-23 13:04:44 +01:00
) {
2024-02-07 08:53:54 +01:00
$ipOrigin = $_SERVER['REMOTE_ADDR'];
$aclListMiddleware = $container->get(AclListMiddleware::class);
if ($aclListMiddleware->check($ipOrigin) === false) {
$response = $app->getResponseFactory()->createResponse();
$response->getBody()->write(
json_encode(['error' => __('IP %s is not in ACL list', $ipOrigin)])
);
2024-01-12 08:22:17 +01:00
2024-02-07 08:53:54 +01:00
$errorCode = HttpCodesEnum::UNAUTHORIZED;
$newResponse = $response->withStatus($errorCode);
return $newResponse;
2024-01-12 08:22:17 +01:00
}
2024-02-07 08:53:54 +01:00
$userTokenMiddleware = $container->get(UserTokenMiddleware::class);
if ($userTokenMiddleware->check($request) === false) {
2024-01-12 08:22:17 +01:00
$response = $app->getResponseFactory()->createResponse();
$response->getBody()->write(
2024-02-07 08:53:54 +01:00
json_encode(['error' => __('You need to be authenticated to perform this action')])
2024-01-12 08:22:17 +01:00
);
2024-01-24 09:23:01 +01:00
$errorCode = HttpCodesEnum::UNAUTHORIZED;
$newResponse = $response->withStatus($errorCode);
return $newResponse;
}
try {
include_once __DIR__.'/includeEnterpriseDependencies.php';
} catch (\Throwable $th) {
$response = $app->getResponseFactory()->createResponse();
$response->getBody()->write(
2024-02-07 08:53:54 +01:00
json_encode(['error' => __('Invalid License')])
2024-01-24 09:23:01 +01:00
);
$errorCode = HttpCodesEnum::UNAUTHORIZED;
2024-01-12 08:22:17 +01:00
$newResponse = $response->withStatus($errorCode);
return $newResponse;
}
$response = $handler->handle($request);
return $response;
};
$app->add($beforeMiddleware);
// Handle exceptions.
// Define Custom Error Handler.
$customErrorHandler = function (
Request $request,
Throwable $exception,
bool $displayErrorDetails,
bool $logErrors,
bool $logErrorDetails,
?LoggerInterface $logger=null
) use ($app) {
$logger?->error($exception->getMessage());
$response = $app->getResponseFactory()->createResponse();
$response->getBody()->write(
json_encode(['error' => $exception->getMessage()])
);
$errorCode = 500;
if (empty($exception->getCode()) === false) {
$errorCode = $exception->getCode();
}
$newResponse = $response->withStatus($errorCode);
return $newResponse;
};
// Add Error Middleware.
2024-01-31 13:17:46 +01:00
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$errorMiddleware->setDefaultErrorHandler($customErrorHandler);
2024-01-12 08:22:17 +01:00
};