src/Subscriber/SecurityKeySubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber;
  4. use App\Exception\Auth\AuthenticationFailedException;
  5. use Flagception\Manager\FeatureManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class SecurityKeySubscriber implements EventSubscriberInterface
  10. {
  11.     private FeatureManagerInterface $manager;
  12.     public function __construct(FeatureManagerInterface $manager)
  13.     {
  14.         $this->manager $manager;
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         $request $event->getRequest();
  19.         $featureName $request->attributes->get('_feature');
  20.         $securityKey $request->headers->get('authorization');
  21.         $additionalInfo = [
  22.             'feature' => $featureName,
  23.         ];
  24.         if ($featureName && $this->manager->isActive($featureName) && $securityKey !== getenv(strtoupper($featureName) . '_SECURITY_KEY')) {
  25.             throw new AuthenticationFailedException($additionalInfo);
  26.         }
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             KernelEvents::REQUEST => [['onKernelRequest'19]],
  32.         ];
  33.     }
  34. }