Skip to content

Commit 2650ddc

Browse files
committed
security-package/issues/21: reCaptcha is added multiple times to head
1 parent 0c32aa7 commit 2650ddc

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptchaCustomer\Model\AjaxLogin;
9+
10+
use Magento\Framework\App\Action\Action;
11+
use Magento\Framework\App\ActionFlag;
12+
use Magento\Framework\App\ResponseInterface;
13+
use Magento\Framework\Serialize\SerializerInterface;
14+
15+
/**
16+
* ErrorProcessor
17+
*/
18+
class ErrorProcessor
19+
{
20+
/**
21+
* @var ActionFlag
22+
*/
23+
private $actionFlag;
24+
25+
/**
26+
* @var SerializerInterface
27+
*/
28+
private $serializer;
29+
30+
/**
31+
* @param ActionFlag $actionFlag
32+
* @param SerializerInterface $serializer
33+
*/
34+
public function __construct(
35+
ActionFlag $actionFlag,
36+
SerializerInterface $serializer
37+
) {
38+
$this->actionFlag = $actionFlag;
39+
$this->serializer = $serializer;
40+
}
41+
42+
/**
43+
* @param ResponseInterface $response
44+
* @param string $message
45+
* @return void
46+
*/
47+
public function processError(ResponseInterface $response, string $message): void
48+
{
49+
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
50+
51+
$jsonPayload = $this->serializer->serialize([
52+
'errors' => true,
53+
'message' => $message,
54+
]);
55+
$response->representJson($jsonPayload);
56+
}
57+
}

ReCaptchaCustomer/Observer/AjaxLoginObserver.php

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
namespace Magento\ReCaptchaCustomer\Observer;
99

1010
use Magento\Framework\App\Action\Action;
11-
use Magento\Framework\App\ActionFlag;
12-
use Magento\Framework\App\ResponseInterface;
1311
use Magento\Framework\Event\Observer;
1412
use Magento\Framework\Event\ObserverInterface;
1513
use Magento\Framework\Exception\InputException;
1614
use Magento\Framework\Exception\LocalizedException;
17-
use Magento\Framework\Serialize\SerializerInterface;
15+
use Magento\ReCaptchaCustomer\Model\AjaxLogin\ErrorProcessor;
1816
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
1917
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
2018
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface;
@@ -41,16 +39,6 @@ class AjaxLoginObserver implements ObserverInterface
4139
*/
4240
private $captchaValidator;
4341

44-
/**
45-
* @var ActionFlag
46-
*/
47-
private $actionFlag;
48-
49-
/**
50-
* @var SerializerInterface
51-
*/
52-
private $serializer;
53-
5442
/**
5543
* @var IsCaptchaEnabledInterface
5644
*/
@@ -61,31 +49,33 @@ class AjaxLoginObserver implements ObserverInterface
6149
*/
6250
private $logger;
6351

52+
/**
53+
* @var ErrorProcessor
54+
*/
55+
private $errorProcessor;
56+
6457
/**
6558
* @param CaptchaResponseResolverInterface $captchaResponseResolver
6659
* @param ValidationConfigResolverInterface $validationConfigResolver
6760
* @param ValidatorInterface $captchaValidator
68-
* @param ActionFlag $actionFlag
69-
* @param SerializerInterface $serializer
7061
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
7162
* @param LoggerInterface $logger
63+
* @param ErrorProcessor $errorProcessor
7264
*/
7365
public function __construct(
7466
CaptchaResponseResolverInterface $captchaResponseResolver,
7567
ValidationConfigResolverInterface $validationConfigResolver,
7668
ValidatorInterface $captchaValidator,
77-
ActionFlag $actionFlag,
78-
SerializerInterface $serializer,
7969
IsCaptchaEnabledInterface $isCaptchaEnabled,
80-
LoggerInterface $logger
70+
LoggerInterface $logger,
71+
ErrorProcessor $errorProcessor
8172
) {
8273
$this->captchaResponseResolver = $captchaResponseResolver;
8374
$this->validationConfigResolver = $validationConfigResolver;
8475
$this->captchaValidator = $captchaValidator;
85-
$this->actionFlag = $actionFlag;
86-
$this->serializer = $serializer;
8776
$this->isCaptchaEnabled = $isCaptchaEnabled;
8877
$this->logger = $logger;
78+
$this->errorProcessor = $errorProcessor;
8979
}
9080

9181
/**
@@ -101,36 +91,27 @@ public function execute(Observer $observer): void
10191
$controller = $observer->getControllerAction();
10292
$request = $controller->getRequest();
10393
$response = $controller->getResponse();
94+
10495
$validationConfig = $this->validationConfigResolver->get($key);
10596

10697
try {
10798
$reCaptchaResponse = $this->captchaResponseResolver->resolve($request);
10899
} catch (InputException $e) {
109100
$this->logger->error($e);
110-
$this->processError($response, $validationConfig->getValidationFailureMessage());
101+
$this->errorProcessor->processError(
102+
$response,
103+
$validationConfig->getValidationFailureMessage()
104+
);
111105
return;
112106
}
113107

114108
$validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig);
115109
if (false === $validationResult->isValid()) {
116-
$this->processError($response, $validationConfig->getValidationFailureMessage());
110+
$this->errorProcessor->processError(
111+
$response,
112+
$validationConfig->getValidationFailureMessage()
113+
);
117114
}
118115
}
119116
}
120-
121-
/**
122-
* @param ResponseInterface $response
123-
* @param string $message
124-
* @return void
125-
*/
126-
private function processError(ResponseInterface $response, string $message): void
127-
{
128-
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
129-
130-
$jsonPayload = $this->serializer->serialize([
131-
'errors' => true,
132-
'message' => $message,
133-
]);
134-
$response->representJson($jsonPayload);
135-
}
136117
}

0 commit comments

Comments
 (0)