src/EventSubscriber/TokenSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. // src/EventSubscriber/TokenSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Controller\TokenAuthenticatedController;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Psr\Log\LoggerInterface;
  10. use App\Service\OAuth2\Autoloader;
  11. use Doctrine\DBAL\Connection;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use App\Service\OAuth2\Storage\Pdo;
  14. use App\Service\OAuth2\Server;
  15. use App\Service\OAuth2\Request;
  16. use App\Service\OAuth2\Response;
  17. class TokenSubscriber implements EventSubscriberInterface
  18. {
  19.     private $tokens;
  20.     public function __construct(Connection $connectionParameterBagInterface $paramsLoggerInterface $apiLogger)
  21.     {
  22.         $this->connection $connection;
  23.         $this->params $params;
  24.         $this->logger $apiLogger;
  25.     }
  26.     public function onKernelController(ControllerEvent $event)
  27.     {
  28.         $controller $event->getController();
  29.         $allowed_ip $this->params->get('api.allowed_ips');
  30.         // when a controller class defines multiple action methods, the controller
  31.         // is returned as [$controllerInstance, 'methodName']
  32.         if (is_array($controller)) {
  33.             $controller $controller[0];
  34.         }
  35.         if(!in_array($_SERVER['REMOTE_ADDR'],$allowed_ip) && !in_array('0.0.0.0'$allowed_ip)){
  36.             $this->logger->warning("API called from not allowed ip",array("REMOTE_ADDR" => $_SERVER['REMOTE_ADDR']) );
  37.             throw new AccessDeniedHttpException();
  38.         }
  39.         if ($controller instanceof TokenAuthenticatedController) {
  40.             $token $event->getRequest()->query->get('token');
  41.             Autoloader::register();
  42.             /** @var \Doctrine\DBAL\Connection */
  43.             $connectionParams $this->connection->getParams();
  44.             $db_user $connectionParams['user'];
  45.             $db_passwd $connectionParams['password'];
  46.             $db_host $connectionParams['host'];
  47.             $db_port $connectionParams['port'];
  48.             $db_name $connectionParams['dbname'];
  49.             $db_dsn "pgsql:host=".$db_host.";port=".$db_port.";dbname=".$db_name.";";
  50.             // create your storage again
  51.             $storage = new Pdo(array('dsn' => $db_dsn'username' => $db_user'password' => $db_passwd));
  52.             // create your server again
  53.             $server = new Server($storage);
  54.             // Handle a request for an OAuth2.0 Access Token and send the response to the client
  55.             if (!$server->verifyResourceRequest(Request::createFromGlobals(), new Response())) {
  56.                 $server->getResponse()->send();
  57.                 die;
  58.             }
  59.         }
  60.     }
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             KernelEvents::CONTROLLER => 'onKernelController',
  65.         ];
  66.     }
  67. }