app/Plugin/Api/EventListener/AuthorizationRequestResolveListener.php line 62

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\Api\EventListener;
  13. use Eccube\Entity\Master\Authority;
  14. use Eccube\Entity\Member;
  15. use League\OAuth2\Server\Exception\OAuthServerException;
  16. use Plugin\Api\Form\Type\Admin\OAuth2AuthorizationType;
  17. use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent;
  23. use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;
  24. use Twig\Environment as Twig;
  25. final class AuthorizationRequestResolveListener implements EventSubscriberInterface
  26. {
  27.     /** @var Twig */
  28.     protected $twig;
  29.     /** @var PsrHttpFactory */
  30.     protected $psr7Factory;
  31.     /** @var FormFactoryInterface */
  32.     protected $formFactory;
  33.     /** @var RequestStack */
  34.     protected $requestStack;
  35.     public function __construct(
  36.         Twig $twig,
  37.         PsrHttpFactory $psr7Factory,
  38.         FormFactoryInterface $formFactory,
  39.         RequestStack $requestStack
  40.     ) {
  41.         $this->twig $twig;
  42.         $this->psr7Factory $psr7Factory;
  43.         $this->formFactory $formFactory;
  44.         $this->requestStack $requestStack;
  45.     }
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE => 'onAuthorizationRequestResolve',
  50.         ];
  51.     }
  52.     public function onAuthorizationRequestResolve(AuthorizationRequestResolveEvent $event): void
  53.     {
  54.         $user $event->getUser();
  55.         $request $this->requestStack->getMasterRequest();
  56.         // システム管理者以外は承認しない
  57.         if (!$user instanceof Member || $user->getAuthority()->getId() !== Authority::ADMIN) {
  58.             $event->resolveAuthorization(AuthorizationRequestResolveEvent::AUTHORIZATION_DENIED);
  59.             return;
  60.         }
  61.         if (!$request->query->has('redirect_uri')) {
  62.             // redirect_uri_mismatch を返すべきだが OAuthServerException ではサポートされていない
  63.             // http://openid-foundation-japan.github.io/draft-ietf-oauth-v2.ja.html#auth-error-codes
  64.             throw OAuthServerException::invalidRequest('redirect_uri');
  65.         }
  66.         if (!$event->isAuthorizationApproved()) {
  67.             $builder $this->formFactory->createBuilder(OAuth2AuthorizationType::class);
  68.             $form $builder->getForm();
  69.             $form['client_id']->setData($event->getClient()->getIdentifier());
  70.             $form['client_secret']->setData($event->getClient()->getSecret());
  71.             $form['redirect_uri']->setData($event->getRedirectUri());
  72.             $form['state']->setData($event->getState());
  73.             $form['scope']->setData(join(' '$event->getScopes()));
  74.             $content $this->twig->render(
  75.                 '@Api/admin/OAuth/authorization.twig',
  76.                 [
  77.                     'scopes' => $event->getScopes(),
  78.                     'form' => $form->createView(),
  79.                 ]
  80.             );
  81.             if ('POST' === $request->getMethod()) {
  82.                 $form->handleRequest($request);
  83.                 if ($form->isSubmitted() && $form->isValid()) {
  84.                     if ($form->get('approve')->isClicked()) {
  85.                         $event->resolveAuthorization(AuthorizationRequestResolveEvent::AUTHORIZATION_APPROVED);
  86.                     }
  87.                 } else {
  88.                     $event->resolveAuthorization(AuthorizationRequestResolveEvent::AUTHORIZATION_DENIED);
  89.                 }
  90.             } else {
  91.                 $Response $this->psr7Factory->createResponse(Response::create($content));
  92.                 $event->setResponse($Response);
  93.             }
  94.         }
  95.     }
  96. }