diff --git a/ReCaptcha/Block/Frontend/ReCaptcha.php b/ReCaptcha/Block/Frontend/ReCaptcha.php index ebb75831..031de316 100644 --- a/ReCaptcha/Block/Frontend/ReCaptcha.php +++ b/ReCaptcha/Block/Frontend/ReCaptcha.php @@ -30,16 +30,12 @@ class ReCaptcha extends Template /** * @param Template\Context $context - * @param null $decoder @deprecated - * @param null $encoder @deprecated * @param LayoutSettings $layoutSettings * @param array $data * @param Config|null $config */ public function __construct( Template\Context $context, - $decoder, - $encoder, LayoutSettings $layoutSettings, array $data = [], Config $config = null diff --git a/ReCaptcha/Model/Provider/Failure/RedirectUrl/BeforeAuthUrlProvider.php b/ReCaptcha/Model/BeforeAuthUrlProvider.php similarity index 81% rename from ReCaptcha/Model/Provider/Failure/RedirectUrl/BeforeAuthUrlProvider.php rename to ReCaptcha/Model/BeforeAuthUrlProvider.php index 1026c1a1..29e96b2c 100644 --- a/ReCaptcha/Model/Provider/Failure/RedirectUrl/BeforeAuthUrlProvider.php +++ b/ReCaptcha/Model/BeforeAuthUrlProvider.php @@ -5,16 +5,15 @@ */ declare(strict_types=1); -namespace Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl; +namespace Magento\ReCaptcha\Model; use Magento\Customer\Model\Url; use Magento\Framework\Session\SessionManagerInterface; -use Magento\ReCaptcha\Model\Provider\Failure\RedirectUrlProviderInterface; /** * @inheritDoc */ -class BeforeAuthUrlProvider implements RedirectUrlProviderInterface +class BeforeAuthUrlProvider { /** * @var SessionManagerInterface diff --git a/ReCaptcha/Model/CaptchaRequestHandler.php b/ReCaptcha/Model/CaptchaRequestHandler.php new file mode 100644 index 00000000..0dc47bb7 --- /dev/null +++ b/ReCaptcha/Model/CaptchaRequestHandler.php @@ -0,0 +1,90 @@ +validate = $validate; + $this->remoteAddress = $remoteAddress; + $this->messageManager = $messageManager; + $this->actionFlag = $actionFlag; + $this->config = $config; + } + + /** + * @inheritdoc + */ + public function execute( + string $area, + RequestInterface $request, + HttpInterface $response, + string $redirectOnFailureUrl + ): void { + $reCaptchaResponse = $request->getParam(ValidateInterface::PARAM_RECAPTCHA_RESPONSE); + $remoteIp = $this->remoteAddress->getRemoteAddress(); + $options['threshold'] = ($area === Area::AREA_ADMINHTML) + ? $this->config->getMinBackendScore() : $this->config->getMinFrontendScore(); + + if (false === $this->validate->validate($reCaptchaResponse, $remoteIp, $options)) { + $this->messageManager->addErrorMessage($this->config->getErrorDescription()); + $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); + + $response->setRedirect($redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Model/CaptchaRequestHandlerInterface.php b/ReCaptcha/Model/CaptchaRequestHandlerInterface.php new file mode 100644 index 00000000..0b5f4fb2 --- /dev/null +++ b/ReCaptcha/Model/CaptchaRequestHandlerInterface.php @@ -0,0 +1,37 @@ +getPrivateKey() || !$this->getPublicKey()) { + if (!$this->isAreaEnabled(Area::AREA_ADMINHTML) || !$this->getPrivateKey() || !$this->getPublicKey()) { return false; } @@ -105,7 +105,7 @@ public function isEnabledBackend(): bool */ public function isEnabledFrontend(): bool { - if (!$this->getPrivateKey() || !$this->getPublicKey()) { + if (!$this->isAreaEnabled(Area::AREA_FRONTEND) || !$this->getPrivateKey() || !$this->getPublicKey()) { return false; } @@ -167,7 +167,7 @@ public function isEnabledFrontendContact(): bool * Return true if enabled on frontend create user * @return bool */ - public function isEnabledFrontendCreate(): bool + public function isEnabledFrontendCreateUser(): bool { if (!$this->isEnabledFrontend()) { return false; @@ -211,22 +211,6 @@ public function isEnabledFrontendNewsletter(): bool ); } - /** - * Return true if enabled on frontend send to friend - * @return bool - */ - public function isEnabledFrontendSendFriend(): bool - { - if (!$this->isEnabledFrontend()) { - return false; - } - - return (bool) $this->scopeConfig->getValue( - static::XML_PATH_ENABLED_FRONTEND_SENDFRIEND, - ScopeInterface::SCOPE_WEBSITE - ); - } - /** * @return bool */ @@ -359,4 +343,19 @@ public function getMinBackendScore(): float static::XML_PATH_SIZE_MIN_SCORE_BACKEND ))); } + + /** + * Return true if area is configured to be active + * @param string $area + * @return bool + */ + public function isAreaEnabled(string $area): bool + { + if (!in_array($area, [Area::AREA_FRONTEND, Area::AREA_ADMINHTML], true)) { + throw new \InvalidArgumentException('Area parameter must be one of frontend or adminhtml'); + } + + return (($area === Area::AREA_ADMINHTML) && $this->isEnabledBackend()) + || (($area === Area::AREA_FRONTEND) && $this->isEnabledFrontend()); + } } diff --git a/ReCaptcha/Model/ConfigEnabledInterface.php b/ReCaptcha/Model/ConfigEnabledInterface.php new file mode 100644 index 00000000..41eb13ba --- /dev/null +++ b/ReCaptcha/Model/ConfigEnabledInterface.php @@ -0,0 +1,22 @@ +scopeConfig = $scopeConfig; - $this->config = $config; - $this->enableConfigFlag = $enableConfigFlag; - $this->requireRequestParam = $requireRequestParam; - $this->area = $area; - $this->request = $request; - - if (!in_array($this->area, [Area::AREA_FRONTEND, Area::AREA_ADMINHTML], true)) { - throw new \InvalidArgumentException('Area parameter must be one of frontend or adminhtml'); - } - } - - /** - * Return true if area is configured to be active - * @return bool - */ - private function isAreaEnabled(): bool - { - return - (($this->area === Area::AREA_ADMINHTML) && $this->config->isEnabledBackend()) || - (($this->area === Area::AREA_FRONTEND) && $this->config->isEnabledFrontend()); - } - - /** - * Return true if current zone is enabled - * @return bool - */ - private function isZoneEnabled(): bool - { - return !$this->enableConfigFlag || $this->scopeConfig->getValue($this->enableConfigFlag); - } - - /** - * Return true if request if valid - * @return bool - */ - private function isRequestValid(): bool - { - return !$this->requireRequestParam || $this->request->getParam($this->requireRequestParam); - } - - /** - * Return true if check is required - * @return bool - */ - public function execute(): bool - { - return - $this->isAreaEnabled() && - $this->isZoneEnabled() && - $this->isRequestValid(); - } -} diff --git a/ReCaptcha/Model/IsCheckRequiredInterface.php b/ReCaptcha/Model/IsCheckRequiredInterface.php deleted file mode 100644 index 267c0fda..00000000 --- a/ReCaptcha/Model/IsCheckRequiredInterface.php +++ /dev/null @@ -1,20 +0,0 @@ -config = $config; + $this->configEnabledProviders = $configEnabledProviders; } /** @@ -32,7 +44,7 @@ public function __construct( */ public function getCaptchaSettings(): array { - return [ + $settings = [ 'siteKey' => $this->config->getPublicKey(), 'size' => $this->config->getFrontendSize(), 'badge' => $this->config->getFrontendPosition(), @@ -40,13 +52,16 @@ public function getCaptchaSettings(): array 'lang' => $this->config->getLanguageCode(), 'enabled' => [ 'login' => $this->config->isEnabledFrontendLogin(), - 'create' => $this->config->isEnabledFrontendCreate(), + 'create' => $this->config->isEnabledFrontendCreateUser(), 'forgot' => $this->config->isEnabledFrontendForgot(), 'contact' => $this->config->isEnabledFrontendContact(), 'review' => $this->config->isEnabledFrontendReview(), 'newsletter' => $this->config->isEnabledFrontendNewsletter(), - 'sendfriend' => $this->config->isEnabledFrontendSendFriend(), ] ]; + foreach ($this->configEnabledProviders as $key => $configEnabledProvider) { + $settings['enabled'][$key] = $configEnabledProvider->isEnabled(); + } + return $settings; } } diff --git a/ReCaptcha/Model/Provider/Failure/AjaxResponseFailure.php b/ReCaptcha/Model/Provider/Failure/AjaxResponseFailure.php deleted file mode 100644 index 3199bc77..00000000 --- a/ReCaptcha/Model/Provider/Failure/AjaxResponseFailure.php +++ /dev/null @@ -1,68 +0,0 @@ -actionFlag = $actionFlag; - $this->serializer = $serializer; - $this->config = $config; - } - - /** - * Handle reCaptcha failure - * @param ResponseInterface $response - * @return void - */ - public function execute(ResponseInterface $response = null): void - { - $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); - - $jsonPayload = $this->serializer->encode([ - 'errors' => true, - 'message' => $this->config->getErrorDescription(), - ]); - $response->representJson($jsonPayload); - } -} diff --git a/ReCaptcha/Model/Provider/Failure/AuthenticationExceptionFailure.php b/ReCaptcha/Model/Provider/Failure/AuthenticationExceptionFailure.php deleted file mode 100644 index a57b1e56..00000000 --- a/ReCaptcha/Model/Provider/Failure/AuthenticationExceptionFailure.php +++ /dev/null @@ -1,46 +0,0 @@ -config = $config; - } - - /** - * Handle reCaptcha failure - * @param ResponseInterface $response - * @return void - * @throws AuthenticationException - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function execute(ResponseInterface $response = null): void - { - throw new AuthenticationException($this->config->getErrorDescription()); - } -} diff --git a/ReCaptcha/Model/Provider/Failure/ObserverRedirectFailure.php b/ReCaptcha/Model/Provider/Failure/ObserverRedirectFailure.php deleted file mode 100644 index 477227c1..00000000 --- a/ReCaptcha/Model/Provider/Failure/ObserverRedirectFailure.php +++ /dev/null @@ -1,85 +0,0 @@ -messageManager = $messageManager; - $this->actionFlag = $actionFlag; - $this->config = $config; - $this->redirectUrlProvider = $redirectUrlProvider; - } - - /** - * Get redirect URL - * @return string - */ - private function getUrl() - { - return $this->redirectUrlProvider->execute(); - } - - /** - * Handle reCaptcha failure - * @param ResponseInterface $response - * @return void - */ - public function execute(ResponseInterface $response = null): void - { - $this->messageManager->addErrorMessage($this->config->getErrorDescription()); - $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); - - $response->setRedirect($this->getUrl()); - } -} diff --git a/ReCaptcha/Model/Provider/Failure/RedirectUrl/RefererProvider.php b/ReCaptcha/Model/Provider/Failure/RedirectUrl/RefererProvider.php deleted file mode 100644 index c13b6cec..00000000 --- a/ReCaptcha/Model/Provider/Failure/RedirectUrl/RefererProvider.php +++ /dev/null @@ -1,40 +0,0 @@ -redirect = $redirect; - } - - /** - * Get redirection URL - * @return string - */ - public function execute(): string - { - return $this->redirect->getRedirectUrl(); - } -} diff --git a/ReCaptcha/Model/Provider/Failure/RedirectUrl/ReferrerUrlProvider.php b/ReCaptcha/Model/Provider/Failure/RedirectUrl/ReferrerUrlProvider.php deleted file mode 100644 index 57c15a28..00000000 --- a/ReCaptcha/Model/Provider/Failure/RedirectUrl/ReferrerUrlProvider.php +++ /dev/null @@ -1,43 +0,0 @@ -redirect = $redirect; - } - - /** - * Get redirection URL - * - * @return string - */ - public function execute(): string - { - return $this->redirect->getRefererUrl(); - } -} diff --git a/ReCaptcha/Model/Provider/Failure/RedirectUrl/SimpleUrlProvider.php b/ReCaptcha/Model/Provider/Failure/RedirectUrl/SimpleUrlProvider.php deleted file mode 100644 index 313c7e4d..00000000 --- a/ReCaptcha/Model/Provider/Failure/RedirectUrl/SimpleUrlProvider.php +++ /dev/null @@ -1,57 +0,0 @@ -urlPath = $urlPath; - $this->urlParams = $urlParams; - $this->url = $url; - } - - /** - * Get redirection URL - * @return string - */ - public function execute(): string - { - return $this->url->getUrl($this->urlPath, $this->urlParams); - } -} diff --git a/ReCaptcha/Model/Provider/Failure/RedirectUrlProviderInterface.php b/ReCaptcha/Model/Provider/Failure/RedirectUrlProviderInterface.php deleted file mode 100644 index 0e66c63e..00000000 --- a/ReCaptcha/Model/Provider/Failure/RedirectUrlProviderInterface.php +++ /dev/null @@ -1,20 +0,0 @@ -request = $request; - $this->serializer = $serializer; - } - - /** - * @inheritDoc - */ - public function execute(): string - { - if ($content = $this->request->getContent()) { - try { - $jsonParams = $this->serializer->decode($content); - if (isset($jsonParams[ValidateInterface::PARAM_RECAPTCHA_RESPONSE])) { - return $jsonParams[ValidateInterface::PARAM_RECAPTCHA_RESPONSE]; - } - } catch (\Exception $e) { - return ''; - } - } - - return ''; - } -} diff --git a/ReCaptcha/Model/Provider/Response/DefaultResponseProvider.php b/ReCaptcha/Model/Provider/Response/DefaultResponseProvider.php deleted file mode 100644 index 01c24431..00000000 --- a/ReCaptcha/Model/Provider/Response/DefaultResponseProvider.php +++ /dev/null @@ -1,41 +0,0 @@ -request = $request; - } - - /** - * @inheritDoc - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - public function execute(): string - { - return $this->request->getParam(ValidateInterface::PARAM_RECAPTCHA_RESPONSE); - } -} diff --git a/ReCaptcha/Model/Provider/ResponseProviderInterface.php b/ReCaptcha/Model/Provider/ResponseProviderInterface.php deleted file mode 100644 index 09e0e60b..00000000 --- a/ReCaptcha/Model/Provider/ResponseProviderInterface.php +++ /dev/null @@ -1,19 +0,0 @@ -config = $config; - $this->state = $state ?: ObjectManager::getInstance()->get(State::class); } /** - * Return true if reCaptcha validation has passed - * @param string $reCaptchaResponse - * @param string $remoteIp - * @return bool - * @throws LocalizedException + * @inheritdoc */ - public function validate(string $reCaptchaResponse, string $remoteIp): bool + public function validate(string $reCaptchaResponse, string $remoteIp, array $options = []): bool { $secret = $this->config->getPrivateKey(); @@ -57,11 +42,9 @@ public function validate(string $reCaptchaResponse, string $remoteIp): bool // @codingStandardsIgnoreEmd if ($this->config->getType() === 'recaptcha_v3') { - $threshold = $this->state->getAreaCode() === Area::AREA_ADMINHTML ? - $this->config->getMinBackendScore() : - $this->config->getMinFrontendScore(); - - $reCaptcha->setScoreThreshold($threshold); + if (isset($options['threshold'])) { + $reCaptcha->setScoreThreshold($options['threshold']); + } } $res = $reCaptcha->verify($reCaptchaResponse, $remoteIp); diff --git a/ReCaptcha/Model/ValidateInterface.php b/ReCaptcha/Model/ValidateInterface.php index f85d038c..e5941379 100644 --- a/ReCaptcha/Model/ValidateInterface.php +++ b/ReCaptcha/Model/ValidateInterface.php @@ -7,8 +7,12 @@ namespace Magento\ReCaptcha\Model; +use Magento\Framework\Exception\LocalizedException; + /** - * SPI - Interface for recaptcha validation + * Interface for recaptcha validation + * + * @api */ interface ValidateInterface { @@ -21,7 +25,9 @@ interface ValidateInterface * Return true if reCaptcha validation has passed * @param string $reCaptchaResponse * @param string $remoteIp + * @param array $options * @return bool + * @throws LocalizedException */ - public function validate(string $reCaptchaResponse, string $remoteIp): bool; + public function validate(string $reCaptchaResponse, string $remoteIp, array $options = []): bool; } diff --git a/ReCaptcha/Observer/Adminhtml/ForgotPasswordObserver.php b/ReCaptcha/Observer/Adminhtml/ForgotPasswordObserver.php new file mode 100644 index 00000000..e1b38605 --- /dev/null +++ b/ReCaptcha/Observer/Adminhtml/ForgotPasswordObserver.php @@ -0,0 +1,72 @@ +config = $config; + $this->url = $url; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + + if ($this->config->isAreaEnabled(Area::AREA_ADMINHTML) && null !== $request->getParam('email')) { + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->url->getUrl('*/*/forgotpassword', ['_secure' => true]); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Observer/Adminhtml/LoginObserver.php b/ReCaptcha/Observer/Adminhtml/LoginObserver.php new file mode 100644 index 00000000..f8356179 --- /dev/null +++ b/ReCaptcha/Observer/Adminhtml/LoginObserver.php @@ -0,0 +1,76 @@ +validate = $validate; + $this->remoteAddress = $remoteAddress; + $this->config = $config; + } + + /** + * @param Observer $observer + * @return void + * @throws AuthenticationException + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_ADMINHTML)) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + + $reCaptchaResponse = $controller->getRequest()->getParam(ValidateInterface::PARAM_RECAPTCHA_RESPONSE); + $remoteIp = $this->remoteAddress->getRemoteAddress(); + $options['threshold'] = $this->config->getMinBackendScore(); + + if (false === $this->validate->validate($reCaptchaResponse, $remoteIp, $options)) { + throw new AuthenticationException($this->config->getErrorDescription()); + } + } + } +} diff --git a/ReCaptcha/Observer/Frontend/AjaxLoginObserver.php b/ReCaptcha/Observer/Frontend/AjaxLoginObserver.php new file mode 100644 index 00000000..4480292e --- /dev/null +++ b/ReCaptcha/Observer/Frontend/AjaxLoginObserver.php @@ -0,0 +1,110 @@ +validate = $validate; + $this->remoteAddress = $remoteAddress; + $this->actionFlag = $actionFlag; + $this->serializer = $serializer; + $this->config = $config; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendLogin()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + + $reCaptchaResponse = ''; + if ($content = $controller->getRequest()->getContent()) { + try { + $jsonParams = $this->serializer->unserialize($content); + if (isset($jsonParams[ValidateInterface::PARAM_RECAPTCHA_RESPONSE])) { + $reCaptchaResponse = $jsonParams[ValidateInterface::PARAM_RECAPTCHA_RESPONSE]; + } + } catch (\Exception $e) { + $reCaptchaResponse = ''; + } + } + + $remoteIp = $this->remoteAddress->getRemoteAddress(); + $options['threshold'] = $this->config->getMinFrontendScore(); + + if (!$this->validate->validate($reCaptchaResponse, $remoteIp, $options)) { + $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); + + $jsonPayload = $this->serializer->serialize([ + 'errors' => true, + 'message' => $this->config->getErrorDescription(), + ]); + + $controller->getResponse()->representJson($jsonPayload); + } + } + } +} diff --git a/ReCaptcha/Observer/Frontend/ContactFormObserver.php b/ReCaptcha/Observer/Frontend/ContactFormObserver.php new file mode 100644 index 00000000..d156486e --- /dev/null +++ b/ReCaptcha/Observer/Frontend/ContactFormObserver.php @@ -0,0 +1,71 @@ +url = $url; + $this->config = $config; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendContact()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->url->getUrl('contact/index/index'); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Observer/Frontend/CreateUserObserver.php b/ReCaptcha/Observer/Frontend/CreateUserObserver.php new file mode 100644 index 00000000..f6bfa4c6 --- /dev/null +++ b/ReCaptcha/Observer/Frontend/CreateUserObserver.php @@ -0,0 +1,70 @@ +url = $url; + $this->config = $config; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendCreateUser()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->url->getUrl('*/*/create', ['_secure' => true]); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Observer/Frontend/ForgotPasswordObserver.php b/ReCaptcha/Observer/Frontend/ForgotPasswordObserver.php new file mode 100644 index 00000000..a6a78b62 --- /dev/null +++ b/ReCaptcha/Observer/Frontend/ForgotPasswordObserver.php @@ -0,0 +1,71 @@ +url = $url; + $this->config = $config; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendForgot()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->url->getUrl('*/*/forgotpassword', ['_secure' => true]); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Observer/Frontend/LoginObserver.php b/ReCaptcha/Observer/Frontend/LoginObserver.php new file mode 100644 index 00000000..867c9d9a --- /dev/null +++ b/ReCaptcha/Observer/Frontend/LoginObserver.php @@ -0,0 +1,71 @@ +beforeAuthUrlProvider = $beforeAuthUrlProvider; + $this->config = $config; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendLogin()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->beforeAuthUrlProvider->execute(); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Observer/Frontend/NewsletterObserver.php b/ReCaptcha/Observer/Frontend/NewsletterObserver.php new file mode 100644 index 00000000..a6099a5e --- /dev/null +++ b/ReCaptcha/Observer/Frontend/NewsletterObserver.php @@ -0,0 +1,71 @@ +redirect = $redirect; + $this->config = $config; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendNewsletter()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->redirect->getRefererUrl(); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Observer/Frontend/ReviewFormObserver.php b/ReCaptcha/Observer/Frontend/ReviewFormObserver.php new file mode 100644 index 00000000..0ff531ae --- /dev/null +++ b/ReCaptcha/Observer/Frontend/ReviewFormObserver.php @@ -0,0 +1,71 @@ +redirect = $redirect; + $this->config = $config; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isAreaEnabled(Area::AREA_FRONTEND) && $this->config->isEnabledFrontendReview()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->redirect->getRedirectUrl(); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptcha/Observer/ReCaptchaObserver.php b/ReCaptcha/Observer/ReCaptchaObserver.php deleted file mode 100644 index 5b953efa..00000000 --- a/ReCaptcha/Observer/ReCaptchaObserver.php +++ /dev/null @@ -1,88 +0,0 @@ -responseProvider = $responseProvider; - $this->validate = $validate; - $this->failureProvider = $failureProvider; - $this->remoteAddress = $remoteAddress; - $this->isCheckRequired = $isCheckRequired; - } - - /** - * @param Observer $observer - * @return void - */ - public function execute(Observer $observer) - { - if ($this->isCheckRequired->execute()) { - $reCaptchaResponse = $this->responseProvider->execute(); - $remoteIp = $this->remoteAddress->getRemoteAddress(); - - /** @var Action $controller */ - $controller = $observer->getControllerAction(); - - if (!$this->validate->validate($reCaptchaResponse, $remoteIp)) { - $this->failureProvider->execute($controller ? $controller->getResponse() : null); - } - } - } -} diff --git a/ReCaptcha/etc/adminhtml/di.xml b/ReCaptcha/etc/adminhtml/di.xml deleted file mode 100644 index 13ca2064..00000000 --- a/ReCaptcha/etc/adminhtml/di.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - adminhtml - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Adminhtml\Login - Magento\ReCaptcha\Model\Provider\Failure\AuthenticationExceptionFailure - - - - - - - */*/forgotpassword - - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\ForgotPassword - - - - - adminhtml - email - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Adminhtml\ForgotPassword - Magento\ReCaptcha\Model\Provider\Failure\ForgotPasswordObserver - - - diff --git a/ReCaptcha/etc/adminhtml/system.xml b/ReCaptcha/etc/adminhtml/system.xml index 22f9cde8..93680bae 100644 --- a/ReCaptcha/etc/adminhtml/system.xml +++ b/ReCaptcha/etc/adminhtml/system.xml @@ -172,7 +172,7 @@ 1 - Requires an Invisible ReCaptcha v2 or ReCaptcha v3 key. If enabled, a badge will be displayed in every page. @@ -181,14 +181,6 @@ 1 - - - Magento\Config\Model\Config\Source\Yesno - - 1 - - diff --git a/ReCaptcha/etc/config.xml b/ReCaptcha/etc/config.xml index f9c6d40f..6cf187fd 100644 --- a/ReCaptcha/etc/config.xml +++ b/ReCaptcha/etc/config.xml @@ -30,7 +30,6 @@ 1 1 1 - 1 0.6 diff --git a/ReCaptcha/etc/di.xml b/ReCaptcha/etc/di.xml index b603ca8a..7de8903c 100644 --- a/ReCaptcha/etc/di.xml +++ b/ReCaptcha/etc/di.xml @@ -7,7 +7,10 @@ --> + + @@ -21,22 +24,6 @@ - - - - - - - - - - - - - - diff --git a/ReCaptcha/etc/frontend/di.xml b/ReCaptcha/etc/frontend/di.xml index be8bc946..74fd49f6 100644 --- a/ReCaptcha/etc/frontend/di.xml +++ b/ReCaptcha/etc/frontend/di.xml @@ -16,231 +16,11 @@ - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\BeforeAuthUrlProvider - - - - - recaptcha/frontend/enabled_login - frontend - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\Login - Magento\ReCaptcha\Model\Provider\Failure\LoginObserver - - - - - - - */*/forgotpassword - - true - - - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\ForgotPassword - - - - - recaptcha/frontend/enabled_forgot - frontend - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\ForgotPassword - Magento\ReCaptcha\Model\Provider\Failure\ForgotPasswordObserver - - - - - - - */*/create - - true - - - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\CreateUser - - - - - recaptcha/frontend/enabled_create - frontend - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\CreateUser - Magento\ReCaptcha\Model\Provider\Failure\CreateUserObserver - - - - - - - contact/index/index - - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\ContactForm - - - - - recaptcha/frontend/enabled_contact - frontend - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\ContactForm - Magento\ReCaptcha\Model\Provider\Failure\ContactFormObserver - - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\Login - Magento\ReCaptcha\Model\Provider\Response\AjaxResponseProvider - Magento\ReCaptcha\Model\Provider\Failure\AjaxResponseFailure - - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\SendFriendForm - Magento\ReCaptcha\Model\Provider\Response\DefaultResponseProvider - Magento\ReCaptcha\Model\Provider\Failure\SendFriendObserver - - - - - recaptcha/frontend/enabled_sendfriend - frontend - - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\ReferrerUrlProvider - - - - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\RefererProvider - - - - - recaptcha/frontend/enabled_review - frontend - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\ReviewForm - Magento\ReCaptcha\Model\Provider\Failure\ReviewFormObserver - - - - - - - recaptcha/frontend/enabled_newsletter - frontend - - - - - Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl\ReferrerUrlProvider - - - - - Magento\ReCaptcha\Model\Provider\IsCheckRequired\Frontend\Newsletter - Magento\ReCaptcha\Model\Provider\Response\DefaultResponseProvider - Magento\ReCaptcha\Model\Provider\Failure\NewsletterObserver - - diff --git a/ReCaptcha/etc/frontend/events.xml b/ReCaptcha/etc/frontend/events.xml index 7e8a8f63..5da85214 100644 --- a/ReCaptcha/etc/frontend/events.xml +++ b/ReCaptcha/etc/frontend/events.xml @@ -29,7 +29,4 @@ - - - diff --git a/ReCaptcha/view/frontend/web/css/source/_module.less b/ReCaptcha/view/frontend/web/css/source/_module.less index 39633d03..cbfd43e7 100644 --- a/ReCaptcha/view/frontend/web/css/source/_module.less +++ b/ReCaptcha/view/frontend/web/css/source/_module.less @@ -9,7 +9,7 @@ } .required-captcha.checkbox{ - position: absolute; + position: absolute; display: block; visibility: visible; overflow: hidden; @@ -23,7 +23,3 @@ margin-bottom: 10px; } } - -.form.send.friend .g-recaptcha { - margin-top: 40px; -} diff --git a/ReCaptchaSendFriend/Model/ConfigEnabled.php b/ReCaptchaSendFriend/Model/ConfigEnabled.php new file mode 100644 index 00000000..648306dc --- /dev/null +++ b/ReCaptchaSendFriend/Model/ConfigEnabled.php @@ -0,0 +1,59 @@ +reCaptchaConfig = $reCaptchaConfig; + $this->scopeConfig = $scopeConfig; + } + + /** + * Return true if enabled on frontend send to friend + * @return bool + */ + public function isEnabled(): bool + { + if (!$this->reCaptchaConfig->isEnabledFrontend()) { + return false; + } + + return (bool)$this->scopeConfig->getValue( + static::XML_PATH_ENABLED_FRONTEND_SENDFRIEND, + ScopeInterface::SCOPE_WEBSITE + ); + } +} diff --git a/ReCaptchaSendFriend/Observer/SendFriendObserver.php b/ReCaptchaSendFriend/Observer/SendFriendObserver.php new file mode 100644 index 00000000..396b4f8b --- /dev/null +++ b/ReCaptchaSendFriend/Observer/SendFriendObserver.php @@ -0,0 +1,71 @@ +redirect = $redirect; + $this->config = $config; + $this->captchaRequestHandler = $captchaRequestHandler; + } + + /** + * @param Observer $observer + * @return void + * @throws LocalizedException + */ + public function execute(Observer $observer): void + { + if ($this->config->isEnabled()) { + /** @var Action $controller */ + $controller = $observer->getControllerAction(); + $request = $controller->getRequest(); + $response = $controller->getResponse(); + $redirectOnFailureUrl = $this->redirect->getRefererUrl(); + + $this->captchaRequestHandler->execute(Area::AREA_ADMINHTML, $request, $response, $redirectOnFailureUrl); + } + } +} diff --git a/ReCaptchaSendFriend/README.md b/ReCaptchaSendFriend/README.md new file mode 100644 index 00000000..2eb34c63 --- /dev/null +++ b/ReCaptchaSendFriend/README.md @@ -0,0 +1 @@ +Please refer to: https://github.com/magento/security-package \ No newline at end of file diff --git a/ReCaptchaSendFriend/composer.json b/ReCaptchaSendFriend/composer.json new file mode 100644 index 00000000..3b9fa29d --- /dev/null +++ b/ReCaptchaSendFriend/composer.json @@ -0,0 +1,26 @@ +{ + "name": "magento/module-re-captcha-send-friend", + "version": "1.0.0", + "description": "Google reCaptcha integration for Magento2", + "require": { + "php": "~7.1.3||~7.2.0||~7.3.0", + "magento/framework": "102.0.*", + "magento/module-re-captcha": "*" + }, + "authors": [ + { + "name": "Riccardo Tempesta", + "email": "riccardo.tempesta@magespecialist.it" + } + ], + "type": "magento2-module", + "license": "OSL-3.0", + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Magento\\ReCaptchaSendFriend\\": "" + } + } +} diff --git a/ReCaptchaSendFriend/etc/adminhtml/system.xml b/ReCaptchaSendFriend/etc/adminhtml/system.xml new file mode 100644 index 00000000..f020f641 --- /dev/null +++ b/ReCaptchaSendFriend/etc/adminhtml/system.xml @@ -0,0 +1,24 @@ + + + + +
+ + + + Magento\Config\Model\Config\Source\Yesno + + 1 + + + +
+
+
diff --git a/ReCaptchaSendFriend/etc/config.xml b/ReCaptchaSendFriend/etc/config.xml new file mode 100644 index 00000000..58255293 --- /dev/null +++ b/ReCaptchaSendFriend/etc/config.xml @@ -0,0 +1,17 @@ + + + + + + + 1 + + + + diff --git a/ReCaptchaSendFriend/etc/di.xml b/ReCaptchaSendFriend/etc/di.xml new file mode 100644 index 00000000..52b92e82 --- /dev/null +++ b/ReCaptchaSendFriend/etc/di.xml @@ -0,0 +1,23 @@ + + + + + + Magento\ReCaptchaSendFriend\Model\ConfigEnabled + + + + + + + Magento\ReCaptchaSendFriend\Model\ConfigEnabled + + + + diff --git a/ReCaptchaSendFriend/etc/frontend/events.xml b/ReCaptchaSendFriend/etc/frontend/events.xml new file mode 100644 index 00000000..d28d8ba7 --- /dev/null +++ b/ReCaptchaSendFriend/etc/frontend/events.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/ReCaptchaSendFriend/etc/module.xml b/ReCaptchaSendFriend/etc/module.xml new file mode 100644 index 00000000..9a3e3a08 --- /dev/null +++ b/ReCaptchaSendFriend/etc/module.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/ReCaptchaSendFriend/registration.php b/ReCaptchaSendFriend/registration.php new file mode 100644 index 00000000..9f72af49 --- /dev/null +++ b/ReCaptchaSendFriend/registration.php @@ -0,0 +1,12 @@ + - - diff --git a/ReCaptchaSendFriend/view/frontend/web/css/source/_module.less b/ReCaptchaSendFriend/view/frontend/web/css/source/_module.less new file mode 100644 index 00000000..495f4b46 --- /dev/null +++ b/ReCaptchaSendFriend/view/frontend/web/css/source/_module.less @@ -0,0 +1,7 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +.form.send.friend .g-recaptcha { + margin-top: 40px; +}