7 declare(strict_types=1);
19 use Magento\Framework\Controller\Result\RawFactory;
41 private $formKeyValidator;
51 private $redirectFactory;
56 private $rawResultFactory;
67 FormKeyValidator $formKeyValidator,
68 BackendUrl $backendUrl,
70 RawFactory $rawResultFactory
73 $this->formKeyValidator = $formKeyValidator;
74 $this->backendUrl = $backendUrl;
75 $this->redirectFactory = $redirectFactory;
76 $this->rawResultFactory = $rawResultFactory;
85 private function validateRequest(
93 $valid = $action->validateForCsrf(
$request);
96 if ($valid ===
null) {
98 $validSecretKey =
true;
100 $validFormKey = $this->formKeyValidator->validate(
$request);
101 }
elseif ($this->auth->isLoggedIn()
102 && $this->backendUrl->useSecretKey()
104 $secretKeyValue = (string)
$request->getParam(
105 BackendUrl::SECRET_KEY_PARAM_NAME,
108 $secretKey = $this->backendUrl->getSecretKey();
109 $validSecretKey = ($secretKeyValue === $secretKey);
111 $valid = $validFormKey && $validSecretKey;
123 private function createException(
125 ActionInterface $action
126 ): InvalidRequestException {
130 if ($action instanceof CsrfAwareActionInterface) {
131 $exception = $action->createCsrfValidationException(
$request);
134 if ($exception ===
null) {
139 $response = $this->rawResultFactory->create();
142 $exception =
new InvalidRequestException(
$response);
145 $response = $this->redirectFactory->create()
146 ->setUrl($this->backendUrl->getStartupPageUrl());
147 $exception =
new InvalidRequestException(
151 'Invalid security or form key. Please refresh the page.' 170 if (!$action->_processUrlKeys()) {
175 if (!$this->validateRequest(
$request, $action)) {
176 throw $this->createException(
$request, $action);
elseif(isset( $params[ 'redirect_parent']))
validate(RequestInterface $request, ActionInterface $action)
__construct(Auth $auth, FormKeyValidator $formKeyValidator, BackendUrl $backendUrl, RedirectFactory $redirectFactory, RawFactory $rawResultFactory)