diff --git a/ReCaptcha/Model/CaptchaValidator.php b/ReCaptcha/Model/CaptchaValidator.php deleted file mode 100644 index 85670699..00000000 --- a/ReCaptcha/Model/CaptchaValidator.php +++ /dev/null @@ -1,52 +0,0 @@ -getPrivateKey(); - - if ($reCaptchaResponse) { - // @codingStandardsIgnoreStart - $reCaptcha = new ReCaptcha($secret); - // @codingStandardsIgnoreEmd - - if ($validationConfig->getCaptchaType() === 'recaptcha_v3') { - if (isset($options['threshold'])) { - $reCaptcha->setScoreThreshold($validationConfig->getScoreThreshold()); - } - } - $res = $reCaptcha->verify($reCaptchaResponse, $validationConfig->getRemoteIp()); - - if (($validationConfig->getCaptchaType() === 'recaptcha_v3') && ($res->getScore() === null)) { - throw new LocalizedException(__('Internal error: Make sure you are using reCaptcha V3 api keys')); - } - - if ($res->isSuccess()) { - return true; - } - } - - return false; - } -} diff --git a/ReCaptcha/composer.json b/ReCaptcha/composer.json deleted file mode 100644 index 0a7e2f61..00000000 --- a/ReCaptcha/composer.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "name": "magento/module-re-captcha", - "version": "3.0.0", - "description": "Google reCaptcha integration for Magento2", - "require": { - "php": "~7.1.3||~7.2.0||~7.3.0", - "magento/framework": "102.0.*", - "magento/magento-composer-installer": "*", - "magento/module-re-captcha-api": "*", - "google/recaptcha": "^1.2" - }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], - "type": "magento2-module", - "license": "OSL-3.0", - "autoload": { - "files": [ - "registration.php" - ], - "psr-4": { - "Magento\\ReCaptcha\\": "" - } - } -} diff --git a/ReCaptcha/etc/acl.xml b/ReCaptcha/etc/acl.xml deleted file mode 100644 index 292eb6ae..00000000 --- a/ReCaptcha/etc/acl.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/ReCaptcha/etc/di.xml b/ReCaptcha/etc/di.xml deleted file mode 100644 index b7a7e8fc..00000000 --- a/ReCaptcha/etc/di.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - diff --git a/ReCaptchaAdminUi/Model/CaptchaConfig.php b/ReCaptchaAdminUi/Model/CaptchaConfig.php deleted file mode 100644 index febbaffc..00000000 --- a/ReCaptchaAdminUi/Model/CaptchaConfig.php +++ /dev/null @@ -1,149 +0,0 @@ -scopeConfig = $scopeConfig; - } - - /** - * @inheritdoc - */ - public function getPublicKey(): string - { - return trim((string)$this->scopeConfig->getValue(self::XML_PATH_PUBLIC_KEY)); - } - - /** - * @inheritdoc - */ - public function getPrivateKey(): string - { - return trim((string)$this->scopeConfig->getValue(self::XML_PATH_PRIVATE_KEY)); - } - - /** - * @inheritdoc - */ - public function getCaptchaType(): string - { - return (string)$this->scopeConfig->getValue(self::XML_PATH_TYPE); - } - - /** - * @inheritdoc - */ - public function getSize(): string - { - return (string)$this->scopeConfig->getValue(self::XML_PATH_SIZE); - } - - /** - * @inheritdoc - */ - public function getTheme(): string - { - return (string)$this->scopeConfig->getValue(self::XML_PATH_THEME); - } - - /** - * @inheritdoc - */ - public function getScoreThreshold(): float - { - return min(1.0, max(0.1, (float) $this->scopeConfig->getValue( - self::XML_PATH_SCORE_THRESHOLD - ))); - } - - /** - * @inheritdoc - */ - public function getErrorMessage(): Phrase - { - if ($this->getCaptchaType() === 'recaptcha_v3') { - return __('You cannot proceed with such operation, your reCAPTCHA reputation is too low.'); - } - - return __('Incorrect reCAPTCHA validation.'); - } - - /** - * @inheritdoc - */ - public function getLanguageCode(): string - { - return (string)$this->scopeConfig->getValue( - self::XML_PATH_LANGUAGE_CODE - ); - } - - /** - * @inheritdoc - */ - public function getInvisibleBadgePosition(): string - { - return (string)$this->scopeConfig->getValue( - self::XML_PATH_POSITION - ); - } - - /** - * @inheritdoc - */ - public function isCaptchaEnabledFor(string $key): bool - { - if (!$this->areKeysConfigured()) { - return false; - } - - $flag = self::XML_PATH_IS_ENABLED_FOR . $key; - return $this->scopeConfig->isSetFlag($flag); - } - - /** - * Return true if reCAPTCHA keys (public and private) are configured - * - * @return bool - */ - private function areKeysConfigured(): bool - { - return $this->getPrivateKey() && $this->getPublicKey(); - } -} diff --git a/ReCaptchaAdminUi/Model/CaptchaTypeResolver.php b/ReCaptchaAdminUi/Model/CaptchaTypeResolver.php new file mode 100644 index 00000000..0b89bb25 --- /dev/null +++ b/ReCaptchaAdminUi/Model/CaptchaTypeResolver.php @@ -0,0 +1,44 @@ +scopeConfig = $scopeConfig; + } + + /** + * @inheritdoc + */ + public function getCaptchaTypeFor(string $key): ?string + { + $type = $this->scopeConfig->getValue( + self::XML_PATH_TYPE_FOR . $key + ); + return $type; + } +} diff --git a/ReCaptchaAdminUi/Model/OptionSource.php b/ReCaptchaAdminUi/Model/OptionSource.php new file mode 100644 index 00000000..12cc6a92 --- /dev/null +++ b/ReCaptchaAdminUi/Model/OptionSource.php @@ -0,0 +1,42 @@ +options = $options; + } + + /** + * @inheritDoc + */ + public function toOptionArray(): array + { + return array_values($this->options); + } +} diff --git a/ReCaptchaAdminUi/composer.json b/ReCaptchaAdminUi/composer.json index 300cb638..1501ffbf 100644 --- a/ReCaptchaAdminUi/composer.json +++ b/ReCaptchaAdminUi/composer.json @@ -1,19 +1,13 @@ { "name": "magento/module-re-captcha-admin-ui", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "description": "Google reCAPTCHA integration for Magento2", "require": { "php": "~7.1.3||~7.2.0||~7.3.0", "magento/framework": "102.0.*", "magento/module-config": "*", - "magento/module-re-captcha-api": "*" + "magento/module-re-captcha-api": "*", + "magento/module-re-captcha-ui": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaAdminUi/etc/adminhtml/di.xml b/ReCaptchaAdminUi/etc/adminhtml/di.xml index 0771cfc2..547a2a93 100644 --- a/ReCaptchaAdminUi/etc/adminhtml/di.xml +++ b/ReCaptchaAdminUi/etc/adminhtml/di.xml @@ -7,6 +7,6 @@ --> - + diff --git a/ReCaptchaAdminUi/etc/adminhtml/system.xml b/ReCaptchaAdminUi/etc/adminhtml/system.xml index d31a5448..7a196afe 100644 --- a/ReCaptchaAdminUi/etc/adminhtml/system.xml +++ b/ReCaptchaAdminUi/etc/adminhtml/system.xml @@ -12,161 +12,29 @@ Security - + separator-top - Google reCAPTCHA + Google reCAPTCHA Admin Panel security - Magento_ReCaptcha::config + Magento_ReCaptchaUi::config - + Admin Panel - - - reCAPTCHA Type - Magento\ReCaptchaApi\Model\Config\Source\Type - - - - Google API Website Key - - - - Google API Secret Key - - - - Minimum Score Threshold - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. - - recaptcha_v3 - - - - - Theme - Magento\ReCaptchaApi\Model\Config\Source\Theme - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - Size - Magento\ReCaptchaApi\Model\Config\Source\Size - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - Language Code - - Applicable only to reCAPTCHA v2 ("I am not a robot"). - Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. - See supported Language codes. - - ]]> - - recaptcha - - - - - Invisible Badge Position - Magento\ReCaptchaApi\Model\Config\Source\Position - Applicable only to Invisible reCAPTCHA v2 or reCAPTCHA v3. - - recaptcha_v3,invisible - - + - - Storefront - - - reCAPTCHA Type - Magento\ReCaptchaApi\Model\Config\Source\Type - - - - Google API Website Key - - - - Google API Secret Key - - - - Minimum Score Threshold - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. - - recaptcha_v3 - - - - - Theme - Magento\ReCaptchaApi\Model\Config\Source\Theme - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - Size - Magento\ReCaptchaApi\Model\Config\Source\Size - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - Language Code - - Applicable only to reCAPTCHA v2 ("I am not a robot"). - Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. - See supported Language codes. - - ]]> - - recaptcha - - + + separator-top + Google reCAPTCHA Storefront + security + Magento_ReCaptchaUi::config - - Invisible Badge Position - Magento\ReCaptchaApi\Model\Config\Source\Position - Applicable only to Invisible reCAPTCHA v2 or reCAPTCHA v3. - - recaptcha_v3,invisible - - + + Storefront diff --git a/ReCaptchaAdminUi/etc/config.xml b/ReCaptchaAdminUi/etc/config.xml deleted file mode 100644 index 2fc7fc5c..00000000 --- a/ReCaptchaAdminUi/etc/config.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - recaptcha_v3 - normal - inline - light - - 0.9 - - - - diff --git a/ReCaptchaAdminUi/etc/di.xml b/ReCaptchaAdminUi/etc/di.xml index aed03b0b..b78aafe6 100644 --- a/ReCaptchaAdminUi/etc/di.xml +++ b/ReCaptchaAdminUi/etc/di.xml @@ -7,16 +7,15 @@ --> - + - - 1 - 1 - - - 1 - 1 + + + No + + - + diff --git a/ReCaptchaApi/Api/CaptchaConfigInterface.php b/ReCaptchaApi/Api/CaptchaConfigInterface.php deleted file mode 100644 index a98b85c4..00000000 --- a/ReCaptchaApi/Api/CaptchaConfigInterface.php +++ /dev/null @@ -1,100 +0,0 @@ - 'inline', 'label' => __('Inline')], - ['value' => 'bottomright', 'label' => __('Bottom Right')], - ['value' => 'bottomleft', 'label' => __('Bottom Left')], - ]; - } -} diff --git a/ReCaptchaApi/Model/Config/Source/Size.php b/ReCaptchaApi/Model/Config/Source/Size.php deleted file mode 100644 index d8413686..00000000 --- a/ReCaptchaApi/Model/Config/Source/Size.php +++ /dev/null @@ -1,32 +0,0 @@ - 'normal', 'label' => __('Normal')], - ['value' => 'compact', 'label' => __('Compact')], - ]; - } -} diff --git a/ReCaptchaApi/Model/Config/Source/Theme.php b/ReCaptchaApi/Model/Config/Source/Theme.php deleted file mode 100644 index 4dfb054c..00000000 --- a/ReCaptchaApi/Model/Config/Source/Theme.php +++ /dev/null @@ -1,32 +0,0 @@ - 'light', 'label' => __('Light Theme')], - ['value' => 'dark', 'label' => __('Dark Theme')], - ]; - } -} diff --git a/ReCaptchaApi/Model/Config/Source/Type.php b/ReCaptchaApi/Model/Config/Source/Type.php deleted file mode 100644 index f3628d89..00000000 --- a/ReCaptchaApi/Model/Config/Source/Type.php +++ /dev/null @@ -1,32 +0,0 @@ - 'recaptcha_v3', 'label' => __('Invisible reCAPTCHA v3')], - ['value' => 'invisible', 'label' => __('Invisible reCAPTCHA v2')], - ['value' => 'recaptcha', 'label' => __('reCAPTCHA v2')], - ]; - } -} diff --git a/ReCaptchaApi/Model/IsInvisibleCaptcha.php b/ReCaptchaApi/Model/IsInvisibleCaptcha.php deleted file mode 100644 index da594c54..00000000 --- a/ReCaptchaApi/Model/IsInvisibleCaptcha.php +++ /dev/null @@ -1,40 +0,0 @@ -invisibleTypes = $invisibleTypes; - } - - /** - * @inheritdoc - */ - public function isInvisible(string $captchaType): bool - { - return in_array($captchaType, $this->invisibleTypes, true); - } -} diff --git a/ReCaptchaApi/composer.json b/ReCaptchaApi/composer.json deleted file mode 100644 index 540470f3..00000000 --- a/ReCaptchaApi/composer.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "magento/module-re-captcha-api", - "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.*" - }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], - "type": "magento2-module", - "license": "OSL-3.0", - "autoload": { - "files": [ - "registration.php" - ], - "psr-4": { - "Magento\\ReCaptchaApi\\": "" - } - } -} diff --git a/ReCaptchaApi/etc/di.xml b/ReCaptchaApi/etc/di.xml deleted file mode 100644 index 174b5ad4..00000000 --- a/ReCaptchaApi/etc/di.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - invisible - recaptcha_v3 - - - - diff --git a/ReCaptchaCheckout/Block/LayoutProcessor/Checkout/Onepage.php b/ReCaptchaCheckout/Block/LayoutProcessor/Checkout/Onepage.php index 77b04d9f..1dc20f65 100644 --- a/ReCaptchaCheckout/Block/LayoutProcessor/Checkout/Onepage.php +++ b/ReCaptchaCheckout/Block/LayoutProcessor/Checkout/Onepage.php @@ -8,8 +8,9 @@ namespace Magento\ReCaptchaCheckout\Block\LayoutProcessor\Checkout; use Magento\Checkout\Block\Checkout\LayoutProcessorInterface; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; -use Magento\ReCaptchaUi\Model\CaptchaUiSettingsProviderInterface; +use Magento\Framework\Exception\InputException; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; +use Magento\ReCaptchaUi\Model\UiConfigResolverInterface; /** * Checkout layout processor @@ -17,39 +18,44 @@ class Onepage implements LayoutProcessorInterface { /** - * @var CaptchaUiSettingsProviderInterface + * @var UiConfigResolverInterface */ - private $captchaUiSettingsProvider; + private $captchaUiConfigResolver; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** - * @param CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider - * @param CaptchaConfigInterface $captchaConfig + * @param UiConfigResolverInterface $captchaUiConfigResolver + * @param IsCaptchaEnabledInterface $isCaptchaEnabled */ public function __construct( - CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider, - CaptchaConfigInterface $captchaConfig + UiConfigResolverInterface $captchaUiConfigResolver, + IsCaptchaEnabledInterface $isCaptchaEnabled ) { - $this->captchaUiSettingsProvider = $captchaUiSettingsProvider; - $this->captchaConfig = $captchaConfig; + $this->captchaUiConfigResolver = $captchaUiConfigResolver; + $this->isCaptchaEnabled = $isCaptchaEnabled; } /** - * @inheritDoc + * {@inheritdoc} + * + * @param array $jsLayout + * @return array + * @throws InputException */ public function process($jsLayout) { - if ($this->captchaConfig->isCaptchaEnabledFor('customer_login')) { + $key = 'customer_login'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['customer-email']['children'] - ['recaptcha']['settings'] = $this->captchaUiSettingsProvider->get(); + ['recaptcha']['settings'] = $this->captchaUiConfigResolver->get($key); $jsLayout['components']['checkout']['children']['authentication']['children'] - ['recaptcha']['settings'] = $this->captchaUiSettingsProvider->get(); + ['recaptcha']['settings'] = $this->captchaUiConfigResolver->get($key); } else { if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['customer-email']['children']['recaptcha'])) { diff --git a/ReCaptchaCheckout/composer.json b/ReCaptchaCheckout/composer.json index d94f264d..be2c373a 100644 --- a/ReCaptchaCheckout/composer.json +++ b/ReCaptchaCheckout/composer.json @@ -1,7 +1,6 @@ { "name": "magento/module-re-captcha-checkout", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "description": "Google reCAPTCHA integration for Magento2", "require": { "php": "~7.1.3||~7.2.0||~7.3.0", "magento/framework": "102.0.*", @@ -9,12 +8,6 @@ "magento/module-re-captcha-api": "*", "magento/module-re-captcha-ui": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaContact/Observer/ContactFormObserver.php b/ReCaptchaContact/Observer/ContactFormObserver.php index c81de36e..4ff8d9e0 100644 --- a/ReCaptchaContact/Observer/ContactFormObserver.php +++ b/ReCaptchaContact/Observer/ContactFormObserver.php @@ -12,7 +12,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\UrlInterface; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\RequestHandlerInterface; /** @@ -26,9 +26,9 @@ class ContactFormObserver implements ObserverInterface private $url; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var RequestHandlerInterface @@ -37,16 +37,16 @@ class ContactFormObserver implements ObserverInterface /** * @param UrlInterface $url - * @param CaptchaConfigInterface $captchaConfig + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param RequestHandlerInterface $requestHandler */ public function __construct( UrlInterface $url, - CaptchaConfigInterface $captchaConfig, + IsCaptchaEnabledInterface $isCaptchaEnabled, RequestHandlerInterface $requestHandler ) { $this->url = $url; - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->requestHandler = $requestHandler; } @@ -57,14 +57,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('contact')) { + $key = 'contact'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $redirectOnFailureUrl = $this->url->getUrl('*/*/index'); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaContact/composer.json b/ReCaptchaContact/composer.json index dde78157..37a02f40 100644 --- a/ReCaptchaContact/composer.json +++ b/ReCaptchaContact/composer.json @@ -1,19 +1,12 @@ { "name": "magento/module-re-captcha-contact", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "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-api": "*", "magento/module-re-captcha-ui": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaContact/etc/adminhtml/system.xml b/ReCaptchaContact/etc/adminhtml/system.xml index b4ad3b16..f2a55236 100644 --- a/ReCaptchaContact/etc/adminhtml/system.xml +++ b/ReCaptchaContact/etc/adminhtml/system.xml @@ -8,12 +8,12 @@ - - - + + Enable for Contact Us - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type diff --git a/ReCaptchaContact/etc/config.xml b/ReCaptchaContact/etc/config.xml index 05bef1de..a7060fc1 100644 --- a/ReCaptchaContact/etc/config.xml +++ b/ReCaptchaContact/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + + + diff --git a/ReCaptchaContact/view/frontend/layout/contact_index_index.xml b/ReCaptchaContact/view/frontend/layout/contact_index_index.xml index f3f44cfe..7068b36d 100644 --- a/ReCaptchaContact/view/frontend/layout/contact_index_index.xml +++ b/ReCaptchaContact/view/frontend/layout/contact_index_index.xml @@ -13,9 +13,9 @@ name="recaptcha" after="-" template="Magento_ReCaptchaFrontendUi::recaptcha.phtml" - ifconfig="recaptcha/frontend/enabled_for_contact"> + ifconfig="recaptcha_frontend/type_for/contact"> - contact + contact diff --git a/ReCaptchaCustomer/Model/AjaxLogin/CaptchaResponseResolver.php b/ReCaptchaCustomer/Model/AjaxLogin/CaptchaResponseResolver.php index 670a8866..dacd2c8f 100644 --- a/ReCaptchaCustomer/Model/AjaxLogin/CaptchaResponseResolver.php +++ b/ReCaptchaCustomer/Model/AjaxLogin/CaptchaResponseResolver.php @@ -7,7 +7,8 @@ namespace Magento\ReCaptchaCustomer\Model\AjaxLogin; -use Magento\Framework\App\Request\Http; +use Magento\Framework\App\PlainTextRequestInterface; +use Magento\Framework\App\RequestInterface; use Magento\Framework\Exception\InputException; use Magento\Framework\Serialize\SerializerInterface; use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; @@ -31,9 +32,13 @@ public function __construct(SerializerInterface $serializer) } /** - * @inheritdoc + * {@inheritdoc} + * + * @param RequestInterface|PlainTextRequestInterface $request + * @return string + * @throws InputException */ - public function resolve(Http $request): string + public function resolve(RequestInterface $request): string { $content = $request->getContent(); if (empty($content)) { diff --git a/ReCaptchaCustomer/Observer/AjaxLoginObserver.php b/ReCaptchaCustomer/Observer/AjaxLoginObserver.php index 8eea6ea2..1af97117 100644 --- a/ReCaptchaCustomer/Observer/AjaxLoginObserver.php +++ b/ReCaptchaCustomer/Observer/AjaxLoginObserver.php @@ -12,13 +12,11 @@ use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress; use Magento\Framework\Serialize\SerializerInterface; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; -use Magento\ReCaptchaApi\Api\CaptchaValidatorInterface; -use Magento\ReCaptchaApi\Api\Data\ValidationConfigInterface; -use Magento\ReCaptchaApi\Api\Data\ValidationConfigInterfaceFactory; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; +use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface; +use Magento\ReCaptchaValidationApi\Api\ValidatorInterface; /** * AjaxLoginObserver @@ -31,14 +29,14 @@ class AjaxLoginObserver implements ObserverInterface private $captchaResponseResolver; /** - * @var CaptchaValidatorInterface + * @var ValidationConfigResolverInterface */ - private $captchaValidator; + private $validationConfigResolver; /** - * @var RemoteAddress + * @var ValidatorInterface */ - private $remoteAddress; + private $captchaValidator; /** * @var ActionFlag @@ -51,40 +49,32 @@ class AjaxLoginObserver implements ObserverInterface private $serializer; /** - * @var CaptchaConfigInterface - */ - private $captchaConfig; - - /** - * @var ValidationConfigInterfaceFactory + * @var IsCaptchaEnabledInterface */ - private $validationConfigFactory; + private $isCaptchaEnabled; /** * @param CaptchaResponseResolverInterface $captchaResponseResolver - * @param CaptchaValidatorInterface $captchaValidator - * @param RemoteAddress $remoteAddress + * @param ValidationConfigResolverInterface $validationConfigResolver + * @param ValidatorInterface $captchaValidator * @param ActionFlag $actionFlag * @param SerializerInterface $serializer - * @param CaptchaConfigInterface $captchaConfig - * @param ValidationConfigInterfaceFactory $validationConfigFactory + * @param IsCaptchaEnabledInterface $isCaptchaEnabled */ public function __construct( CaptchaResponseResolverInterface $captchaResponseResolver, - CaptchaValidatorInterface $captchaValidator, - RemoteAddress $remoteAddress, + ValidationConfigResolverInterface $validationConfigResolver, + ValidatorInterface $captchaValidator, ActionFlag $actionFlag, SerializerInterface $serializer, - CaptchaConfigInterface $captchaConfig, - ValidationConfigInterfaceFactory $validationConfigFactory + IsCaptchaEnabledInterface $isCaptchaEnabled ) { $this->captchaResponseResolver = $captchaResponseResolver; + $this->validationConfigResolver = $validationConfigResolver; $this->captchaValidator = $captchaValidator; - $this->remoteAddress = $remoteAddress; $this->actionFlag = $actionFlag; $this->serializer = $serializer; - $this->captchaConfig = $captchaConfig; - $this->validationConfigFactory = $validationConfigFactory; + $this->isCaptchaEnabled = $isCaptchaEnabled; } /** @@ -94,30 +84,22 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('customer_login')) { + $key = 'customer_login'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $reCaptchaResponse = $this->captchaResponseResolver->resolve($request); - /** @var ValidationConfigInterface $validationConfig */ - $validationConfig = $this->validationConfigFactory->create( - [ - 'privateKey' => $this->captchaConfig->getPrivateKey(), - 'captchaType' => $this->captchaConfig->getCaptchaType(), - 'remoteIp' => $this->remoteAddress->getRemoteAddress(), - 'scoreThreshold' => $this->captchaConfig->getScoreThreshold(), - 'extensionAttributes' => null, - ] - ); + $validationConfig = $this->validationConfigResolver->get($key); if (!$this->captchaValidator->isValid($reCaptchaResponse, $validationConfig)) { $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); $jsonPayload = $this->serializer->serialize([ 'errors' => true, - 'message' => $this->captchaConfig->getErrorMessage(), + 'message' => $validationConfig->getValidationFailureMessage(), ]); $response->representJson($jsonPayload); } diff --git a/ReCaptchaCustomer/Observer/CreateCustomerObserver.php b/ReCaptchaCustomer/Observer/CreateCustomerObserver.php index bdd92304..c4e773c9 100644 --- a/ReCaptchaCustomer/Observer/CreateCustomerObserver.php +++ b/ReCaptchaCustomer/Observer/CreateCustomerObserver.php @@ -11,7 +11,7 @@ use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\UrlInterface; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\RequestHandlerInterface; /** @@ -25,9 +25,9 @@ class CreateCustomerObserver implements ObserverInterface private $url; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var RequestHandlerInterface @@ -36,16 +36,16 @@ class CreateCustomerObserver implements ObserverInterface /** * @param UrlInterface $url - * @param CaptchaConfigInterface $captchaConfig + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param RequestHandlerInterface $requestHandler */ public function __construct( UrlInterface $url, - CaptchaConfigInterface $captchaConfig, + IsCaptchaEnabledInterface $isCaptchaEnabled, RequestHandlerInterface $requestHandler ) { $this->url = $url; - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->requestHandler = $requestHandler; } @@ -56,14 +56,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('customer_create')) { + $key = 'customer_create'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $redirectOnFailureUrl = $this->url->getUrl('*/*/create', ['_secure' => true]); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaCustomer/Observer/ForgotPasswordObserver.php b/ReCaptchaCustomer/Observer/ForgotPasswordObserver.php index f2bd2e99..e352442b 100644 --- a/ReCaptchaCustomer/Observer/ForgotPasswordObserver.php +++ b/ReCaptchaCustomer/Observer/ForgotPasswordObserver.php @@ -12,7 +12,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\UrlInterface; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\RequestHandlerInterface; /** @@ -26,9 +26,9 @@ class ForgotPasswordObserver implements ObserverInterface private $url; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var RequestHandlerInterface @@ -37,16 +37,16 @@ class ForgotPasswordObserver implements ObserverInterface /** * @param UrlInterface $url - * @param CaptchaConfigInterface $captchaConfig + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param RequestHandlerInterface $requestHandler */ public function __construct( UrlInterface $url, - CaptchaConfigInterface $captchaConfig, + IsCaptchaEnabledInterface $isCaptchaEnabled, RequestHandlerInterface $requestHandler ) { $this->url = $url; - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->requestHandler = $requestHandler; } @@ -57,14 +57,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('customer_forgot_password')) { + $key = 'customer_forgot_password'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor('customer_forgot_password')) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $redirectOnFailureUrl = $this->url->getUrl('*/*/forgotpassword', ['_secure' => true]); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaCustomer/Observer/LoginObserver.php b/ReCaptchaCustomer/Observer/LoginObserver.php index cceb972e..808b0d41 100644 --- a/ReCaptchaCustomer/Observer/LoginObserver.php +++ b/ReCaptchaCustomer/Observer/LoginObserver.php @@ -13,7 +13,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Session\SessionManagerInterface; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\RequestHandlerInterface; /** @@ -22,9 +22,9 @@ class LoginObserver implements ObserverInterface { /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var RequestHandlerInterface @@ -42,18 +42,18 @@ class LoginObserver implements ObserverInterface private $url; /** - * @param CaptchaConfigInterface $captchaConfig + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param RequestHandlerInterface $requestHandler * @param SessionManagerInterface $sessionManager * @param Url $url */ public function __construct( - CaptchaConfigInterface $captchaConfig, + IsCaptchaEnabledInterface $isCaptchaEnabled, RequestHandlerInterface $requestHandler, SessionManagerInterface $sessionManager, Url $url ) { - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->requestHandler = $requestHandler; $this->sessionManager = $sessionManager; $this->url = $url; @@ -66,14 +66,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('customer_login')) { + $key = 'customer_login'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $redirectOnFailureUrl = $this->sessionManager->getBeforeAuthUrl() ?: $this->url->getLoginUrl(); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaCustomer/Plugin/Block/Account/InjectRecaptchaInAuthenticationPopup.php b/ReCaptchaCustomer/Plugin/Block/Account/InjectRecaptchaInAuthenticationPopup.php index f0ce5809..57ead070 100644 --- a/ReCaptchaCustomer/Plugin/Block/Account/InjectRecaptchaInAuthenticationPopup.php +++ b/ReCaptchaCustomer/Plugin/Block/Account/InjectRecaptchaInAuthenticationPopup.php @@ -8,9 +8,10 @@ namespace Magento\ReCaptchaCustomer\Plugin\Block\Account; use Magento\Customer\Block\Account\AuthenticationPopup; +use Magento\Framework\Exception\InputException; use Magento\Framework\Serialize\Serializer\Json; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; -use Magento\ReCaptchaUi\Model\CaptchaUiSettingsProviderInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; +use Magento\ReCaptchaUi\Model\UiConfigResolverInterface; /** * Inject authentication popup in layout @@ -18,14 +19,14 @@ class InjectRecaptchaInAuthenticationPopup { /** - * @var CaptchaUiSettingsProviderInterface + * @var UiConfigResolverInterface */ - private $captchaUiSettingsProvider; + private $captchaUiConfigResolver; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var Json @@ -33,17 +34,17 @@ class InjectRecaptchaInAuthenticationPopup private $serializer; /** - * @param CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider - * @param CaptchaConfigInterface $captchaConfig + * @param UiConfigResolverInterface $captchaUiConfigResolver + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param Json $serializer */ public function __construct( - CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider, - CaptchaConfigInterface $captchaConfig, + UiConfigResolverInterface $captchaUiConfigResolver, + IsCaptchaEnabledInterface $isCaptchaEnabled, Json $serializer ) { - $this->captchaUiSettingsProvider = $captchaUiSettingsProvider; - $this->captchaConfig = $captchaConfig; + $this->captchaUiConfigResolver = $captchaUiConfigResolver; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->serializer = $serializer; } @@ -51,15 +52,17 @@ public function __construct( * @param AuthenticationPopup $subject * @param string $result * @return string + * @throws InputException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetJsLayout(AuthenticationPopup $subject, $result) { $layout = $this->serializer->unserialize($result); + $key = 'customer_login'; - if ($this->captchaConfig->isCaptchaEnabledFor('customer_login')) { + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { $layout['components']['authenticationPopup']['children']['recaptcha']['settings'] - = $this->captchaUiSettingsProvider->get(); + = $this->captchaUiConfigResolver->get($key); } else { unset($layout['components']['authenticationPopup']['children']['recaptcha']); } diff --git a/ReCaptchaCustomer/composer.json b/ReCaptchaCustomer/composer.json index 0088c10b..667906eb 100644 --- a/ReCaptchaCustomer/composer.json +++ b/ReCaptchaCustomer/composer.json @@ -1,20 +1,14 @@ { "name": "magento/module-re-captcha-customer", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "description": "Google reCAPTCHA integration for Magento2", "require": { "php": "~7.1.3||~7.2.0||~7.3.0", "magento/framework": "102.0.*", "magento/module-customer": "*", "magento/module-re-captcha-api": "*", - "magento/module-re-captcha-ui": "*" + "magento/module-re-captcha-ui": "*", + "magento/module-re-captcha-validation-api": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaCustomer/etc/adminhtml/system.xml b/ReCaptchaCustomer/etc/adminhtml/system.xml index a31252b1..c83b515a 100644 --- a/ReCaptchaCustomer/etc/adminhtml/system.xml +++ b/ReCaptchaCustomer/etc/adminhtml/system.xml @@ -8,22 +8,22 @@ - - - + + Enable for Customer Login - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type - Enable for Forgot Password - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type - Enable for Create New Customer Account - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type diff --git a/ReCaptchaCustomer/etc/config.xml b/ReCaptchaCustomer/etc/config.xml index 2d9e5ef5..e3ead5e4 100644 --- a/ReCaptchaCustomer/etc/config.xml +++ b/ReCaptchaCustomer/etc/config.xml @@ -8,12 +8,12 @@ - - - 1 - 1 - 1 - - + + + + + + + diff --git a/ReCaptchaCustomer/view/frontend/layout/customer_account_create.xml b/ReCaptchaCustomer/view/frontend/layout/customer_account_create.xml index 1ffcf250..cb9186f6 100644 --- a/ReCaptchaCustomer/view/frontend/layout/customer_account_create.xml +++ b/ReCaptchaCustomer/view/frontend/layout/customer_account_create.xml @@ -14,9 +14,9 @@ name="recaptcha" after="-" template="Magento_ReCaptchaFrontendUi::recaptcha.phtml" - ifconfig="recaptcha/frontend/enabled_for_customer_create"> + ifconfig="recaptcha_frontend/type_for/customer_create"> - customer_create + customer_create diff --git a/ReCaptchaCustomer/view/frontend/layout/customer_account_forgotpassword.xml b/ReCaptchaCustomer/view/frontend/layout/customer_account_forgotpassword.xml index 1bc7ca9b..77127af3 100644 --- a/ReCaptchaCustomer/view/frontend/layout/customer_account_forgotpassword.xml +++ b/ReCaptchaCustomer/view/frontend/layout/customer_account_forgotpassword.xml @@ -13,9 +13,9 @@ name="recaptcha" after="-" template="Magento_ReCaptchaFrontendUi::recaptcha.phtml" - ifconfig="recaptcha/frontend/enabled_for_customer_forgot_password"> + ifconfig="recaptcha_frontend/type_for/customer_forgot_password"> - customer_forgot_password + customer_forgot_password diff --git a/ReCaptchaCustomer/view/frontend/layout/customer_account_login.xml b/ReCaptchaCustomer/view/frontend/layout/customer_account_login.xml index deb76f0a..70c005bf 100644 --- a/ReCaptchaCustomer/view/frontend/layout/customer_account_login.xml +++ b/ReCaptchaCustomer/view/frontend/layout/customer_account_login.xml @@ -13,9 +13,9 @@ name="recaptcha" after="-" template="Magento_ReCaptchaFrontendUi::recaptcha.phtml" - ifconfig="recaptcha/frontend/enabled_for_customer_login"> + ifconfig="recaptcha_frontend/type_for/customer_login"> - customer_login + customer_login diff --git a/ReCaptchaFrontendUi/Model/CaptchaConfig.php b/ReCaptchaFrontendUi/Model/CaptchaConfig.php deleted file mode 100644 index 57c02f86..00000000 --- a/ReCaptchaFrontendUi/Model/CaptchaConfig.php +++ /dev/null @@ -1,159 +0,0 @@ -scopeConfig = $scopeConfig; - } - - /** - * @inheritdoc - */ - public function getPublicKey(): string - { - return trim((string)$this->scopeConfig->getValue(self::XML_PATH_PUBLIC_KEY, ScopeInterface::SCOPE_WEBSITE)); - } - - /** - * @inheritdoc - */ - public function getPrivateKey(): string - { - return trim((string)$this->scopeConfig->getValue(self::XML_PATH_PRIVATE_KEY, ScopeInterface::SCOPE_WEBSITE)); - } - - /** - * @inheritdoc - */ - public function getCaptchaType(): string - { - return (string)$this->scopeConfig->getValue(self::XML_PATH_TYPE); - } - - /** - * @inheritdoc - */ - public function getSize(): string - { - return (string)$this->scopeConfig->getValue( - self::XML_PATH_SIZE, - ScopeInterface::SCOPE_WEBSITE - ); - } - - /** - * @inheritdoc - */ - public function getTheme(): string - { - return (string)$this->scopeConfig->getValue( - self::XML_PATH_THEME, - ScopeInterface::SCOPE_WEBSITE - ); - } - - /** - * Get language code - * @return string - */ - public function getLanguageCode(): string - { - return (string)$this->scopeConfig->getValue( - self::XML_PATH_LANGUAGE_CODE, - ScopeInterface::SCOPE_STORE - ); - } - - /** - * @inheritdoc - */ - public function getInvisibleBadgePosition(): string - { - return (string)$this->scopeConfig->getValue( - self::XML_PATH_POSITION, - ScopeInterface::SCOPE_WEBSITE - ); - } - - /** - * @inheritdoc - */ - public function getScoreThreshold(): float - { - return min(1.0, max(0.1, (float)$this->scopeConfig->getValue( - self::XML_PATH_SCORE_THRESHOLD, - ScopeInterface::SCOPE_WEBSITE - ))); - } - - /** - * @inheritdoc - */ - public function getErrorMessage(): Phrase - { - if ($this->getCaptchaType() === 'recaptcha_v3') { - return __('You cannot proceed with such operation, your reCAPTCHA reputation is too low.'); - } - - return __('Incorrect reCAPTCHA validation.'); - } - - /** - * @inheritdoc - */ - public function isCaptchaEnabledFor(string $key): bool - { - if (!$this->areKeysConfigured()) { - return false; - } - - $flag = self::XML_PATH_IS_ENABLED_FOR . $key; - return $this->scopeConfig->isSetFlag($flag, ScopeInterface::SCOPE_WEBSITE); - } - - /** - * Return true if reCAPTCHA keys (public and private) are configured - * - * @return bool - */ - private function areKeysConfigured(): bool - { - return $this->getPrivateKey() && $this->getPublicKey(); - } -} diff --git a/ReCaptchaFrontendUi/Model/CaptchaTypeResolver.php b/ReCaptchaFrontendUi/Model/CaptchaTypeResolver.php new file mode 100644 index 00000000..56ce5019 --- /dev/null +++ b/ReCaptchaFrontendUi/Model/CaptchaTypeResolver.php @@ -0,0 +1,46 @@ +scopeConfig = $scopeConfig; + } + + /** + * @inheritdoc + */ + public function getCaptchaTypeFor(string $key): ?string + { + $type = $this->scopeConfig->getValue( + self::XML_PATH_TYPE_FOR . $key, + ScopeInterface::SCOPE_WEBSITE + ); + return $type; + } +} diff --git a/ReCaptchaFrontendUi/composer.json b/ReCaptchaFrontendUi/composer.json index c8d50f9a..588c3ba2 100644 --- a/ReCaptchaFrontendUi/composer.json +++ b/ReCaptchaFrontendUi/composer.json @@ -1,20 +1,14 @@ { "name": "magento/module-re-captcha-frontend-ui", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "description": "Google reCAPTCHA integration for Magento2", "require": { "php": "~7.1.3||~7.2.0||~7.3.0", "magento/framework": "102.0.*", "magento/module-store": "*", "magento/module-config": "*", - "magento/module-re-captcha-api": "*" + "magento/module-re-captcha-api": "*", + "magento/module-re-captcha-ui": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaFrontendUi/etc/config.xml b/ReCaptchaFrontendUi/etc/config.xml deleted file mode 100644 index f5c8ea7e..00000000 --- a/ReCaptchaFrontendUi/etc/config.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - recaptcha_v3 - normal - inline - light - - 0.6 - - - - diff --git a/ReCaptchaFrontendUi/etc/di.xml b/ReCaptchaFrontendUi/etc/di.xml deleted file mode 100644 index 22ba261a..00000000 --- a/ReCaptchaFrontendUi/etc/di.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - 1 - 1 - - - 1 - 1 - - - - diff --git a/ReCaptchaFrontendUi/etc/frontend/di.xml b/ReCaptchaFrontendUi/etc/frontend/di.xml index b5119d38..5f00c9a8 100644 --- a/ReCaptchaFrontendUi/etc/frontend/di.xml +++ b/ReCaptchaFrontendUi/etc/frontend/di.xml @@ -7,8 +7,8 @@ --> - + redirect = $redirect; - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->requestHandler = $requestHandler; } @@ -57,14 +57,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('newsletter')) { + $key = 'newsletter'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $redirectOnFailureUrl = $this->redirect->getRefererUrl(); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaNewsletter/Plugin/CheckReCaptchaVisibilityPlugin.php b/ReCaptchaNewsletter/Plugin/CheckReCaptchaVisibilityPlugin.php deleted file mode 100644 index d2dc8cf5..00000000 --- a/ReCaptchaNewsletter/Plugin/CheckReCaptchaVisibilityPlugin.php +++ /dev/null @@ -1,49 +0,0 @@ -isInvisibleCaptcha = $isInvisibleCaptcha; - } - - /** - * Check ReCaptcha visibility for newsletters. - * - * ReCaptcha should be disabled for newsletter subscription - * if ReCaptcha type is not set to 'invisible' or 'recaptcha_v3'. - * - * @param CaptchaConfigInterface $subject - * @param bool $result - * @param string $key - * @return bool - */ - public function afterIsCaptchaEnabledFor(CaptchaConfigInterface $subject, bool $result, string $key): bool - { - if ($result && $key === 'newsletter') { - $result = $this->isInvisibleCaptcha->isInvisible($subject->getCaptchaType()); - } - return $result; - } -} diff --git a/ReCaptchaNewsletter/composer.json b/ReCaptchaNewsletter/composer.json index a3bb1023..82f33bf0 100644 --- a/ReCaptchaNewsletter/composer.json +++ b/ReCaptchaNewsletter/composer.json @@ -1,19 +1,12 @@ { "name": "magento/module-re-captcha-newsletter", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "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-api": "*", "magento/module-re-captcha-ui": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaNewsletter/etc/adminhtml/system.xml b/ReCaptchaNewsletter/etc/adminhtml/system.xml index e9b6983c..5c3464b5 100644 --- a/ReCaptchaNewsletter/etc/adminhtml/system.xml +++ b/ReCaptchaNewsletter/etc/adminhtml/system.xml @@ -8,16 +8,13 @@ - - - + + Enable Invisible reCAPTCHA in Newsletter Subscription - Requires an Invisible reCAPTCHA v2 or reCAPTCHA v3 key. If enabled, a badge will be displayed in every page. - Magento\Config\Model\Config\Source\Yesno - - recaptcha_v3,invisible - + If enabled, a badge will be displayed in every page. + Magento\ReCaptchaApi\Model\OptionSource\Type diff --git a/ReCaptchaNewsletter/etc/config.xml b/ReCaptchaNewsletter/etc/config.xml index 33dbab69..56acb6bd 100644 --- a/ReCaptchaNewsletter/etc/config.xml +++ b/ReCaptchaNewsletter/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + + + diff --git a/ReCaptchaNewsletter/view/frontend/layout/default.xml b/ReCaptchaNewsletter/view/frontend/layout/default.xml index 0e125757..82c684a2 100644 --- a/ReCaptchaNewsletter/view/frontend/layout/default.xml +++ b/ReCaptchaNewsletter/view/frontend/layout/default.xml @@ -14,12 +14,12 @@ name="recaptcha-newsletter" after="-" template="Magento_ReCaptchaNewsletter::recaptcha_newsletter.phtml" - ifconfig="recaptcha/frontend/enabled_for_newsletter"> + ifconfig="recaptcha_frontend/type_for/newsletter"> - newsletter - + newsletter + true - + bottomright invisible diff --git a/ReCaptchaReview/Observer/ReviewFormObserver.php b/ReCaptchaReview/Observer/ReviewFormObserver.php index 2c33cf19..722ec3fb 100644 --- a/ReCaptchaReview/Observer/ReviewFormObserver.php +++ b/ReCaptchaReview/Observer/ReviewFormObserver.php @@ -12,7 +12,7 @@ use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\RequestHandlerInterface; /** @@ -26,9 +26,9 @@ class ReviewFormObserver implements ObserverInterface private $redirect; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var RequestHandlerInterface @@ -37,16 +37,16 @@ class ReviewFormObserver implements ObserverInterface /** * @param RedirectInterface $redirect - * @param CaptchaConfigInterface $captchaConfig + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param RequestHandlerInterface $requestHandler */ public function __construct( RedirectInterface $redirect, - CaptchaConfigInterface $captchaConfig, + IsCaptchaEnabledInterface $isCaptchaEnabled, RequestHandlerInterface $requestHandler ) { $this->redirect = $redirect; - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->requestHandler = $requestHandler; } @@ -57,14 +57,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('product_review')) { + $key = 'product_review'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $redirectOnFailureUrl = $this->redirect->getRedirectUrl(); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaReview/composer.json b/ReCaptchaReview/composer.json index c3efd1d5..9db49439 100644 --- a/ReCaptchaReview/composer.json +++ b/ReCaptchaReview/composer.json @@ -1,19 +1,12 @@ { "name": "magento/module-re-captcha-review", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "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-api": "*", "magento/module-re-captcha-ui": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaReview/etc/adminhtml/system.xml b/ReCaptchaReview/etc/adminhtml/system.xml index e3237e7b..908a7341 100644 --- a/ReCaptchaReview/etc/adminhtml/system.xml +++ b/ReCaptchaReview/etc/adminhtml/system.xml @@ -8,12 +8,12 @@ - - - + + Enable for Product Review - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type diff --git a/ReCaptchaReview/etc/config.xml b/ReCaptchaReview/etc/config.xml index ad78b62b..b8fc1dac 100644 --- a/ReCaptchaReview/etc/config.xml +++ b/ReCaptchaReview/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + + + diff --git a/ReCaptchaReview/view/frontend/layout/catalog_product_view.xml b/ReCaptchaReview/view/frontend/layout/catalog_product_view.xml index 4b319168..95b33228 100644 --- a/ReCaptchaReview/view/frontend/layout/catalog_product_view.xml +++ b/ReCaptchaReview/view/frontend/layout/catalog_product_view.xml @@ -11,9 +11,9 @@ + ifconfig="recaptcha_frontend/type_for/product_review"> - product_review + product_review diff --git a/ReCaptchaSendFriend/Observer/SendFriendObserver.php b/ReCaptchaSendFriend/Observer/SendFriendObserver.php index 962c8c0e..28fd6e07 100644 --- a/ReCaptchaSendFriend/Observer/SendFriendObserver.php +++ b/ReCaptchaSendFriend/Observer/SendFriendObserver.php @@ -12,7 +12,7 @@ use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\RequestHandlerInterface; /** @@ -26,9 +26,9 @@ class SendFriendObserver implements ObserverInterface private $redirect; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var RequestHandlerInterface @@ -37,16 +37,16 @@ class SendFriendObserver implements ObserverInterface /** * @param RedirectInterface $redirect - * @param CaptchaConfigInterface $captchaConfig + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param RequestHandlerInterface $requestHandler */ public function __construct( RedirectInterface $redirect, - CaptchaConfigInterface $captchaConfig, + IsCaptchaEnabledInterface $isCaptchaEnabled, RequestHandlerInterface $requestHandler ) { $this->redirect = $redirect; - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->requestHandler = $requestHandler; } @@ -57,14 +57,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('sendfriend')) { + $key = 'sendfriend'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); $response = $controller->getResponse(); $redirectOnFailureUrl = $this->redirect->getRefererUrl(); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaSendFriend/composer.json b/ReCaptchaSendFriend/composer.json index c1a04341..166b7050 100644 --- a/ReCaptchaSendFriend/composer.json +++ b/ReCaptchaSendFriend/composer.json @@ -1,19 +1,12 @@ { "name": "magento/module-re-captcha-send-friend", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "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-api": "*", "magento/module-re-captcha-ui": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaSendFriend/etc/adminhtml/system.xml b/ReCaptchaSendFriend/etc/adminhtml/system.xml index ecd03a0a..87c8cae8 100644 --- a/ReCaptchaSendFriend/etc/adminhtml/system.xml +++ b/ReCaptchaSendFriend/etc/adminhtml/system.xml @@ -8,12 +8,12 @@ - - - + + Enable for Send To Friend - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type diff --git a/ReCaptchaSendFriend/etc/config.xml b/ReCaptchaSendFriend/etc/config.xml index 1bdf3cc4..a23d1a11 100644 --- a/ReCaptchaSendFriend/etc/config.xml +++ b/ReCaptchaSendFriend/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + + + diff --git a/ReCaptchaSendFriend/view/frontend/layout/sendfriend_product_send.xml b/ReCaptchaSendFriend/view/frontend/layout/sendfriend_product_send.xml index 5d19ea7a..4fdf650b 100644 --- a/ReCaptchaSendFriend/view/frontend/layout/sendfriend_product_send.xml +++ b/ReCaptchaSendFriend/view/frontend/layout/sendfriend_product_send.xml @@ -13,9 +13,9 @@ name="recaptcha" after="-" template="Magento_ReCaptchaFrontendUi::recaptcha.phtml" - ifconfig="recaptcha/frontend/enabled_for_sendfriend"> + ifconfig="recaptcha_frontend/type_for/sendfriend"> - sendfriend + sendfriend diff --git a/ReCaptchaUi/Block/ReCaptcha.php b/ReCaptchaUi/Block/ReCaptcha.php index d16625c8..cca673d3 100644 --- a/ReCaptchaUi/Block/ReCaptcha.php +++ b/ReCaptchaUi/Block/ReCaptcha.php @@ -7,10 +7,11 @@ namespace Magento\ReCaptchaUi\Block; +use Magento\Framework\Exception\InputException; use Magento\Framework\Serialize\Serializer\Json; use Magento\Framework\View\Element\Template; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; -use Magento\ReCaptchaUi\Model\CaptchaUiSettingsProviderInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; +use Magento\ReCaptchaUi\Model\UiConfigResolverInterface; /** * @api @@ -18,14 +19,14 @@ class ReCaptcha extends Template { /** - * @var CaptchaUiSettingsProviderInterface + * @var UiConfigResolverInterface */ - private $captchaUiSettingsProvider; + private $captchaUiConfigResolver; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @var Json @@ -34,34 +35,37 @@ class ReCaptcha extends Template /** * @param Template\Context $context - * @param CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider - * @param CaptchaConfigInterface $captchaConfig + * @param UiConfigResolverInterface $captchaUiConfigResolver + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param Json $serializer * @param array $data */ public function __construct( Template\Context $context, - CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider, - CaptchaConfigInterface $captchaConfig, + UiConfigResolverInterface $captchaUiConfigResolver, + IsCaptchaEnabledInterface $isCaptchaEnabled, Json $serializer, array $data = [] ) { parent::__construct($context, $data); - $this->captchaUiSettingsProvider = $captchaUiSettingsProvider; - $this->captchaConfig = $captchaConfig; + $this->captchaUiConfigResolver = $captchaUiConfigResolver; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->serializer = $serializer; } /** - * Get current recaptcha ID + * Get reCAPTCHA ID */ public function getRecaptchaId() { - return (string)$this->getData('recaptcha_id') ?: 'recaptcha-' . md5($this->getNameInLayout()); + return (string)$this->getData('recaptcha_id') ?: 'recaptcha-' . sha1($this->getNameInLayout()); } /** - * @inheritdoc + * {@inheritdoc} + * + * @return string + * @throws InputException */ public function getJsLayout() { @@ -74,7 +78,7 @@ public function getJsLayout() $layout['components'][$this->getRecaptchaId()] = array_replace_recursive( [ - 'settings' => $this->getCaptchaSettings(), + 'settings' => $this->getCaptchaUiConfig(), ], $layout['components'][$this->getRecaptchaId()] ); @@ -84,28 +88,33 @@ public function getJsLayout() } /** + * Get UI config for reCAPTCHA rendering + * * @return array + * @throws InputException */ - public function getCaptchaSettings(): array + public function getCaptchaUiConfig(): array { - $settings = $this->getData('captcha_settings'); + $key = $this->getData('recaptcha_for'); + $uiConfig = $this->getData('captcha_ui_config'); - if ($settings) { - $settings = array_replace_recursive( $this->captchaUiSettingsProvider->get(), $settings); + if ($uiConfig) { + $uiConfig = array_replace_recursive($this->captchaUiConfigResolver->get($key), $uiConfig); } else { - $settings = $this->captchaUiSettingsProvider->get(); + $uiConfig = $this->captchaUiConfigResolver->get($key); } - return $settings; + return $uiConfig; } /** * @return string + * @throws InputException */ public function toHtml() { - $enabledFor = $this->getData('enabled_for'); - if (empty($enabledFor) || !$this->captchaConfig->isCaptchaEnabledFor($enabledFor)) { + $key = $this->getData('recaptcha_for'); + if (empty($key) || !$this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { return ''; } diff --git a/ReCaptchaUi/Model/CaptchaResponseResolver.php b/ReCaptchaUi/Model/CaptchaResponseResolver.php index 6f0bab7a..53c92d86 100644 --- a/ReCaptchaUi/Model/CaptchaResponseResolver.php +++ b/ReCaptchaUi/Model/CaptchaResponseResolver.php @@ -7,7 +7,7 @@ namespace Magento\ReCaptchaUi\Model; -use Magento\Framework\App\Request\Http; +use Magento\Framework\App\RequestInterface; use Magento\Framework\Exception\InputException; /** @@ -18,7 +18,7 @@ class CaptchaResponseResolver implements CaptchaResponseResolverInterface /** * @inheritdoc */ - public function resolve(Http $request): string + public function resolve(RequestInterface $request): string { $reCaptchaParam = $request->getParam(self::PARAM_RECAPTCHA); if (empty($reCaptchaParam)) { diff --git a/ReCaptchaUi/Model/CaptchaResponseResolverInterface.php b/ReCaptchaUi/Model/CaptchaResponseResolverInterface.php index c4702eb0..c1a31e4d 100644 --- a/ReCaptchaUi/Model/CaptchaResponseResolverInterface.php +++ b/ReCaptchaUi/Model/CaptchaResponseResolverInterface.php @@ -7,7 +7,7 @@ namespace Magento\ReCaptchaUi\Model; -use Magento\Framework\App\Request\Http; +use Magento\Framework\App\PlainTextRequestInterface; use Magento\Framework\App\RequestInterface; use Magento\Framework\Exception\InputException; @@ -28,9 +28,9 @@ interface CaptchaResponseResolverInterface /** * Extract reCAPTCHA response parameter from Request object * - * @param Http|RequestInterface $request + * @param RequestInterface|PlainTextRequestInterface $request * @return string * @throws InputException */ - public function resolve(Http $request): string; + public function resolve(RequestInterface $request): string; } diff --git a/ReCaptchaUi/Model/CaptchaTypeResolver.php b/ReCaptchaUi/Model/CaptchaTypeResolver.php new file mode 100644 index 00000000..01185c46 --- /dev/null +++ b/ReCaptchaUi/Model/CaptchaTypeResolver.php @@ -0,0 +1,22 @@ +captchaConfig = $captchaConfig; - $this->isInvisibleCaptcha = $isInvisibleCaptcha; - } - - /** - * @inheritdoc - */ - public function get(): array - { - $settings = [ - 'render' => [ - 'sitekey' => $this->captchaConfig->getPublicKey(), - 'theme' => $this->captchaConfig->getTheme(), - 'size' => $this->captchaConfig->getSize(), - 'badge' => $this->captchaConfig->getInvisibleBadgePosition(), - ], - 'lang' => $this->captchaConfig->getLanguageCode(), - 'invisible' => $this->isInvisibleCaptcha->isInvisible($this->captchaConfig->getCaptchaType()), - ]; - return $settings; - } -} diff --git a/ReCaptchaUi/Model/IsCaptchaEnabled.php b/ReCaptchaUi/Model/IsCaptchaEnabled.php new file mode 100644 index 00000000..8eaf8b12 --- /dev/null +++ b/ReCaptchaUi/Model/IsCaptchaEnabled.php @@ -0,0 +1,71 @@ +captchaTypeResolver = $captchaTypeResolver; + $this->uiConfigResolver = $uiConfigResolver; + $this->validationConfigResolver = $validationConfigResolver; + } + + /** + * @inheritdoc + */ + public function isCaptchaEnabledFor(string $key): bool + { + return (null !== $this->captchaTypeResolver->getCaptchaTypeFor($key)) && $this->areKeysConfigured($key); + } + + /** + * Return true if reCAPTCHA keys (public and private) are configured + * + * @param string $key + * @return bool + * @throws InputException + */ + private function areKeysConfigured(string $key): bool + { + $uiConfig = $this->uiConfigResolver->get($key); + $validationConfig = $this->validationConfigResolver->get($key); + + // TODO: + return $validationConfig->getPrivateKey() + && !empty($uiConfig['rendering']['sitekey']); + } +} diff --git a/ReCaptchaUi/Model/IsCaptchaEnabledInterface.php b/ReCaptchaUi/Model/IsCaptchaEnabledInterface.php new file mode 100644 index 00000000..a20d5e93 --- /dev/null +++ b/ReCaptchaUi/Model/IsCaptchaEnabledInterface.php @@ -0,0 +1,27 @@ +captchaResponseResolver = $captchaResponseResolver; + $this->validationConfigResolver = $validationConfigResolver; $this->captchaValidator = $captchaValidator; - $this->remoteAddress = $remoteAddress; $this->messageManager = $messageManager; $this->actionFlag = $actionFlag; - $this->captchaConfig = $captchaConfig; - $this->validationConfigFactory = $validationConfigFactory; } /** * @inheritdoc */ public function execute( - HttpRequest $request, - HttpResponse $response, + string $key, + RequestInterface $request, + HttpResponseInterface $response, string $redirectOnFailureUrl ): void { $reCaptchaResponse = $this->captchaResponseResolver->resolve($request); - - /** @var ValidationConfigInterface $validationConfig */ - $validationConfig = $this->validationConfigFactory->create( - [ - 'privateKey' => $this->captchaConfig->getPrivateKey(), - 'captchaType' => $this->captchaConfig->getCaptchaType(), - 'remoteIp' => $this->remoteAddress->getRemoteAddress(), - 'scoreThreshold' => $this->captchaConfig->getScoreThreshold(), - 'extensionAttributes' => null, - ] - ); + $validationConfig = $this->validationConfigResolver->get($key); if (false === $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig)) { - $this->messageManager->addErrorMessage($this->captchaConfig->getErrorMessage()); + $this->messageManager->addErrorMessage($validationConfig->getValidationFailureMessage()); $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); $response->setRedirect($redirectOnFailureUrl); diff --git a/ReCaptchaUi/Model/RequestHandlerInterface.php b/ReCaptchaUi/Model/RequestHandlerInterface.php index c1cf76c8..3efc05f4 100644 --- a/ReCaptchaUi/Model/RequestHandlerInterface.php +++ b/ReCaptchaUi/Model/RequestHandlerInterface.php @@ -7,11 +7,10 @@ namespace Magento\ReCaptchaUi\Model; -use Magento\Framework\App\Request\Http as HttpRequest; +use Magento\Framework\App\PlainTextRequestInterface; use Magento\Framework\App\RequestInterface; -use Magento\Framework\App\Response\HttpInterface as HttpResponse; -use Magento\Framework\App\ResponseInterface; -use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface; +use Magento\Framework\Exception\InputException; /** * Request handler interface (sugar service for avoiding boilerplate code) @@ -25,15 +24,17 @@ interface RequestHandlerInterface /** * Validate reCAPTCHA data in request, set message and redirect if validation was failed * - * @param HttpRequest|RequestInterface $request - * @param HttpResponse|ResponseInterface $response + * @param string $key Functionality identifier (like customer login, contact) + * @param RequestInterface|PlainTextRequestInterface $request + * @param HttpResponseInterface $response * @param string $redirectOnFailureUrl * @return void - * @throws LocalizedException + * @throws InputException */ public function execute( - HttpRequest $request, - HttpResponse $response, + string $key, + RequestInterface $request, + HttpResponseInterface $response, string $redirectOnFailureUrl ): void; } diff --git a/ReCaptchaUi/Model/CaptchaUiSettingsProviderInterface.php b/ReCaptchaUi/Model/UiConfigProviderInterface.php similarity index 56% rename from ReCaptchaUi/Model/CaptchaUiSettingsProviderInterface.php rename to ReCaptchaUi/Model/UiConfigProviderInterface.php index 4c8889ba..2a3e5440 100644 --- a/ReCaptchaUi/Model/CaptchaUiSettingsProviderInterface.php +++ b/ReCaptchaUi/Model/UiConfigProviderInterface.php @@ -8,14 +8,15 @@ namespace Magento\ReCaptchaUi\Model; /** - * Extension point of the UI setting for reCAPTCHA rendering + * Extension point for adding UI config for concrete reCAPTCHA type * * @api + * @see \Magento\ReCaptchaUi\Model\UiConfigResolver */ -interface CaptchaUiSettingsProviderInterface +interface UiConfigProviderInterface { /** - * Return layout UI setting for reCAPTCHA rendering + * Return UI config for concrete reCAPTCHA type * * @return array */ diff --git a/ReCaptchaUi/Model/UiConfigResolver.php b/ReCaptchaUi/Model/UiConfigResolver.php new file mode 100644 index 00000000..5ca000ad --- /dev/null +++ b/ReCaptchaUi/Model/UiConfigResolver.php @@ -0,0 +1,65 @@ +captchaTypeResolver = $captchaTypeResolver; + + foreach ($uiConfigProviders as $uiConfigProvider) { + if (!$uiConfigProvider instanceof UiConfigProviderInterface) { + throw new InputException( + __('UI config provider must implement %1', [ UiConfigResolverInterface::class]) + ); + } + } + $this->uiConfigProviders = $uiConfigProviders; + } + + /** + * @inheritdoc + */ + public function get(string $key): array + { + $captchaType = $this->captchaTypeResolver->getCaptchaTypeFor($key); + + if (!isset($this->uiConfigProviders[$captchaType])) { + throw new InputException( + __('UI config provider for "%type" is not configured.', ['type' => $captchaType]) + ); + } + return $this->uiConfigProviders[$captchaType]->get(); + } +} diff --git a/ReCaptchaUi/Model/UiConfigResolverInterface.php b/ReCaptchaUi/Model/UiConfigResolverInterface.php new file mode 100644 index 00000000..4ad3c6b5 --- /dev/null +++ b/ReCaptchaUi/Model/UiConfigResolverInterface.php @@ -0,0 +1,27 @@ +captchaTypeResolver = $captchaTypeResolver; + + foreach ($validationConfigProviders as $validationConfigProvider) { + if (!$validationConfigProvider instanceof ValidationConfigProviderInterface) { + throw new InputException( + __('Validation config provider must implement %1.', [ConfigProviderInterface::class]) + ); + } + } + $this->validationConfigProviders = $validationConfigProviders; + } + + /** + * @inheritdoc + */ + public function get(string $key): ValidationConfigInterface + { + $captchaType = $this->captchaTypeResolver->getCaptchaTypeFor($key); + + if (!isset($this->validationConfigProviders[$captchaType])) { + throw new InputException( + __('Validation config provider for "%type" is not configured.', ['type' => $captchaType]) + ); + } + return $this->validationConfigProviders[$captchaType]->get(); + } +} diff --git a/ReCaptchaUi/Model/ValidationConfigResolverInterface.php b/ReCaptchaUi/Model/ValidationConfigResolverInterface.php new file mode 100644 index 00000000..6b758d75 --- /dev/null +++ b/ReCaptchaUi/Model/ValidationConfigResolverInterface.php @@ -0,0 +1,30 @@ + - + sortOrder="10"/> diff --git a/ReCaptchaUi/etc/di.xml b/ReCaptchaUi/etc/di.xml index a19ca090..260929bf 100644 --- a/ReCaptchaUi/etc/di.xml +++ b/ReCaptchaUi/etc/di.xml @@ -9,5 +9,8 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> - + + + + diff --git a/ReCaptchaUser/Command/DisableReCaptchaForUserForgotPasswordCommand.php b/ReCaptchaUser/Command/DisableReCaptchaForUserForgotPasswordCommand.php index dd7b57f2..5ccf1519 100644 --- a/ReCaptchaUser/Command/DisableReCaptchaForUserForgotPasswordCommand.php +++ b/ReCaptchaUser/Command/DisableReCaptchaForUserForgotPasswordCommand.php @@ -35,7 +35,7 @@ public function __construct( protected function configure() { $this->setName('security:recaptcha:disable-for-user-forgot-password'); - $this->setDescription('Disable backend reCaptcha for user forgot password form'); + $this->setDescription('Disable reCAPTCHA for admin user forgot password form'); parent::configure(); } diff --git a/ReCaptchaUser/Command/DisableReCaptchaForUserLoginCommand.php b/ReCaptchaUser/Command/DisableReCaptchaForUserLoginCommand.php index b5ed60f1..461067e6 100644 --- a/ReCaptchaUser/Command/DisableReCaptchaForUserLoginCommand.php +++ b/ReCaptchaUser/Command/DisableReCaptchaForUserLoginCommand.php @@ -35,7 +35,7 @@ public function __construct( protected function configure() { $this->setName('security:recaptcha:disable-for-user-login'); - $this->setDescription('Disable backend reCaptcha for user login form'); + $this->setDescription('Disable reCAPTCHA for admin user login form'); parent::configure(); } diff --git a/ReCaptchaUser/Model/DisableReCaptchaForUserForgotPassword.php b/ReCaptchaUser/Model/DisableReCaptchaForUserForgotPassword.php index f181d0fa..40d234b1 100644 --- a/ReCaptchaUser/Model/DisableReCaptchaForUserForgotPassword.php +++ b/ReCaptchaUser/Model/DisableReCaptchaForUserForgotPassword.php @@ -11,11 +11,11 @@ use Magento\Framework\App\Config\ConfigResource\ConfigInterface; /** - * Disable ReCaptcha for use forgot password (causes config cache flush) + * Disable reCAPTCHA for use forgot password (causes config cache flush) */ class DisableReCaptchaForUserForgotPassword { - private const XML_PATH_ENABLED = 'recaptcha/backend/enabled_for_user_forgot_password'; + private const XML_PATH_ENABLED = 'recaptcha_backend/type_for/user_forgot_password'; /** * @var ConfigInterface @@ -40,7 +40,7 @@ public function __construct( } /** - * Disable ReCaptcha for use forgot password (causes config cache flush) + * Disable reCAPTCHA for use forgot password (causes config cache flush) */ public function execute() { diff --git a/ReCaptchaUser/Model/DisableReCaptchaForUserLogin.php b/ReCaptchaUser/Model/DisableReCaptchaForUserLogin.php index 6d5336b9..5c088ea1 100644 --- a/ReCaptchaUser/Model/DisableReCaptchaForUserLogin.php +++ b/ReCaptchaUser/Model/DisableReCaptchaForUserLogin.php @@ -11,11 +11,11 @@ use Magento\Framework\App\Config\ConfigResource\ConfigInterface; /** - * Disable ReCaptcha for user login (causes config cache flush) + * Disable reCAPTCHA for user login (causes config cache flush) */ class DisableReCaptchaForUserLogin { - private const XML_PATH_ENABLED = 'recaptcha/backend/enabled_for_user_forgot_password'; + private const XML_PATH_ENABLED = 'recaptcha_backend/type_for/user_forgot_password'; /** * @var ConfigInterface @@ -40,7 +40,7 @@ public function __construct( } /** - * Disable ReCaptcha for user login (causes config cache flush) + * Disable reCAPTCHA for user login (causes config cache flush) */ public function execute() { diff --git a/ReCaptchaUser/Observer/ForgotPasswordObserver.php b/ReCaptchaUser/Observer/ForgotPasswordObserver.php index 62fb4235..17dccecb 100644 --- a/ReCaptchaUser/Observer/ForgotPasswordObserver.php +++ b/ReCaptchaUser/Observer/ForgotPasswordObserver.php @@ -12,7 +12,7 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\UrlInterface; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\RequestHandlerInterface; /** @@ -31,23 +31,23 @@ class ForgotPasswordObserver implements ObserverInterface private $requestHandler; /** - * @var CaptchaConfigInterface + * @var IsCaptchaEnabledInterface */ - private $captchaConfig; + private $isCaptchaEnabled; /** * @param UrlInterface $url * @param RequestHandlerInterface $requestHandler - * @param CaptchaConfigInterface $captchaConfig + * @param IsCaptchaEnabledInterface $isCaptchaEnabled */ public function __construct( UrlInterface $url, RequestHandlerInterface $requestHandler, - CaptchaConfigInterface $captchaConfig + IsCaptchaEnabledInterface $isCaptchaEnabled ) { $this->url = $url; $this->requestHandler = $requestHandler; - $this->captchaConfig = $captchaConfig; + $this->isCaptchaEnabled = $isCaptchaEnabled; } /** @@ -61,11 +61,12 @@ public function execute(Observer $observer): void $controller = $observer->getControllerAction(); $request = $controller->getRequest(); - if ($this->captchaConfig->isCaptchaEnabledFor('user_forgot_password') && null !== $request->getParam('email')) { + $key = 'user_forgot_password'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key) && null !== $request->getParam('email')) { $response = $controller->getResponse(); $redirectOnFailureUrl = $this->url->getUrl('*/*/forgotpassword', ['_secure' => true]); - $this->requestHandler->execute($request, $response, $redirectOnFailureUrl); + $this->requestHandler->execute($key, $request, $response, $redirectOnFailureUrl); } } } diff --git a/ReCaptchaUser/Observer/LoginObserver.php b/ReCaptchaUser/Observer/LoginObserver.php index 8a2d14ec..f581ec77 100644 --- a/ReCaptchaUser/Observer/LoginObserver.php +++ b/ReCaptchaUser/Observer/LoginObserver.php @@ -12,12 +12,10 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\Plugin\AuthenticationException; -use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress; -use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; -use Magento\ReCaptchaApi\Api\CaptchaValidatorInterface; -use Magento\ReCaptchaApi\Api\Data\ValidationConfigInterface; -use Magento\ReCaptchaApi\Api\Data\ValidationConfigInterfaceFactory; +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; +use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface; +use Magento\ReCaptchaValidationApi\Api\ValidatorInterface; /** * LoginObserver @@ -30,24 +28,19 @@ class LoginObserver implements ObserverInterface private $captchaResponseResolver; /** - * @var CaptchaValidatorInterface + * @var ValidationConfigResolverInterface */ - private $captchaValidator; + private $validationConfigResolver; /** - * @var RemoteAddress + * @var ValidatorInterface */ - private $remoteAddress; - - /** - * @var CaptchaConfigInterface - */ - private $captchaConfig; + private $captchaValidator; /** - * @var ValidationConfigInterfaceFactory + * @var IsCaptchaEnabledInterface */ - private $validationConfigFactory; + private $isCaptchaEnabled; /** * @var RequestInterface @@ -61,27 +54,24 @@ class LoginObserver implements ObserverInterface /** * @param CaptchaResponseResolverInterface $captchaResponseResolver - * @param CaptchaValidatorInterface $captchaValidator - * @param RemoteAddress $remoteAddress - * @param CaptchaConfigInterface $captchaConfig - * @param ValidationConfigInterfaceFactory $validationConfigFactory + * @param ValidationConfigResolverInterface $validationConfigResolver + * @param ValidatorInterface $captchaValidator + * @param IsCaptchaEnabledInterface $isCaptchaEnabled * @param RequestInterface $request * @param string $loginActionName */ public function __construct( CaptchaResponseResolverInterface $captchaResponseResolver, - CaptchaValidatorInterface $captchaValidator, - RemoteAddress $remoteAddress, - CaptchaConfigInterface $captchaConfig, - ValidationConfigInterfaceFactory $validationConfigFactory, + ValidationConfigResolverInterface $validationConfigResolver, + ValidatorInterface $captchaValidator, + IsCaptchaEnabledInterface $isCaptchaEnabled, RequestInterface $request, string $loginActionName ) { $this->captchaResponseResolver = $captchaResponseResolver; + $this->validationConfigResolver = $validationConfigResolver; $this->captchaValidator = $captchaValidator; - $this->remoteAddress = $remoteAddress; - $this->captchaConfig = $captchaConfig; - $this->validationConfigFactory = $validationConfigFactory; + $this->isCaptchaEnabled = $isCaptchaEnabled; $this->request = $request; $this->loginActionName = $loginActionName; } @@ -94,23 +84,15 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('user_login') + $key = 'user_login'; + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key) && $this->request->getFullActionName() === $this->loginActionName ) { $reCaptchaResponse = $this->captchaResponseResolver->resolve($this->request); - /** @var ValidationConfigInterface $validationConfig */ - $validationConfig = $this->validationConfigFactory->create( - [ - 'privateKey' => $this->captchaConfig->getPrivateKey(), - 'captchaType' => $this->captchaConfig->getCaptchaType(), - 'remoteIp' => $this->remoteAddress->getRemoteAddress(), - 'scoreThreshold' => $this->captchaConfig->getScoreThreshold(), - 'extensionAttributes' => null, - ] - ); + $validationConfig = $this->validationConfigResolver->get($key); if (false === $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig)) { - throw new AuthenticationException($this->captchaConfig->getErrorMessage()); + throw new AuthenticationException(__($validationConfig->getValidationFailureMessage())); } } } diff --git a/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php b/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php index e89464a7..7807fade 100644 --- a/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php +++ b/ReCaptchaUser/Test/Integration/ForgotPasswordFormTest.php @@ -10,9 +10,8 @@ use Magento\Backend\Model\UrlInterface; use Magento\Framework\Data\Form\FormKey; use Magento\Framework\Message\MessageInterface; -use Magento\ReCaptcha\Model\CaptchaValidator; -use Magento\ReCaptchaApi\Api\CaptchaValidatorInterface; use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; +use Magento\ReCaptchaValidation\Model\Validator; use Magento\TestFramework\Mail\Template\TransportBuilderMock; use PHPUnit\Framework\MockObject\MockObject; use Magento\TestFramework\TestCase\AbstractController; @@ -40,7 +39,7 @@ class ForgotPasswordFormTest extends AbstractController private $transportMock; /** - * @var CaptchaValidatorInterface|MockObject + * @var Validator|MockObject */ private $captchaValidatorMock; @@ -54,16 +53,14 @@ protected function setUp() $this->backendUrl = $this->_objectManager->get(UrlInterface::class); $this->transportMock = $this->_objectManager->get(TransportBuilderMock::class); - $this->captchaValidatorMock = $this->createMock(CaptchaValidatorInterface::class); - $this->_objectManager->addSharedInstance($this->captchaValidatorMock, CaptchaValidator::class); + $this->captchaValidatorMock = $this->createMock(Validator::class); + $this->_objectManager->addSharedInstance($this->captchaValidatorMock, Validator::class); } /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 0 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key */ public function testGetRequestIfReCaptchaIsDisabled() { @@ -72,8 +69,8 @@ public function testGetRequestIfReCaptchaIsDisabled() /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 1 + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible */ public function testGetRequestIfReCaptchaKeysAreNotConfigured() { @@ -82,10 +79,10 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured() /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible */ public function testGetRequestIfReCaptchaIsEnabled() { @@ -94,10 +91,8 @@ public function testGetRequestIfReCaptchaIsEnabled() /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 0 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key */ public function testPostRequestIfReCaptchaIsDisabled() { @@ -106,9 +101,9 @@ public function testPostRequestIfReCaptchaIsDisabled() /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/type invisible * @magentoAdminConfigFixture admin/captcha/always_for/backend_forgotpassword 0 - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 1 + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible */ public function testPostRequestIfReCaptchaKeysAreNotConfigured() { @@ -117,10 +112,10 @@ public function testPostRequestIfReCaptchaKeysAreNotConfigured() /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible */ public function testPostRequestWithSuccessfulReCaptchaValidation() { @@ -135,10 +130,10 @@ public function testPostRequestWithSuccessfulReCaptchaValidation() /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible * @expectedException \Magento\Framework\Exception\InputException * @expectedExceptionMessage Can not resolve reCAPTCHA parameter. */ @@ -157,10 +152,10 @@ public function testPostRequestIfReCaptchaParameterIsMissed() /** * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_forgot_password 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_forgot_password invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_forgot_password invisible */ public function testPostRequestWithFailedReCaptchaValidation() { @@ -176,7 +171,7 @@ public function testPostRequestWithFailedReCaptchaValidation() $this->dispatch('backend/admin/auth/forgotpassword'); $this->assertSessionMessages( - self::equalTo(['Incorrect reCAPTCHA validation.']), + self::equalTo(['reCAPTCHA verification failed']), MessageInterface::TYPE_ERROR ); self::assertEmpty($this->transportMock->getSentMessage()); diff --git a/ReCaptchaUser/Test/Integration/LoginFormTest.php b/ReCaptchaUser/Test/Integration/LoginFormTest.php index 09c8adbb..8652e649 100644 --- a/ReCaptchaUser/Test/Integration/LoginFormTest.php +++ b/ReCaptchaUser/Test/Integration/LoginFormTest.php @@ -11,9 +11,8 @@ use Magento\Backend\Model\UrlInterface; use Magento\Framework\Data\Form\FormKey; use Magento\Framework\Message\MessageInterface; -use Magento\ReCaptcha\Model\CaptchaValidator; -use Magento\ReCaptchaApi\Api\CaptchaValidatorInterface; use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; +use Magento\ReCaptchaValidation\Model\Validator; use Magento\TestFramework\Bootstrap; use Magento\TestFramework\TestCase\AbstractController; use PHPUnit\Framework\MockObject\MockObject; @@ -43,7 +42,7 @@ class LoginFormTest extends AbstractController private $backendUrl; /** - * @var CaptchaValidatorInterface|MockObject + * @var Validator|MockObject */ private $captchaValidatorMock; @@ -57,17 +56,15 @@ protected function setUp() $this->formKey = $this->_objectManager->get(FormKey::class); $this->backendUrl = $this->_objectManager->get(UrlInterface::class); - $this->captchaValidatorMock = $this->createMock(CaptchaValidatorInterface::class); - $this->_objectManager->addSharedInstance($this->captchaValidatorMock, CaptchaValidator::class); + $this->captchaValidatorMock = $this->createMock(Validator::class); + $this->_objectManager->addSharedInstance($this->captchaValidatorMock, Validator::class); } /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 0 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key */ public function testGetRequestIfReCaptchaIsDisabled() { @@ -77,8 +74,8 @@ public function testGetRequestIfReCaptchaIsDisabled() /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 1 + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testGetRequestIfReCaptchaKeysAreNotConfigured() { @@ -88,10 +85,10 @@ public function testGetRequestIfReCaptchaKeysAreNotConfigured() /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testGetRequestIfReCaptchaIsEnabled() { @@ -101,10 +98,8 @@ public function testGetRequestIfReCaptchaIsEnabled() /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 0 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key */ public function testPostRequestIfReCaptchaIsDisabled() { @@ -114,8 +109,8 @@ public function testPostRequestIfReCaptchaIsDisabled() /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 1 + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestIfReCaptchaKeysAreNotConfigured() { @@ -125,10 +120,10 @@ public function testPostRequestIfReCaptchaKeysAreNotConfigured() /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestWithSuccessfulReCaptchaValidation() { @@ -144,10 +139,10 @@ public function testPostRequestWithSuccessfulReCaptchaValidation() /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestIfReCaptchaParameterIsMissed() { @@ -174,10 +169,10 @@ public function testPostRequestIfReCaptchaParameterIsMissed() /** * @magentoAdminConfigFixture admin/security/use_form_key 0 * @magentoAdminConfigFixture admin/captcha/enable 0 - * @magentoAdminConfigFixture recaptcha/backend/public_key test_public_key - * @magentoAdminConfigFixture recaptcha/backend/private_key test_private_key - * @magentoAdminConfigFixture recaptcha/backend/type invisible - * @magentoAdminConfigFixture recaptcha/backend/enabled_for_user_login 1 + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/public_key test_public_key + * @magentoAdminConfigFixture recaptcha_backend/type_invisible/private_key test_private_key + * @magentoAdminConfigFixture recaptcha_backend/type_for/user_login invisible + * @magentoConfigFixture default_store recaptcha_backend/type_for/user_login invisible */ public function testPostRequestWithFailedReCaptchaValidation() { @@ -198,7 +193,7 @@ public function testPostRequestWithFailedReCaptchaValidation() // Location header is different than in the successful case $this->assertRedirect(self::equalTo($this->backendUrl->getUrl('admin'))); $this->assertSessionMessages( - self::equalTo(['Incorrect reCAPTCHA validation.']), + self::equalTo(['reCAPTCHA verification failed']), MessageInterface::TYPE_ERROR ); self::assertFalse($this->auth->isLoggedIn()); @@ -207,7 +202,7 @@ public function testPostRequestWithFailedReCaptchaValidation() /** * @param bool $shouldContainReCaptcha */ - private function checkSuccessfulGetResponse($shouldContainReCaptcha = false) + private function checkSuccessfulGetResponse($shouldContainReCaptcha = false): void { $this->getRequest()->setUri($this->backendUrl->getUrl('admin')); @@ -226,7 +221,7 @@ private function checkSuccessfulGetResponse($shouldContainReCaptcha = false) /** * @param array $postValues */ - private function checkSuccessfulPostResponse(array $postValues = []) + private function checkSuccessfulPostResponse(array $postValues = []): void { $this->getRequest()->setPostValue(array_replace_recursive( [ diff --git a/ReCaptchaUser/composer.json b/ReCaptchaUser/composer.json index f9db4757..774347bb 100644 --- a/ReCaptchaUser/composer.json +++ b/ReCaptchaUser/composer.json @@ -1,19 +1,13 @@ { "name": "magento/module-re-captcha-user", - "version": "1.0.0", - "description": "Google reCaptcha integration for Magento2", + "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-api": "*", - "magento/module-re-captcha-ui": "*" + "magento/module-re-captcha-ui": "*", + "magento/module-re-captcha-validation-api": "*" }, - "authors": [ - { - "name": "Riccardo Tempesta", - "email": "riccardo.tempesta@magespecialist.it" - } - ], "type": "magento2-module", "license": "OSL-3.0", "autoload": { diff --git a/ReCaptchaUser/etc/adminhtml/system.xml b/ReCaptchaUser/etc/adminhtml/system.xml index 0c27fddd..3aeb0e01 100644 --- a/ReCaptchaUser/etc/adminhtml/system.xml +++ b/ReCaptchaUser/etc/adminhtml/system.xml @@ -8,18 +8,18 @@ - - - + + Enable for Login - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type - Enable for Forgot Password - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type diff --git a/ReCaptchaUser/etc/config.xml b/ReCaptchaUser/etc/config.xml index eafddef2..0bd2fe9f 100644 --- a/ReCaptchaUser/etc/config.xml +++ b/ReCaptchaUser/etc/config.xml @@ -8,11 +8,11 @@ - - - 1 - 1 - - + + + + + + diff --git a/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_forgotpassword.xml b/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_forgotpassword.xml index 252be953..aa14ebae 100644 --- a/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_forgotpassword.xml +++ b/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_forgotpassword.xml @@ -8,16 +8,16 @@ - + + ifconfig="recaptcha_backend/type_for/user_forgot_password"> - user_forgot_password + user_forgot_password diff --git a/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_login.xml b/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_login.xml index 0fe6091d..4bc51e0e 100644 --- a/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_login.xml +++ b/ReCaptchaUser/view/adminhtml/layout/adminhtml_auth_login.xml @@ -8,17 +8,17 @@ - - + + + ifconfig="recaptcha_backend/type_for/user_login"> - user_login + user_login diff --git a/ReCaptchaUser/view/adminhtml/templates/recaptcha.phtml b/ReCaptchaUser/view/adminhtml/templates/recaptcha.phtml index e539449a..e9c76038 100644 --- a/ReCaptchaUser/view/adminhtml/templates/recaptcha.phtml +++ b/ReCaptchaUser/view/adminhtml/templates/recaptcha.phtml @@ -4,14 +4,14 @@ * See COPYING.txt for license details. */ /** @var $block Magento\ReCaptchaUi\Block\ReCaptcha */ -$settings = $block->getCaptchaSettings(); -$renderSettings = $settings['render'] ?? []; -$isInvisible = !empty($settings['invisible']); -$languageCode = $settings['lang'] ?? null; +$config = $block->getCaptchaUiConfig(); +$renderingOptions = $config['rendering'] ?? []; +$isInvisible = !empty($config['invisible']); +$languageCode = $config['rendering']['lang'] ?? null; ?> + class="admin-recaptcha-content= /* @noEscape */ !empty($renderingOptions['size']) ? ' size-' . $renderingOptions['size'] : '' ?>">