src/EventSubscriber/ServiceKeySubscriber.php line 40
<?phpnamespace App\EventSubscriber;use App\Security\ServiceKey;use Psr\Log\LoggerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\KernelEvents;/*** Refuses, with a JSON 401, every request under /internal/ that does not carry our key* (see ServiceKey).** It runs before the router and before the firewalls, so an address that names no route* answers 401 too, and nothing about the routes can be learnt without the key. The routes* under /internal/ have no login of their own: security.yaml leaves them out of every* firewall, and this check is their only door.*/final class ServiceKeySubscriber implements EventSubscriberInterface{private const PREFIX = '/internal/';public function __construct(private ServiceKey $key, private LoggerInterface $logger){}public static function getSubscribedEvents(): array{// The router listens at priority 32 and the firewalls at 8: this runs before both.return [KernelEvents::REQUEST => ['onKernelRequest', 512]];}public static function isInternal(string $path): bool{return $path === '/internal' || str_starts_with($path, self::PREFIX);}public function onKernelRequest(RequestEvent $event): void{if (!$event->isMainRequest()) {return;}$request = $event->getRequest();if (!self::isInternal($request->getPathInfo())) {return;}if ($this->key->matches($request->headers->get(ServiceKey::HEADER))) {return;}if (!$this->key->isConfigured()) {$this->logger->error('SOCIAL_SERVICE_KEY is empty: every route under /internal/ refuses');}$event->setResponse(new JsonResponse(['error' => sprintf('A valid %s header is required', ServiceKey::HEADER)],JsonResponse::HTTP_UNAUTHORIZED));}}