<?php
declare(strict_types=1);
namespace WnsSecurityComplianceSuite\Hardening\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use WnsSecurityComplianceSuite\Util\Provider\PluginConfigProvider;
final class ApiRequestSubscriber implements EventSubscriberInterface
{
private const PLUGIN_API_PREFIX = '/api/wscs';
private PluginConfigProvider $pluginConfigProvider;
public function __construct(
PluginConfigProvider $pluginConfigProvider
) {
$this->pluginConfigProvider = $pluginConfigProvider;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$path = $request->getPathInfo();
if ($this->pluginConfigProvider->isPluginApiDisabled() === false) {
return;
}
if (
strpos($path, self::PLUGIN_API_PREFIX) === 0
&& strpos($path, 'two-factor') === false
) {
$event->setResponse(new JsonResponse(['error' => 'The plugin API is disabled'], 403));
}
}
}