vendor/shopware/storefront/Controller/CsrfController.php line 70

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  4. use Shopware\Core\Framework\Routing\Annotation\Since;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Shopware\Storefront\Framework\Csrf\CsrfModes;
  7. use Shopware\Storefront\Framework\Csrf\Exception\CsrfNotEnabledException;
  8. use Shopware\Storefront\Framework\Csrf\Exception\CsrfWrongModeException;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  13. /**
  14.  * @RouteScope(scopes={"storefront"})
  15.  */
  16. class CsrfController extends StorefrontController
  17. {
  18.     /**
  19.      * @var CsrfTokenManagerInterface
  20.      */
  21.     private $csrfTokenManager;
  22.     /**
  23.      * @var bool
  24.      */
  25.     private $csrfEnabled;
  26.     /**
  27.      * @var string
  28.      */
  29.     private $csrfMode;
  30.     public function __construct(CsrfTokenManagerInterface $csrfTokenManagerbool $csrfEnabledstring $csrfMode)
  31.     {
  32.         $this->csrfTokenManager $csrfTokenManager;
  33.         $this->csrfEnabled $csrfEnabled;
  34.         $this->csrfMode $csrfMode;
  35.     }
  36.     /**
  37.      * @Since("6.1.0.0")
  38.      * @Route("/csrf/generate", name="frontend.csrf.generateToken", defaults={"csrf_protected"=false, "XmlHttpRequest"=true}, methods={"POST"})
  39.      */
  40.     public function generateCsrf(Request $request): JsonResponse
  41.     {
  42.         if (!$this->csrfEnabled) {
  43.             throw new CsrfNotEnabledException();
  44.         }
  45.         if ($this->csrfMode !== CsrfModes::MODE_AJAX) {
  46.             throw new CsrfWrongModeException(CsrfModes::MODE_AJAX);
  47.         }
  48.         $intent $request->request->get('intent''ajax');
  49.         $token $this->csrfTokenManager->getToken($intent);
  50.         return new JsonResponse(['token' => $token->getValue()]);
  51.     }
  52.     /**
  53.      * @Since("6.2.0.0")
  54.      *
  55.      * @deprecated tag:v6.4.0 will be removed without replacement
  56.      * @Route("/api-access", name="frontend.api-access", defaults={"csrf_protected"=false, "XmlHttpRequest"=true}, methods={"GET"})
  57.      */
  58.     public function getApiAccess(SalesChannelContext $context): JsonResponse
  59.     {
  60.         return new JsonResponse([
  61.             'accessKey' => $context->getSalesChannel()->getAccessKey(),
  62.             'token' => $context->getToken(),
  63.         ]);
  64.     }
  65. }