src/EventSubscriber/TokenSubscriber.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\TokenAuthenticatedController;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class TokenSubscriber implements EventSubscriberInterface
  9. {
  10.     public function onKernelController(ControllerEvent $event)
  11.     {
  12.         $controller $event->getController();
  13.         // when a controller class defines multiple action methods, the controller
  14.         // is returned as [$controllerInstance, 'methodName']
  15.         if (is_array($controller)) {
  16.             $controller $controller[0];
  17.         }
  18.         if ($controller instanceof TokenAuthenticatedController) {
  19.             $api_key $event->getRequest()->headers->get('api-key');
  20.             $api_secret $event->getRequest()->headers->get('api-secret');
  21.             if ($api_key != $_ENV['API_KEY'] or $api_secret != $_ENV['API_SECRET']) {
  22.                // throw new AccessDeniedHttpException('Unauthorized.');
  23.             }
  24.         }
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::CONTROLLER => 'onKernelController',
  30.         ];
  31.     }
  32. }