src/EventListener/AuthorizationRequestResolverListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Psr\Log\LoggerInterface;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEvent;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use App\Entity\User;
  12. use App\Entity\SwitchUserToken;
  13. class AuthorizationRequestResolverListener {
  14.     private $symfonyUserKey 'symfony_username';
  15.     private EntityManagerInterface $em;
  16.     private LoggerInterface $logger;
  17.     private RequestStack $requestStack;
  18.     private RouterInterface $router;
  19.     private SessionInterface $session;
  20.     private TokenStorageInterface $tokenStorage;
  21.     private string $oauthImpersonate "oauth_impersonate_token";
  22.     public function __construct(EntityManagerInterface $emRequestStack $requestStackRouterInterface $router,
  23.             LoggerInterface $loggerTokenStorageInterface $tokenStorage) {
  24.         $this->em $em;
  25.         $this->logger $logger;
  26.         $this->requestStack $requestStack;
  27.         $this->router $router;
  28.         $this->session $this->requestStack->getSession();
  29.         $this->tokenStorage $tokenStorage;
  30.     }
  31.     public function resolve(AuthorizationRequestResolveEvent $event) {
  32.         $user $this->getUser();
  33.         if (empty($user)) {
  34.             $this->redirectLogin($event);
  35.         } else {
  36.             $this->resolveAuthorization($event$user);
  37.         }
  38.     }
  39.     private function getUser() {
  40.         if ($this->session->has($this->oauthImpersonate)) {
  41.             return $this->processImpersonate();
  42.         }
  43.         $token $this->tokenStorage->getToken();
  44.         if (empty($token)) {
  45.             return null;
  46.         }
  47.         return $token->getUser();
  48.     }
  49.     private function processImpersonate() {
  50.         $strToken $this->session->get($this->oauthImpersonate);
  51.         /* @var $token SwitchUserToken */
  52.         $token $this->em->getRepository(SwitchUserToken::class)->findOneBy(array('token' => $strToken));
  53.         if (empty($token)) {
  54.             return null;
  55.         }
  56.         if ($token->getUsed()) {
  57.             return null;
  58.         }
  59.         $userID $token->getUserID();
  60.         $user $this->em->getRepository(User::class)->find($userID);
  61.         $token->setUsed(true);
  62.         $this->em->persist($token);
  63.         $this->em->flush();
  64.         $this->session->remove($this->oauthImpersonate);
  65.         return $user;
  66.     }
  67.     private function resolveAuthorization(AuthorizationRequestResolveEvent $eventUser $user) {
  68.         /* set data for further processing in league oauth2 server */
  69.         $event->resolveAuthorization(true);
  70.         $event->setUser($user);
  71.         /* remove session value */
  72.         $this->session->remove($this->symfonyUserKey);
  73.     }
  74.     private function redirectLogin(AuthorizationRequestResolveEvent $event) {
  75.         $request $this->requestStack->getCurrentRequest();
  76.         $parameters $request->query->all();
  77.         $location $this->router->generate('oauth2_login'$parameters);
  78.         $this->logger->info($location);
  79.         $redirectResponse = new RedirectResponse($location);
  80.         $event->setResponse($redirectResponse);
  81.     }
  82. }