app/Plugin/Api42/EventListener/AuthorizationRequestResolveListener.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\Api42\EventListener;
  13. use Eccube\Entity\Master\Authority;
  14. use Eccube\Entity\Member;
  15. use League\OAuth2\Server\Exception\OAuthServerException;
  16. use Plugin\Api42\Form\Type\Admin\OAuth2AuthorizationType;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Form\FormFactoryInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEvent;
  22. use League\Bundle\OAuth2ServerBundle\OAuth2Events;
  23. use Twig\Environment as Twig;
  24. final class AuthorizationRequestResolveListener implements EventSubscriberInterface
  25. {
  26.     /** @var Twig */
  27.     protected $twig;
  28.     /** @var FormFactoryInterface */
  29.     protected $formFactory;
  30.     /** @var RequestStack */
  31.     protected $requestStack;
  32.     public function __construct(
  33.         Twig $twig,
  34.         FormFactoryInterface $formFactory,
  35.         RequestStack $requestStack
  36.     ) {
  37.         $this->twig $twig;
  38.         $this->formFactory $formFactory;
  39.         $this->requestStack $requestStack;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE => 'onAuthorizationRequestResolve',
  45.         ];
  46.     }
  47.     public function onAuthorizationRequestResolve(AuthorizationRequestResolveEvent $event): void
  48.     {
  49.         $user $event->getUser();
  50.         $request $this->requestStack->getMainRequest();
  51.         // システム管理者以外は承認しない
  52.         if (!$user instanceof Member || $user->getAuthority()->getId() !== Authority::ADMIN) {
  53.             $event->resolveAuthorization(AuthorizationRequestResolveEvent::AUTHORIZATION_DENIED);
  54.             return;
  55.         }
  56.         if (!$request->query->has('redirect_uri')) {
  57.             // redirect_uri_mismatch を返すべきだが OAuthServerException ではサポートされていない
  58.             // http://openid-foundation-japan.github.io/draft-ietf-oauth-v2.ja.html#auth-error-codes
  59.             throw OAuthServerException::invalidRequest('redirect_uri');
  60.         }
  61.         if (!$event->isAuthorizationApproved()) {
  62.             $builder $this->formFactory->createBuilder(OAuth2AuthorizationType::class);
  63.             $form $builder->getForm();
  64.             $form['client_id']->setData($event->getClient()->getIdentifier());
  65.             $form['client_secret']->setData($event->getClient()->getSecret());
  66.             $form['redirect_uri']->setData($event->getRedirectUri());
  67.             $form['state']->setData($event->getState());
  68.             $form['scope']->setData(join(' '$event->getScopes()));
  69.             $content $this->twig->render(
  70.                 '@Api42/admin/OAuth/authorization.twig',
  71.                 [
  72.                     'scopes' => $event->getScopes(),
  73.                     'form' => $form->createView(),
  74.                 ]
  75.             );
  76.             if ('POST' === $request->getMethod()) {
  77.                 $form->handleRequest($request);
  78.                 if ($form->isSubmitted() && $form->isValid()) {
  79.                     if ($form->get('approve')->isClicked()) {
  80.                         $event->resolveAuthorization(AuthorizationRequestResolveEvent::AUTHORIZATION_APPROVED);
  81.                     }
  82.                 } else {
  83.                     $event->resolveAuthorization(AuthorizationRequestResolveEvent::AUTHORIZATION_DENIED);
  84.                 }
  85.             } else {
  86.                 $event->setResponse(Response::create($content));
  87.             }
  88.         }
  89.     }
  90. }