From 46c25788a587c9d563952f81e742f2889f2785a0 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Thu, 20 Feb 2020 11:44:55 +0200 Subject: [PATCH 01/18] security-package/issues/123: reCAPTCHA adapters modularity. --- ReCaptchaAdminUi/Model/CaptchaConfig.php | 33 +++++++++++--- ReCaptchaAdminUi/Model/Config/Source/Type.php | 24 ++++++++--- .../Config/Source/Type/OptionInterface.php | 30 +++++++++++++ ReCaptchaFrontendUi/Model/CaptchaConfig.php | 29 ++++++++++--- ReCaptchaFrontendUi/etc/config.xml | 2 +- .../Model/Config/Source/Type/Option.php | 36 ++++++++++++++++ ReCaptchaInvisibleVersion2/README.md | 1 + ReCaptchaInvisibleVersion2/composer.json | 26 +++++++++++ ReCaptchaInvisibleVersion2/etc/di.xml | 31 +++++++++++++ ReCaptchaInvisibleVersion2/etc/module.xml | 11 +++++ ReCaptchaInvisibleVersion2/registration.php | 12 ++++++ .../Model/Config/Source/Type/Option.php | 36 ++++++++++++++++ ReCaptchaInvisibleVersion3/README.md | 1 + ReCaptchaInvisibleVersion3/composer.json | 26 +++++++++++ ReCaptchaInvisibleVersion3/etc/di.xml | 43 +++++++++++++++++++ ReCaptchaInvisibleVersion3/etc/module.xml | 11 +++++ ReCaptchaInvisibleVersion3/registration.php | 12 ++++++ .../Model/Config/Source/Type/Option.php | 36 ++++++++++++++++ ReCaptchaVersion2/README.md | 1 + ReCaptchaVersion2/composer.json | 26 +++++++++++ ReCaptchaVersion2/etc/di.xml | 17 ++++++++ ReCaptchaVersion2/etc/module.xml | 11 +++++ ReCaptchaVersion2/registration.php | 12 ++++++ 23 files changed, 449 insertions(+), 18 deletions(-) create mode 100644 ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php create mode 100644 ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php create mode 100644 ReCaptchaInvisibleVersion2/README.md create mode 100644 ReCaptchaInvisibleVersion2/composer.json create mode 100644 ReCaptchaInvisibleVersion2/etc/di.xml create mode 100644 ReCaptchaInvisibleVersion2/etc/module.xml create mode 100644 ReCaptchaInvisibleVersion2/registration.php create mode 100644 ReCaptchaInvisibleVersion3/Model/Config/Source/Type/Option.php create mode 100644 ReCaptchaInvisibleVersion3/README.md create mode 100644 ReCaptchaInvisibleVersion3/composer.json create mode 100644 ReCaptchaInvisibleVersion3/etc/di.xml create mode 100644 ReCaptchaInvisibleVersion3/etc/module.xml create mode 100644 ReCaptchaInvisibleVersion3/registration.php create mode 100644 ReCaptchaVersion2/Model/Config/Source/Type/Option.php create mode 100644 ReCaptchaVersion2/README.md create mode 100644 ReCaptchaVersion2/composer.json create mode 100644 ReCaptchaVersion2/etc/di.xml create mode 100644 ReCaptchaVersion2/etc/module.xml create mode 100644 ReCaptchaVersion2/registration.php diff --git a/ReCaptchaAdminUi/Model/CaptchaConfig.php b/ReCaptchaAdminUi/Model/CaptchaConfig.php index 15a6621f..2bfb7f37 100644 --- a/ReCaptchaAdminUi/Model/CaptchaConfig.php +++ b/ReCaptchaAdminUi/Model/CaptchaConfig.php @@ -22,19 +22,36 @@ class CaptchaConfig implements CaptchaConfigInterface private const XML_PATH_SCORE_THRESHOLD = 'recaptcha/backend/score_threshold'; private const XML_PATH_SIZE = 'recaptcha/backend/size'; - private const XML_PATH_THEME= 'recaptcha/backend/theme'; + private const XML_PATH_THEME = 'recaptcha/backend/theme'; /** * @var ScopeConfigInterface */ private $scopeConfig; + /** + * @var array + */ + private $invisibleTypes; + + /** + * @var array + */ + private $captchaErrorMessages; + /** * @param ScopeConfigInterface $scopeConfig + * @param array $invisibleTypes + * @param array $captchaErrorMessages */ - public function __construct(ScopeConfigInterface $scopeConfig) - { + public function __construct( + ScopeConfigInterface $scopeConfig, + array $invisibleTypes = [], + array $captchaErrorMessages = [] + ) { $this->scopeConfig = $scopeConfig; + $this->invisibleTypes = $invisibleTypes; + $this->captchaErrorMessages = $captchaErrorMessages; } /** @@ -98,7 +115,7 @@ public function getTheme(): ?string */ public function getScoreThreshold(): float { - return min(1.0, max(0.1, (float) $this->scopeConfig->getValue( + return min(1.0, max(0.1, (float)$this->scopeConfig->getValue( self::XML_PATH_SCORE_THRESHOLD ))); } @@ -108,8 +125,10 @@ public function getScoreThreshold(): float */ public function getErrorMessage(): Phrase { - if ($this->getCaptchaType() === 'recaptcha_v3') { - return __('You cannot proceed with such operation, your reCaptcha reputation is too low.'); + foreach ($this->captchaErrorMessages as $captchaErrorMessage) { + if ($this->getCaptchaType() === $captchaErrorMessage['type']) { + return __($captchaErrorMessage['message']); + } } return __('Incorrect ReCaptcha validation'); @@ -120,7 +139,7 @@ public function getErrorMessage(): Phrase */ public function isInvisibleRecaptcha(): bool { - return in_array($this->getCaptchaType(), ['invisible', 'recaptcha_v3'], true); + return in_array($this->getCaptchaType(), $this->invisibleTypes, true); } /** diff --git a/ReCaptchaAdminUi/Model/Config/Source/Type.php b/ReCaptchaAdminUi/Model/Config/Source/Type.php index 87779278..ca761f7b 100644 --- a/ReCaptchaAdminUi/Model/Config/Source/Type.php +++ b/ReCaptchaAdminUi/Model/Config/Source/Type.php @@ -8,21 +8,35 @@ namespace Magento\ReCaptchaAdminUi\Model\Config\Source; use Magento\Framework\Data\OptionSourceInterface; +use Magento\ReCaptchaApi\Model\Config\Source\Type\OptionInterface; /** * Recaptcha type options */ class Type implements OptionSourceInterface { + /** + * @var OptionInterface[] + */ + private $options; + + /** + * @param OptionInterface[] $options + */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** * @inheritDoc */ public function toOptionArray() { - return [ - ['value' => 'recaptcha_v3', 'label' => __('Invisible reCaptcha v3')], - ['value' => 'invisible', 'label' => __('Invisible reCaptcha v2')], - ['value' => 'recaptcha', 'label' => __('reCaptcha v2')], - ]; + return array_map( + function (OptionInterface $option) { + return ['value' => $option->getValue(), 'label' => __($option->getLabel())]; + }, + $this->options); } } diff --git a/ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php b/ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php new file mode 100644 index 00000000..9b98c73d --- /dev/null +++ b/ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php @@ -0,0 +1,30 @@ +scopeConfig = $scopeConfig; + $this->invisibleTypes = $invisibleTypes; + $this->captchaErrorMessages = $captchaErrorMessages; } /** @@ -61,7 +78,7 @@ public function getPrivateKey(): string */ public function isInvisibleRecaptcha(): bool { - return in_array($this->getCaptchaType(), ['invisible', 'recaptcha_v3'], true); + return in_array($this->getCaptchaType(), $this->invisibleTypes, true); } /** @@ -153,8 +170,10 @@ public function getScoreThreshold(): float */ public function getErrorMessage(): Phrase { - if ($this->getCaptchaType() === 'recaptcha_v3') { - return __('You cannot proceed with such operation, your reCaptcha reputation is too low.'); + foreach ($this->captchaErrorMessages as $captchaErrorMessage) { + if ($this->getCaptchaType() === $captchaErrorMessage['type']) { + return __($captchaErrorMessage['message']); + } } return __('Incorrect ReCaptcha validation'); diff --git a/ReCaptchaFrontendUi/etc/config.xml b/ReCaptchaFrontendUi/etc/config.xml index 5bb4238a..f5c8ea7e 100644 --- a/ReCaptchaFrontendUi/etc/config.xml +++ b/ReCaptchaFrontendUi/etc/config.xml @@ -10,7 +10,7 @@ - 0 + recaptcha_v3 normal inline light diff --git a/ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php b/ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php new file mode 100644 index 00000000..ff4af4a8 --- /dev/null +++ b/ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php @@ -0,0 +1,36 @@ + + + + + + + Magento\ReCaptchaInvisibleVersion2\Model\Config\Source\Type\Option + + + + + + + invisible + + + + + + + invisible + + + + diff --git a/ReCaptchaInvisibleVersion2/etc/module.xml b/ReCaptchaInvisibleVersion2/etc/module.xml new file mode 100644 index 00000000..26873a73 --- /dev/null +++ b/ReCaptchaInvisibleVersion2/etc/module.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/ReCaptchaInvisibleVersion2/registration.php b/ReCaptchaInvisibleVersion2/registration.php new file mode 100644 index 00000000..e553e442 --- /dev/null +++ b/ReCaptchaInvisibleVersion2/registration.php @@ -0,0 +1,12 @@ + + + + + + + Magento\ReCaptchaInvisibleVersion3\Model\Config\Source\Type\Option + + + + + + + recaptcha_v3 + + + + recaptcha_v3 + You cannot proceed with such operation, your reCaptcha reputation is too low. + + + + + + + + recaptcha_v3 + + + + recaptcha_v3 + You cannot proceed with such operation, your reCaptcha reputation is too low. + + + + + diff --git a/ReCaptchaInvisibleVersion3/etc/module.xml b/ReCaptchaInvisibleVersion3/etc/module.xml new file mode 100644 index 00000000..52958c86 --- /dev/null +++ b/ReCaptchaInvisibleVersion3/etc/module.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/ReCaptchaInvisibleVersion3/registration.php b/ReCaptchaInvisibleVersion3/registration.php new file mode 100644 index 00000000..526abd78 --- /dev/null +++ b/ReCaptchaInvisibleVersion3/registration.php @@ -0,0 +1,12 @@ + + + + + + + Magento\ReCaptchaVersion2\Model\Config\Source\Type\Option + + + + diff --git a/ReCaptchaVersion2/etc/module.xml b/ReCaptchaVersion2/etc/module.xml new file mode 100644 index 00000000..bd4b3b95 --- /dev/null +++ b/ReCaptchaVersion2/etc/module.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/ReCaptchaVersion2/registration.php b/ReCaptchaVersion2/registration.php new file mode 100644 index 00000000..f8dcbd99 --- /dev/null +++ b/ReCaptchaVersion2/registration.php @@ -0,0 +1,12 @@ + Date: Thu, 20 Feb 2020 16:02:55 +0200 Subject: [PATCH 02/18] security-package/issues/123: reCAPTCHA adapters modularity - CaptchaValidator fix. --- ReCaptcha/Model/CaptchaValidator.php | 35 ++++++++++++++++++++++----- ReCaptchaInvisibleVersion3/etc/di.xml | 10 ++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/ReCaptcha/Model/CaptchaValidator.php b/ReCaptcha/Model/CaptchaValidator.php index 2285f4f0..801999d5 100644 --- a/ReCaptcha/Model/CaptchaValidator.php +++ b/ReCaptcha/Model/CaptchaValidator.php @@ -17,6 +17,28 @@ */ class CaptchaValidator implements CaptchaValidatorInterface { + /** + * @var string[] + */ + private $thresholdApplicable; + + /** + * @var string[] + */ + private $scoreRequired; + + /** + * @param string[] $thresholdApplicable + * @param string[] $scoreRequired + */ + public function __construct( + array $thresholdApplicable = [], + array $scoreRequired = [] + ) { + $this->thresholdApplicable = $thresholdApplicable; + $this->scoreRequired = $scoreRequired; + } + /** * @inheritdoc */ @@ -29,17 +51,18 @@ public function validate( if ($reCaptchaResponse) { // @codingStandardsIgnoreStart $reCaptcha = new ReCaptcha($secret); - // @codingStandardsIgnoreEmd + // @codingStandardsIgnoreEnd - if ($validationConfig->getCaptchaType() === 'recaptcha_v3') { - if (isset($options['threshold'])) { - $reCaptcha->setScoreThreshold($validationConfig->getScoreThreshold()); + if (in_array($validationConfig->getCaptchaType(), $this->thresholdApplicable)) { + $scoreThreshold = $validationConfig->getScoreThreshold(); + if (isset($scoreThreshold)) { + $reCaptcha->setScoreThreshold($scoreThreshold); } } $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 (in_array($validationConfig->getCaptchaType(), $this->scoreRequired) && ($res->getScore() === null)) { + throw new LocalizedException(__('Internal error: Make sure you are using correct api keys')); } if ($res->isSuccess()) { diff --git a/ReCaptchaInvisibleVersion3/etc/di.xml b/ReCaptchaInvisibleVersion3/etc/di.xml index 2d444476..85a379da 100644 --- a/ReCaptchaInvisibleVersion3/etc/di.xml +++ b/ReCaptchaInvisibleVersion3/etc/di.xml @@ -40,4 +40,14 @@ + + + + recaptcha_v3 + + + recaptcha_v3 + + + From 289fcd386365894304b60cb8025c1d405613a77b Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Thu, 20 Feb 2020 16:26:57 +0200 Subject: [PATCH 03/18] security-package/issues/123: reCAPTCHA adapters modularity - config.xml split. --- ReCaptchaInvisibleVersion2/etc/config.xml | 20 +++++++++++++++++++ ReCaptchaInvisibleVersion2/etc/module.xml | 6 +++++- .../etc/config.xml | 6 ++++-- ReCaptchaInvisibleVersion3/etc/module.xml | 7 ++++++- .../etc/config.xml | 9 +++++---- 5 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 ReCaptchaInvisibleVersion2/etc/config.xml rename {ReCaptchaAdminUi => ReCaptchaInvisibleVersion3}/etc/config.xml (77%) rename {ReCaptchaFrontendUi => ReCaptchaVersion2}/etc/config.xml (81%) diff --git a/ReCaptchaInvisibleVersion2/etc/config.xml b/ReCaptchaInvisibleVersion2/etc/config.xml new file mode 100644 index 00000000..d95f7715 --- /dev/null +++ b/ReCaptchaInvisibleVersion2/etc/config.xml @@ -0,0 +1,20 @@ + + + + + + + invisible + + + invisible + + + + diff --git a/ReCaptchaInvisibleVersion2/etc/module.xml b/ReCaptchaInvisibleVersion2/etc/module.xml index 26873a73..9f5b3770 100644 --- a/ReCaptchaInvisibleVersion2/etc/module.xml +++ b/ReCaptchaInvisibleVersion2/etc/module.xml @@ -7,5 +7,9 @@ --> - + + + + + diff --git a/ReCaptchaAdminUi/etc/config.xml b/ReCaptchaInvisibleVersion3/etc/config.xml similarity index 77% rename from ReCaptchaAdminUi/etc/config.xml rename to ReCaptchaInvisibleVersion3/etc/config.xml index fdc59146..2886ad5c 100644 --- a/ReCaptchaAdminUi/etc/config.xml +++ b/ReCaptchaInvisibleVersion3/etc/config.xml @@ -11,10 +11,12 @@ recaptcha_v3 - normal - light 0.9 + + recaptcha_v3 + 0.6 + diff --git a/ReCaptchaInvisibleVersion3/etc/module.xml b/ReCaptchaInvisibleVersion3/etc/module.xml index 52958c86..4ddafc9f 100644 --- a/ReCaptchaInvisibleVersion3/etc/module.xml +++ b/ReCaptchaInvisibleVersion3/etc/module.xml @@ -7,5 +7,10 @@ --> - + + + + + + diff --git a/ReCaptchaFrontendUi/etc/config.xml b/ReCaptchaVersion2/etc/config.xml similarity index 81% rename from ReCaptchaFrontendUi/etc/config.xml rename to ReCaptchaVersion2/etc/config.xml index f5c8ea7e..88556663 100644 --- a/ReCaptchaFrontendUi/etc/config.xml +++ b/ReCaptchaVersion2/etc/config.xml @@ -9,13 +9,14 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> + + recaptcha + - recaptcha_v3 + recaptcha normal - inline light - - 0.6 + inline From 18e1acdedf6dbb4933645018039d3c986e5942ce Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Thu, 20 Feb 2020 17:05:50 +0200 Subject: [PATCH 04/18] security-package/issues/123: reCAPTCHA adapters modularity - system.xml split. --- ReCaptchaAdminUi/etc/adminhtml/system.xml | 54 ------------------- .../etc/adminhtml/system.xml | 29 ++++++++++ ReCaptchaVersion2/etc/adminhtml/system.xml | 50 +++++++++++++++++ 3 files changed, 79 insertions(+), 54 deletions(-) create mode 100644 ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml create mode 100644 ReCaptchaVersion2/etc/adminhtml/system.xml diff --git a/ReCaptchaAdminUi/etc/adminhtml/system.xml b/ReCaptchaAdminUi/etc/adminhtml/system.xml index cccdfe03..fabe18c4 100644 --- a/ReCaptchaAdminUi/etc/adminhtml/system.xml +++ b/ReCaptchaAdminUi/etc/adminhtml/system.xml @@ -38,33 +38,6 @@ showInStore="0" canRestore="0"> - - - - Magento\ReCaptchaAdminUi\Model\Config\Source\Theme - - recaptcha - - - - - - Magento\ReCaptchaAdminUi\Model\Config\Source\Size - - recaptcha - - - - - - - recaptcha_v3 - - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. - Google API secret key - - - Magento\ReCaptchaAdminUi\Model\Config\Source\Theme - - recaptcha - - - - - - Magento\ReCaptchaAdminUi\Model\Config\Source\Size - - recaptcha - - - - - - - recaptcha_v3 - - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. - - diff --git a/ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml b/ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml new file mode 100644 index 00000000..d66808b1 --- /dev/null +++ b/ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml @@ -0,0 +1,29 @@ + + +
+ + + + + recaptcha_v3 + + From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. + + + + + + + recaptcha_v3 + + From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. + + +
+
+
diff --git a/ReCaptchaVersion2/etc/adminhtml/system.xml b/ReCaptchaVersion2/etc/adminhtml/system.xml new file mode 100644 index 00000000..0d788212 --- /dev/null +++ b/ReCaptchaVersion2/etc/adminhtml/system.xml @@ -0,0 +1,50 @@ + + + + +
+ + + + Magento\ReCaptchaAdminUi\Model\Config\Source\Theme + + recaptcha + + + + + Magento\ReCaptchaAdminUi\Model\Config\Source\Size + + recaptcha + + + + + + + Magento\ReCaptchaAdminUi\Model\Config\Source\Theme + + recaptcha + + + + + Magento\ReCaptchaAdminUi\Model\Config\Source\Size + + recaptcha + + + +
+
+
From 1642caf67b6e4cb1c6db42b64061d4f472dd5a8f Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 21 Feb 2020 15:19:01 +0200 Subject: [PATCH 05/18] security-package/issues/123: reCAPTCHA adapters modularity - CaptchaValidator split. --- ReCaptcha/Model/CaptchaValidator.php | 49 +++++-------------- .../Model/CaptchaTypeValidatorInterface.php | 32 ++++++++++++ .../Model/CaptchaValidator.php | 44 +++++++++++++++++ ReCaptchaInvisibleVersion2/etc/di.xml | 7 +++ .../Model/CaptchaValidator.php | 49 +++++++++++++++++++ ReCaptchaInvisibleVersion3/etc/di.xml | 17 +++---- ReCaptchaVersion2/Model/CaptchaValidator.php | 44 +++++++++++++++++ ReCaptchaVersion2/etc/di.xml | 7 +++ 8 files changed, 203 insertions(+), 46 deletions(-) create mode 100644 ReCaptchaApi/Model/CaptchaTypeValidatorInterface.php create mode 100644 ReCaptchaInvisibleVersion2/Model/CaptchaValidator.php create mode 100644 ReCaptchaInvisibleVersion3/Model/CaptchaValidator.php create mode 100644 ReCaptchaVersion2/Model/CaptchaValidator.php diff --git a/ReCaptcha/Model/CaptchaValidator.php b/ReCaptcha/Model/CaptchaValidator.php index 801999d5..7a008494 100644 --- a/ReCaptcha/Model/CaptchaValidator.php +++ b/ReCaptcha/Model/CaptchaValidator.php @@ -10,7 +10,6 @@ use Magento\Framework\Exception\LocalizedException; use Magento\ReCaptchaApi\Api\CaptchaValidatorInterface; use Magento\ReCaptchaApi\Api\Data\ValidationConfigInterface; -use ReCaptcha\ReCaptcha; /** * @inheritDoc @@ -18,25 +17,17 @@ class CaptchaValidator implements CaptchaValidatorInterface { /** - * @var string[] + * @var CaptchaTypeValidatorInterface[] */ - private $thresholdApplicable; + private $validators; /** - * @var string[] - */ - private $scoreRequired; - - /** - * @param string[] $thresholdApplicable - * @param string[] $scoreRequired + * @param CaptchaTypeValidatorInterface[] $validators */ public function __construct( - array $thresholdApplicable = [], - array $scoreRequired = [] + array $validators = [] ) { - $this->thresholdApplicable = $thresholdApplicable; - $this->scoreRequired = $scoreRequired; + $this->validators = $validators; } /** @@ -46,30 +37,16 @@ public function validate( string $reCaptchaResponse, ValidationConfigInterface $validationConfig ): bool { - $secret = $validationConfig->getPrivateKey(); - - if ($reCaptchaResponse) { - // @codingStandardsIgnoreStart - $reCaptcha = new ReCaptcha($secret); - // @codingStandardsIgnoreEnd - - if (in_array($validationConfig->getCaptchaType(), $this->thresholdApplicable)) { - $scoreThreshold = $validationConfig->getScoreThreshold(); - if (isset($scoreThreshold)) { - $reCaptcha->setScoreThreshold($scoreThreshold); - } - } - $res = $reCaptcha->verify($reCaptchaResponse, $validationConfig->getRemoteIp()); - - if (in_array($validationConfig->getCaptchaType(), $this->scoreRequired) && ($res->getScore() === null)) { - throw new LocalizedException(__('Internal error: Make sure you are using correct api keys')); - } - - if ($res->isSuccess()) { - return true; + /** + * @var string $validatorCode + * @var CaptchaTypeValidatorInterface $validatorInstance + */ + foreach ($this->validators as $validatorCode => $validatorInstance) { + if ($validatorCode === $validationConfig->getCaptchaType()) { + return $validatorInstance->validate($reCaptchaResponse, $validationConfig); } } - return false; + throw new LocalizedException(__('No reCAPTCHA validator found.')); } } diff --git a/ReCaptchaApi/Model/CaptchaTypeValidatorInterface.php b/ReCaptchaApi/Model/CaptchaTypeValidatorInterface.php new file mode 100644 index 00000000..37a9b434 --- /dev/null +++ b/ReCaptchaApi/Model/CaptchaTypeValidatorInterface.php @@ -0,0 +1,32 @@ +getPrivateKey(); + + if ($reCaptchaResponse) { + // @codingStandardsIgnoreStart + $reCaptcha = new ReCaptcha($secret); + // @codingStandardsIgnoreEnd + + $res = $reCaptcha->verify($reCaptchaResponse, $validationConfig->getRemoteIp()); + + if ($res->isSuccess()) { + return true; + } + } + + return false; + } +} diff --git a/ReCaptchaInvisibleVersion2/etc/di.xml b/ReCaptchaInvisibleVersion2/etc/di.xml index e849717f..097a8961 100644 --- a/ReCaptchaInvisibleVersion2/etc/di.xml +++ b/ReCaptchaInvisibleVersion2/etc/di.xml @@ -7,6 +7,13 @@ --> + + + + Magento\ReCaptchaInvisibleVersion2\Model\CaptchaValidator + + + diff --git a/ReCaptchaInvisibleVersion3/Model/CaptchaValidator.php b/ReCaptchaInvisibleVersion3/Model/CaptchaValidator.php new file mode 100644 index 00000000..bd489542 --- /dev/null +++ b/ReCaptchaInvisibleVersion3/Model/CaptchaValidator.php @@ -0,0 +1,49 @@ +getPrivateKey(); + + if ($reCaptchaResponse) { + // @codingStandardsIgnoreStart + $reCaptcha = new ReCaptcha($secret); + // @codingStandardsIgnoreEnd + + $reCaptcha->setScoreThreshold($validationConfig->getScoreThreshold()); + $res = $reCaptcha->verify($reCaptchaResponse, $validationConfig->getRemoteIp()); + + if ($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/ReCaptchaInvisibleVersion3/etc/di.xml b/ReCaptchaInvisibleVersion3/etc/di.xml index 85a379da..2d15a721 100644 --- a/ReCaptchaInvisibleVersion3/etc/di.xml +++ b/ReCaptchaInvisibleVersion3/etc/di.xml @@ -7,6 +7,13 @@ --> + + + + Magento\ReCaptchaInvisibleVersion3\Model\CaptchaValidator + + + @@ -40,14 +47,4 @@ - - - - recaptcha_v3 - - - recaptcha_v3 - - - diff --git a/ReCaptchaVersion2/Model/CaptchaValidator.php b/ReCaptchaVersion2/Model/CaptchaValidator.php new file mode 100644 index 00000000..f34ea80d --- /dev/null +++ b/ReCaptchaVersion2/Model/CaptchaValidator.php @@ -0,0 +1,44 @@ +getPrivateKey(); + + if ($reCaptchaResponse) { + // @codingStandardsIgnoreStart + $reCaptcha = new ReCaptcha($secret); + // @codingStandardsIgnoreEnd + + $res = $reCaptcha->verify($reCaptchaResponse, $validationConfig->getRemoteIp()); + + if ($res->isSuccess()) { + return true; + } + } + + return false; + } +} diff --git a/ReCaptchaVersion2/etc/di.xml b/ReCaptchaVersion2/etc/di.xml index 357d51ee..728f7cbd 100644 --- a/ReCaptchaVersion2/etc/di.xml +++ b/ReCaptchaVersion2/etc/di.xml @@ -7,6 +7,13 @@ --> + + + + Magento\ReCaptchaVersion2\Model\CaptchaValidator + + + From 83fcc321e0d87d5110e0caf44eff12e88676a21d Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 25 Feb 2020 09:26:30 -0600 Subject: [PATCH 06/18] security-package/issues/123: reCAPTCHA adapters modularity --- ReCaptchaAdminUi/etc/adminhtml/system.xml | 40 +++++++-- ReCaptchaContact/etc/adminhtml/system.xml | 6 +- ReCaptchaContact/etc/config.xml | 10 +-- ReCaptchaCustomer/etc/adminhtml/system.xml | 10 +-- ReCaptchaCustomer/etc/config.xml | 14 +-- .../Model/OptionSource}/Position.php | 4 +- ReCaptchaInvisibleVersion2/composer.json | 7 +- .../etc/adminhtml/system.xml | 49 ++++++++++ ReCaptchaInvisibleVersion2/etc/config.xml | 18 ++-- ReCaptchaInvisibleVersion2/etc/module.xml | 6 +- .../Model/OptionSource/Position.php | 33 +++++++ ReCaptchaInvisibleVersion3/composer.json | 7 +- .../etc/adminhtml/system.xml | 79 ++++++++++++---- ReCaptchaInvisibleVersion3/etc/config.xml | 20 ++--- ReCaptchaInvisibleVersion3/etc/module.xml | 7 +- ReCaptchaNewsletter/etc/adminhtml/system.xml | 11 +-- ReCaptchaNewsletter/etc/config.xml | 10 +-- ReCaptchaReview/etc/adminhtml/system.xml | 6 +- ReCaptchaReview/etc/config.xml | 10 +-- ReCaptchaSendFriend/etc/adminhtml/system.xml | 6 +- ReCaptchaSendFriend/etc/config.xml | 10 +-- ReCaptchaUser/etc/adminhtml/system.xml | 8 +- ReCaptchaUser/etc/config.xml | 12 +-- .../Model/OptionSource}/Size.php | 4 +- .../Model/OptionSource}/Theme.php | 4 +- ReCaptchaVersion2/composer.json | 7 +- ReCaptchaVersion2/etc/adminhtml/system.xml | 89 ++++++++++++------- ReCaptchaVersion2/etc/config.xml | 21 +++-- 28 files changed, 340 insertions(+), 168 deletions(-) rename {ReCaptchaApi/Model/Config/Source => ReCaptchaInvisibleVersion2/Model/OptionSource}/Position.php (82%) create mode 100644 ReCaptchaInvisibleVersion2/etc/adminhtml/system.xml create mode 100644 ReCaptchaInvisibleVersion3/Model/OptionSource/Position.php rename {ReCaptchaApi/Model/Config/Source => ReCaptchaVersion2/Model/OptionSource}/Size.php (81%) rename {ReCaptchaApi/Model/Config/Source => ReCaptchaVersion2/Model/OptionSource}/Theme.php (81%) diff --git a/ReCaptchaAdminUi/etc/adminhtml/system.xml b/ReCaptchaAdminUi/etc/adminhtml/system.xml index fe50d29b..9000dadb 100644 --- a/ReCaptchaAdminUi/etc/adminhtml/system.xml +++ b/ReCaptchaAdminUi/etc/adminhtml/system.xml @@ -12,6 +12,32 @@ +
+ separator-top + + security + Magento_ReCaptcha::config + + + + +
+ +
+ separator-top + + security + Magento_ReCaptcha::config + + + + +
+
separator-top @@ -51,7 +77,7 @@ - Magento\ReCaptchaApi\Model\Config\Source\Theme + Magento\ReCaptchaVersion2\Model\OptionSource\Theme Applicable only to reCAPTCHA v2 ("I am not a robot"). recaptcha @@ -61,7 +87,7 @@ - Magento\ReCaptchaApi\Model\Config\Source\Size + Magento\ReCaptchaVersion2\Model\OptionSource\Size Applicable only to reCAPTCHA v2 ("I am not a robot"). recaptcha @@ -75,7 +101,7 @@
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. + See supported Language Codes.
]]> @@ -86,7 +112,7 @@ - Magento\ReCaptchaApi\Model\Config\Source\Position + Magento\ReCaptchaInvisibleVersion3\Model\OptionSource\Position Applicable only to Invisible reCAPTCHA v2 or reCAPTCHA v3. recaptcha_v3,invisible @@ -126,7 +152,7 @@ - Magento\ReCaptchaApi\Model\Config\Source\Theme + Magento\ReCaptchaVersion2\Model\OptionSource\Theme Applicable only to reCAPTCHA v2 ("I am not a robot"). recaptcha @@ -136,7 +162,7 @@ - Magento\ReCaptchaApi\Model\Config\Source\Size + Magento\ReCaptchaVersion2\Model\OptionSource\Size Applicable only to reCAPTCHA v2 ("I am not a robot"). recaptcha @@ -161,7 +187,7 @@ - Magento\ReCaptchaApi\Model\Config\Source\Position + Magento\ReCaptchaInvisibleVersion3\Model\OptionSource\Position Applicable only to Invisible reCAPTCHA v2 or reCAPTCHA v3. recaptcha_v3,invisible diff --git a/ReCaptchaContact/etc/adminhtml/system.xml b/ReCaptchaContact/etc/adminhtml/system.xml index b4ad3b16..57472b63 100644 --- a/ReCaptchaContact/etc/adminhtml/system.xml +++ b/ReCaptchaContact/etc/adminhtml/system.xml @@ -8,9 +8,9 @@ -
- - + + Magento\Config\Model\Config\Source\Yesno diff --git a/ReCaptchaContact/etc/config.xml b/ReCaptchaContact/etc/config.xml index 05bef1de..7dd58443 100644 --- a/ReCaptchaContact/etc/config.xml +++ b/ReCaptchaContact/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + 0 + + diff --git a/ReCaptchaCustomer/etc/adminhtml/system.xml b/ReCaptchaCustomer/etc/adminhtml/system.xml index a31252b1..23a6c3c7 100644 --- a/ReCaptchaCustomer/etc/adminhtml/system.xml +++ b/ReCaptchaCustomer/etc/adminhtml/system.xml @@ -8,19 +8,19 @@ -
- - + + Magento\Config\Model\Config\Source\Yesno - Magento\Config\Model\Config\Source\Yesno - Magento\Config\Model\Config\Source\Yesno diff --git a/ReCaptchaCustomer/etc/config.xml b/ReCaptchaCustomer/etc/config.xml index 2d9e5ef5..8b73ce4a 100644 --- a/ReCaptchaCustomer/etc/config.xml +++ b/ReCaptchaCustomer/etc/config.xml @@ -8,12 +8,12 @@ - - - 1 - 1 - 1 - - + + + 0 + 0 + 0 + + diff --git a/ReCaptchaApi/Model/Config/Source/Position.php b/ReCaptchaInvisibleVersion2/Model/OptionSource/Position.php similarity index 82% rename from ReCaptchaApi/Model/Config/Source/Position.php rename to ReCaptchaInvisibleVersion2/Model/OptionSource/Position.php index 429bf2c7..ca300795 100644 --- a/ReCaptchaApi/Model/Config/Source/Position.php +++ b/ReCaptchaInvisibleVersion2/Model/OptionSource/Position.php @@ -5,7 +5,7 @@ */ declare(strict_types=1); -namespace Magento\ReCaptchaApi\Model\Config\Source; +namespace Magento\ReCaptchaInvisibleVersion2\Model\OptionSource; use Magento\Framework\Data\OptionSourceInterface; @@ -13,7 +13,7 @@ * reCAPTCHA positions * * Extension point for adding reCAPTCHA positions - * Applicable only to Invisible reCAPTCHA types + * Applicable only to Invisible reCAPTCHA type (Invisible reCAPTCHA v2) * * @api */ diff --git a/ReCaptchaInvisibleVersion2/composer.json b/ReCaptchaInvisibleVersion2/composer.json index c9643f5f..43a954f9 100644 --- a/ReCaptchaInvisibleVersion2/composer.json +++ b/ReCaptchaInvisibleVersion2/composer.json @@ -1,11 +1,10 @@ { "name": "magento/module-re-captcha-invisible-version-2", - "version": "3.0.0", + "version": "1.0.0", "description": "Invisible Google reCaptcha V2 integration for Magento2", "require": { "php": "~7.1.3||~7.2.0||~7.3.0", - "magento/framework": "102.0.*", - "magento/magento-composer-installer": "*" + "magento/framework": "102.0.*" }, "authors": [ { @@ -20,7 +19,7 @@ "registration.php" ], "psr-4": { - "Magento\\ReCaptcha\\": "" + "Magento\\ReCaptchaInvisibleVersion2\\": "" } } } diff --git a/ReCaptchaInvisibleVersion2/etc/adminhtml/system.xml b/ReCaptchaInvisibleVersion2/etc/adminhtml/system.xml new file mode 100644 index 00000000..27a3ec97 --- /dev/null +++ b/ReCaptchaInvisibleVersion2/etc/adminhtml/system.xml @@ -0,0 +1,49 @@ + + +
+ + + + + + + + + + + + + + Magento\ReCaptchaInvisibleVersion2\Model\OptionSource\Position + + +
+
+ + + + + + + + + + + + + + Magento\ReCaptchaInvisibleVersion2\Model\OptionSource\Position + + +
+
+
diff --git a/ReCaptchaInvisibleVersion2/etc/config.xml b/ReCaptchaInvisibleVersion2/etc/config.xml index d95f7715..5cde6878 100644 --- a/ReCaptchaInvisibleVersion2/etc/config.xml +++ b/ReCaptchaInvisibleVersion2/etc/config.xml @@ -8,13 +8,15 @@ - - - invisible - - - invisible - - + + + inline + + + + + inline + + diff --git a/ReCaptchaInvisibleVersion2/etc/module.xml b/ReCaptchaInvisibleVersion2/etc/module.xml index 9f5b3770..26873a73 100644 --- a/ReCaptchaInvisibleVersion2/etc/module.xml +++ b/ReCaptchaInvisibleVersion2/etc/module.xml @@ -7,9 +7,5 @@ --> - - - - - + diff --git a/ReCaptchaInvisibleVersion3/Model/OptionSource/Position.php b/ReCaptchaInvisibleVersion3/Model/OptionSource/Position.php new file mode 100644 index 00000000..c40853ac --- /dev/null +++ b/ReCaptchaInvisibleVersion3/Model/OptionSource/Position.php @@ -0,0 +1,33 @@ + 'inline', 'label' => __('Inline')], + ['value' => 'bottomright', 'label' => __('Bottom Right')], + ['value' => 'bottomleft', 'label' => __('Bottom Left')], + ]; + } +} diff --git a/ReCaptchaInvisibleVersion3/composer.json b/ReCaptchaInvisibleVersion3/composer.json index cca4fa77..ee7696d7 100644 --- a/ReCaptchaInvisibleVersion3/composer.json +++ b/ReCaptchaInvisibleVersion3/composer.json @@ -1,11 +1,10 @@ { "name": "magento/module-re-captcha-invisible-version-3", - "version": "3.0.0", + "version": "1.0.0", "description": "Google reCaptcha V3 integration for Magento2", "require": { "php": "~7.1.3||~7.2.0||~7.3.0", - "magento/framework": "102.0.*", - "magento/magento-composer-installer": "*" + "magento/framework": "102.0.*" }, "authors": [ { @@ -20,7 +19,7 @@ "registration.php" ], "psr-4": { - "Magento\\ReCaptcha\\": "" + "Magento\\ReCaptchaInvisibleVersion3\\": "" } } } diff --git a/ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml b/ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml index d66808b1..5df8974c 100644 --- a/ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml +++ b/ReCaptchaInvisibleVersion3/etc/adminhtml/system.xml @@ -1,27 +1,72 @@ -
- - + + + + + + + + + + + + - - - recaptcha_v3 - - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. + + + For more details, see the official documentation + Interpreting the score. + ]]> + + + + + Magento\ReCaptchaInvisibleVersion3\Model\OptionSource\Position - - +
+ + + + + + + + + + + + + + + For more details, see the official documentation + Interpreting the score. + ]]> + + + - - - recaptcha_v3 - - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. + + Magento\ReCaptchaInvisibleVersion3\Model\OptionSource\Position + + For more details, see the official documentation + Interpreting the score. + ]]>
diff --git a/ReCaptchaInvisibleVersion3/etc/config.xml b/ReCaptchaInvisibleVersion3/etc/config.xml index 2886ad5c..ecf03016 100644 --- a/ReCaptchaInvisibleVersion3/etc/config.xml +++ b/ReCaptchaInvisibleVersion3/etc/config.xml @@ -8,15 +8,15 @@ - - - recaptcha_v3 - 0.9 - - - recaptcha_v3 - 0.6 - - + + + 0.5 + + + + + 0.5 + + diff --git a/ReCaptchaInvisibleVersion3/etc/module.xml b/ReCaptchaInvisibleVersion3/etc/module.xml index 4ddafc9f..52958c86 100644 --- a/ReCaptchaInvisibleVersion3/etc/module.xml +++ b/ReCaptchaInvisibleVersion3/etc/module.xml @@ -7,10 +7,5 @@ --> - - - - - - + diff --git a/ReCaptchaNewsletter/etc/adminhtml/system.xml b/ReCaptchaNewsletter/etc/adminhtml/system.xml index e9b6983c..8758f15e 100644 --- a/ReCaptchaNewsletter/etc/adminhtml/system.xml +++ b/ReCaptchaNewsletter/etc/adminhtml/system.xml @@ -8,16 +8,13 @@ -
- - + + - Requires an Invisible reCAPTCHA v2 or reCAPTCHA v3 key. If enabled, a badge will be displayed in every page. + If enabled, a badge will be displayed in every page. Magento\Config\Model\Config\Source\Yesno - - recaptcha_v3,invisible -
diff --git a/ReCaptchaNewsletter/etc/config.xml b/ReCaptchaNewsletter/etc/config.xml index 33dbab69..ae0ce375 100644 --- a/ReCaptchaNewsletter/etc/config.xml +++ b/ReCaptchaNewsletter/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + 0 + + diff --git a/ReCaptchaReview/etc/adminhtml/system.xml b/ReCaptchaReview/etc/adminhtml/system.xml index e3237e7b..10c45747 100644 --- a/ReCaptchaReview/etc/adminhtml/system.xml +++ b/ReCaptchaReview/etc/adminhtml/system.xml @@ -8,9 +8,9 @@ -
- - + + Magento\Config\Model\Config\Source\Yesno diff --git a/ReCaptchaReview/etc/config.xml b/ReCaptchaReview/etc/config.xml index ad78b62b..e6710139 100644 --- a/ReCaptchaReview/etc/config.xml +++ b/ReCaptchaReview/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + 0 + + diff --git a/ReCaptchaSendFriend/etc/adminhtml/system.xml b/ReCaptchaSendFriend/etc/adminhtml/system.xml index ecd03a0a..14e71fc8 100644 --- a/ReCaptchaSendFriend/etc/adminhtml/system.xml +++ b/ReCaptchaSendFriend/etc/adminhtml/system.xml @@ -8,9 +8,9 @@ -
- - + + Magento\Config\Model\Config\Source\Yesno diff --git a/ReCaptchaSendFriend/etc/config.xml b/ReCaptchaSendFriend/etc/config.xml index 1bdf3cc4..b0fe3f59 100644 --- a/ReCaptchaSendFriend/etc/config.xml +++ b/ReCaptchaSendFriend/etc/config.xml @@ -8,10 +8,10 @@ - - - 1 - - + + + 0 + + diff --git a/ReCaptchaUser/etc/adminhtml/system.xml b/ReCaptchaUser/etc/adminhtml/system.xml index 0c27fddd..fad8d141 100644 --- a/ReCaptchaUser/etc/adminhtml/system.xml +++ b/ReCaptchaUser/etc/adminhtml/system.xml @@ -8,15 +8,15 @@ -
- - + + Magento\Config\Model\Config\Source\Yesno - Magento\Config\Model\Config\Source\Yesno diff --git a/ReCaptchaUser/etc/config.xml b/ReCaptchaUser/etc/config.xml index eafddef2..8f541706 100644 --- a/ReCaptchaUser/etc/config.xml +++ b/ReCaptchaUser/etc/config.xml @@ -8,11 +8,11 @@ - - - 1 - 1 - - + + + 0 + 0 + + diff --git a/ReCaptchaApi/Model/Config/Source/Size.php b/ReCaptchaVersion2/Model/OptionSource/Size.php similarity index 81% rename from ReCaptchaApi/Model/Config/Source/Size.php rename to ReCaptchaVersion2/Model/OptionSource/Size.php index d8413686..3d36d55f 100644 --- a/ReCaptchaApi/Model/Config/Source/Size.php +++ b/ReCaptchaVersion2/Model/OptionSource/Size.php @@ -5,7 +5,7 @@ */ declare(strict_types=1); -namespace Magento\ReCaptchaApi\Model\Config\Source; +namespace Magento\ReCaptchaVersion2\Model\OptionSource; use Magento\Framework\Data\OptionSourceInterface; @@ -13,7 +13,7 @@ * reCAPTCHA sizes * * Extension point for adding reCAPTCHA sizes - * Applicable only for visible captcha type (for example "reCAPTCHA v2") + * Applicable only for visible captcha type ("reCAPTCHA v2") * * @api */ diff --git a/ReCaptchaApi/Model/Config/Source/Theme.php b/ReCaptchaVersion2/Model/OptionSource/Theme.php similarity index 81% rename from ReCaptchaApi/Model/Config/Source/Theme.php rename to ReCaptchaVersion2/Model/OptionSource/Theme.php index 4dfb054c..a217169e 100644 --- a/ReCaptchaApi/Model/Config/Source/Theme.php +++ b/ReCaptchaVersion2/Model/OptionSource/Theme.php @@ -5,7 +5,7 @@ */ declare(strict_types=1); -namespace Magento\ReCaptchaApi\Model\Config\Source; +namespace Magento\ReCaptchaVersion2\Model\OptionSource; use Magento\Framework\Data\OptionSourceInterface; @@ -13,7 +13,7 @@ * reCAPTCHA themes * * Extension point for adding reCAPTCHA themes - * Applicable only for visible captcha type (for example "reCAPTCHA v2") + * Applicable only for visible captcha type ("reCAPTCHA v2") * * @api */ diff --git a/ReCaptchaVersion2/composer.json b/ReCaptchaVersion2/composer.json index c9c81ea2..b6704fa4 100644 --- a/ReCaptchaVersion2/composer.json +++ b/ReCaptchaVersion2/composer.json @@ -1,11 +1,10 @@ { "name": "magento/module-re-captcha-version-2", - "version": "3.0.0", + "version": "1.0.0", "description": "Google reCaptcha V2 integration for Magento2", "require": { "php": "~7.1.3||~7.2.0||~7.3.0", - "magento/framework": "102.0.*", - "magento/magento-composer-installer": "*" + "magento/framework": "102.0.*" }, "authors": [ { @@ -20,7 +19,7 @@ "registration.php" ], "psr-4": { - "Magento\\ReCaptcha\\": "" + "Magento\\ReCaptchaVersion2\\": "" } } } diff --git a/ReCaptchaVersion2/etc/adminhtml/system.xml b/ReCaptchaVersion2/etc/adminhtml/system.xml index 0d788212..5e62d67b 100644 --- a/ReCaptchaVersion2/etc/adminhtml/system.xml +++ b/ReCaptchaVersion2/etc/adminhtml/system.xml @@ -1,48 +1,77 @@ - - -
- - + + + + + + + + + + + + - Magento\ReCaptchaAdminUi\Model\Config\Source\Theme - - recaptcha - + Magento\ReCaptchaVersion2\Model\OptionSource\Theme - - Magento\ReCaptchaAdminUi\Model\Config\Source\Size - - recaptcha - + Magento\ReCaptchaVersion2\Model\OptionSource\Size + + + + + supported Language Codes. + ]]> - - +
+ + + + + + + + + + + + - Magento\ReCaptchaAdminUi\Model\Config\Source\Theme - - recaptcha - + Magento\ReCaptchaVersion2\Model\OptionSource\Theme - - Magento\ReCaptchaAdminUi\Model\Config\Source\Size - - recaptcha - + Magento\ReCaptchaVersion2\Model\OptionSource\Size + + + + + supported Language Codes. + ]]>
diff --git a/ReCaptchaVersion2/etc/config.xml b/ReCaptchaVersion2/etc/config.xml index 88556663..3e3902f4 100644 --- a/ReCaptchaVersion2/etc/config.xml +++ b/ReCaptchaVersion2/etc/config.xml @@ -8,16 +8,19 @@ - - - recaptcha - - - recaptcha + + normal light - inline - - + + + + + + normal + light + + + From 60ccc05b53a46228c8d92f871ea9ed7d7118b8ec Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 25 Feb 2020 15:17:28 -0600 Subject: [PATCH 07/18] security-package/issues/123: reCAPTCHA adapters modularity --- ReCaptcha/composer.json | 2 +- ReCaptcha/etc/acl.xml | 2 +- ReCaptchaAdminUi/Model/CaptchaConfig.php | 175 ------------------ ReCaptchaAdminUi/composer.json | 2 +- ReCaptchaAdminUi/etc/adminhtml/system.xml | 162 +--------------- ReCaptchaApi/Api/CaptchaConfigInterface.php | 51 +---- ReCaptchaApi/Model/Config/Source/Type.php | 42 ----- .../Config/Source/Type/OptionInterface.php | 30 --- ReCaptchaApi/Model/IsInvisibleCaptcha.php | 40 ---- ReCaptchaApi/Model/OptionSource/Type.php | 41 ++++ ReCaptchaApi/composer.json | 2 +- ReCaptchaApi/etc/acl.xml | 2 +- ReCaptchaApi/etc/di.xml | 12 +- .../LayoutProcessor/Checkout/Onepage.php | 26 ++- ReCaptchaCheckout/composer.json | 2 +- ReCaptchaContact/composer.json | 2 +- ReCaptchaContact/etc/adminhtml/system.xml | 6 +- ReCaptchaContact/etc/config.xml | 6 +- .../frontend/layout/contact_index_index.xml | 4 +- .../Observer/AjaxLoginObserver.php | 5 +- .../InjectRecaptchaInAuthenticationPopup.php | 19 +- ReCaptchaCustomer/composer.json | 2 +- ReCaptchaCustomer/etc/adminhtml/system.xml | 14 +- ReCaptchaCustomer/etc/config.xml | 10 +- .../layout/customer_account_create.xml | 4 +- .../customer_account_forgotpassword.xml | 4 +- .../layout/customer_account_login.xml | 4 +- ReCaptchaFrontendUi/Model/CaptchaConfig.php | 98 ++-------- ReCaptchaFrontendUi/composer.json | 2 +- .../view/frontend/web/js/reCaptcha.js | 18 +- .../Model/Config/Source/Type/Option.php | 36 ---- .../Model/OptionSource/Position.php | 33 ---- ReCaptchaInvisibleVersion2/etc/di.xml | 38 ---- .../Model/Config/Source/Type/Option.php | 36 ---- .../Model/OptionSource/Position.php | 33 ---- ReCaptchaInvisibleVersion3/etc/di.xml | 50 ----- .../Plugin/CheckReCaptchaVisibilityPlugin.php | 49 ----- ReCaptchaNewsletter/composer.json | 2 +- ReCaptchaNewsletter/etc/adminhtml/system.xml | 6 +- ReCaptchaNewsletter/etc/config.xml | 6 +- ReCaptchaNewsletter/etc/frontend/di.xml | 13 -- .../view/frontend/layout/default.xml | 8 +- ReCaptchaReview/composer.json | 2 +- ReCaptchaReview/etc/adminhtml/system.xml | 6 +- ReCaptchaReview/etc/config.xml | 6 +- .../frontend/layout/catalog_product_view.xml | 4 +- ReCaptchaSendFriend/composer.json | 2 +- ReCaptchaSendFriend/etc/adminhtml/system.xml | 6 +- ReCaptchaSendFriend/etc/config.xml | 6 +- .../layout/sendfriend_product_send.xml | 4 +- ReCaptchaUi/Block/ReCaptcha.php | 42 +++-- .../Model/CaptchaUiSettingsProvider.php | 57 ------ ...face.php => UiConfigProviderInterface.php} | 7 +- ReCaptchaUi/Model/UiConfigResolver.php | 71 +++++++ .../Model/UiConfigResolverInterface.php | 27 +++ ReCaptchaUi/composer.json | 2 +- ReCaptchaUi/etc/di.xml | 2 +- ...eReCaptchaForUserForgotPasswordCommand.php | 2 +- .../DisableReCaptchaForUserLoginCommand.php | 2 +- .../DisableReCaptchaForUserForgotPassword.php | 6 +- .../Model/DisableReCaptchaForUserLogin.php | 6 +- ReCaptchaUser/Observer/LoginObserver.php | 5 +- ReCaptchaUser/composer.json | 2 +- ReCaptchaUser/etc/adminhtml/system.xml | 10 +- ReCaptchaUser/etc/config.xml | 8 +- .../layout/adminhtml_auth_forgotpassword.xml | 6 +- .../adminhtml/layout/adminhtml_auth_login.xml | 8 +- .../view/adminhtml/templates/recaptcha.phtml | 12 +- .../Model/Config/Source/Type/Option.php | 36 ---- ReCaptchaVersion2/Model/OptionSource/Size.php | 32 ---- .../Model/OptionSource/Theme.php | 32 ---- ReCaptchaVersion2/etc/di.xml | 24 --- .../Model/CaptchaValidator.php | 2 +- .../Model/Frontend/UiConfigProvider.php | 103 +++++++++++ .../Model/OptionSource/Size.php | 41 ++++ .../Model/OptionSource/Theme.php | 41 ++++ .../README.md | 0 .../composer.json | 6 +- .../etc/adminhtml/system.xml | 8 +- .../etc/config.xml | 0 ReCaptchaVersion2Checkbox/etc/di.xml | 48 +++++ ReCaptchaVersion2Checkbox/etc/frontend/di.xml | 17 ++ .../etc/module.xml | 2 +- .../registration.php | 2 +- .../Model/CaptchaValidator.php | 2 +- .../Model/Frontend/UiConfigProvider.php | 73 ++++++++ .../Model/OptionSource/Position.php | 41 ++++ .../README.md | 0 .../composer.json | 6 +- .../etc/adminhtml/system.xml | 8 +- .../etc/config.xml | 0 ReCaptchaVersion2Invisible/etc/di.xml | 53 ++++++ .../etc/frontend/di.xml | 17 ++ .../etc/module.xml | 2 +- .../registration.php | 2 +- .../Model/CaptchaValidator.php | 2 +- .../Model/Frontend/UiConfigProvider.php | 73 ++++++++ .../Model/OptionSource/Position.php | 41 ++++ .../README.md | 0 .../composer.json | 6 +- .../etc/adminhtml/system.xml | 8 +- .../etc/config.xml | 2 + ReCaptchaVersion3Invisible/etc/di.xml | 53 ++++++ .../etc/frontend/di.xml | 17 ++ .../etc/module.xml | 2 +- .../registration.php | 2 +- 106 files changed, 987 insertions(+), 1227 deletions(-) delete mode 100644 ReCaptchaAdminUi/Model/CaptchaConfig.php delete mode 100644 ReCaptchaApi/Model/Config/Source/Type.php delete mode 100644 ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php delete mode 100644 ReCaptchaApi/Model/IsInvisibleCaptcha.php create mode 100644 ReCaptchaApi/Model/OptionSource/Type.php delete mode 100644 ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php delete mode 100644 ReCaptchaInvisibleVersion2/Model/OptionSource/Position.php delete mode 100644 ReCaptchaInvisibleVersion2/etc/di.xml delete mode 100644 ReCaptchaInvisibleVersion3/Model/Config/Source/Type/Option.php delete mode 100644 ReCaptchaInvisibleVersion3/Model/OptionSource/Position.php delete mode 100644 ReCaptchaInvisibleVersion3/etc/di.xml delete mode 100644 ReCaptchaNewsletter/Plugin/CheckReCaptchaVisibilityPlugin.php delete mode 100644 ReCaptchaNewsletter/etc/frontend/di.xml delete mode 100644 ReCaptchaUi/Model/CaptchaUiSettingsProvider.php rename ReCaptchaUi/Model/{CaptchaUiSettingsProviderInterface.php => UiConfigProviderInterface.php} (56%) create mode 100644 ReCaptchaUi/Model/UiConfigResolver.php create mode 100644 ReCaptchaUi/Model/UiConfigResolverInterface.php delete mode 100644 ReCaptchaVersion2/Model/Config/Source/Type/Option.php delete mode 100644 ReCaptchaVersion2/Model/OptionSource/Size.php delete mode 100644 ReCaptchaVersion2/Model/OptionSource/Theme.php delete mode 100644 ReCaptchaVersion2/etc/di.xml rename {ReCaptchaVersion2 => ReCaptchaVersion2Checkbox}/Model/CaptchaValidator.php (95%) create mode 100644 ReCaptchaVersion2Checkbox/Model/Frontend/UiConfigProvider.php create mode 100644 ReCaptchaVersion2Checkbox/Model/OptionSource/Size.php create mode 100644 ReCaptchaVersion2Checkbox/Model/OptionSource/Theme.php rename {ReCaptchaInvisibleVersion2 => ReCaptchaVersion2Checkbox}/README.md (100%) rename {ReCaptchaVersion2 => ReCaptchaVersion2Checkbox}/composer.json (71%) rename {ReCaptchaVersion2 => ReCaptchaVersion2Checkbox}/etc/adminhtml/system.xml (90%) rename {ReCaptchaVersion2 => ReCaptchaVersion2Checkbox}/etc/config.xml (100%) create mode 100644 ReCaptchaVersion2Checkbox/etc/di.xml create mode 100644 ReCaptchaVersion2Checkbox/etc/frontend/di.xml rename {ReCaptchaVersion2 => ReCaptchaVersion2Checkbox}/etc/module.xml (83%) rename {ReCaptchaVersion2 => ReCaptchaVersion2Checkbox}/registration.php (86%) rename {ReCaptchaInvisibleVersion2 => ReCaptchaVersion2Invisible}/Model/CaptchaValidator.php (95%) create mode 100644 ReCaptchaVersion2Invisible/Model/Frontend/UiConfigProvider.php create mode 100644 ReCaptchaVersion2Invisible/Model/OptionSource/Position.php rename {ReCaptchaInvisibleVersion3 => ReCaptchaVersion2Invisible}/README.md (100%) rename {ReCaptchaInvisibleVersion3 => ReCaptchaVersion2Invisible}/composer.json (70%) rename {ReCaptchaInvisibleVersion2 => ReCaptchaVersion2Invisible}/etc/adminhtml/system.xml (87%) rename {ReCaptchaInvisibleVersion2 => ReCaptchaVersion2Invisible}/etc/config.xml (100%) create mode 100644 ReCaptchaVersion2Invisible/etc/di.xml create mode 100644 ReCaptchaVersion2Invisible/etc/frontend/di.xml rename {ReCaptchaInvisibleVersion3 => ReCaptchaVersion2Invisible}/etc/module.xml (83%) rename {ReCaptchaInvisibleVersion2 => ReCaptchaVersion2Invisible}/registration.php (86%) rename {ReCaptchaInvisibleVersion3 => ReCaptchaVersion3Invisible}/Model/CaptchaValidator.php (96%) create mode 100644 ReCaptchaVersion3Invisible/Model/Frontend/UiConfigProvider.php create mode 100644 ReCaptchaVersion3Invisible/Model/OptionSource/Position.php rename {ReCaptchaVersion2 => ReCaptchaVersion3Invisible}/README.md (100%) rename {ReCaptchaInvisibleVersion2 => ReCaptchaVersion3Invisible}/composer.json (69%) rename {ReCaptchaInvisibleVersion3 => ReCaptchaVersion3Invisible}/etc/adminhtml/system.xml (92%) rename {ReCaptchaInvisibleVersion3 => ReCaptchaVersion3Invisible}/etc/config.xml (88%) create mode 100644 ReCaptchaVersion3Invisible/etc/di.xml create mode 100644 ReCaptchaVersion3Invisible/etc/frontend/di.xml rename {ReCaptchaInvisibleVersion2 => ReCaptchaVersion3Invisible}/etc/module.xml (83%) rename {ReCaptchaInvisibleVersion3 => ReCaptchaVersion3Invisible}/registration.php (86%) diff --git a/ReCaptcha/composer.json b/ReCaptcha/composer.json index 0a7e2f61..45b04187 100644 --- a/ReCaptcha/composer.json +++ b/ReCaptcha/composer.json @@ -1,7 +1,7 @@ { "name": "magento/module-re-captcha", "version": "3.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.*", diff --git a/ReCaptcha/etc/acl.xml b/ReCaptcha/etc/acl.xml index 292eb6ae..287343b3 100644 --- a/ReCaptcha/etc/acl.xml +++ b/ReCaptcha/etc/acl.xml @@ -14,7 +14,7 @@ diff --git a/ReCaptchaAdminUi/Model/CaptchaConfig.php b/ReCaptchaAdminUi/Model/CaptchaConfig.php deleted file mode 100644 index 4716735f..00000000 --- a/ReCaptchaAdminUi/Model/CaptchaConfig.php +++ /dev/null @@ -1,175 +0,0 @@ -scopeConfig = $scopeConfig; - $this->invisibleTypes = $invisibleTypes; - $this->captchaErrorMessages = $captchaErrorMessages; - } - - /** - * @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 - { - foreach ($this->captchaErrorMessages as $captchaErrorMessage) { - if ($this->getCaptchaType() === $captchaErrorMessage['type']) { - return __($captchaErrorMessage['message']); - } - } - - return __('Incorrect ReCaptcha validation'); - } - - /** - * @return bool - */ - public function isInvisibleRecaptcha(): bool - { - return in_array($this->getCaptchaType(), $this->invisibleTypes, true); - } - - /** - * @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/composer.json b/ReCaptchaAdminUi/composer.json index 300cb638..8fe45f30 100644 --- a/ReCaptchaAdminUi/composer.json +++ b/ReCaptchaAdminUi/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaAdminUi/etc/adminhtml/system.xml b/ReCaptchaAdminUi/etc/adminhtml/system.xml index 9000dadb..cf7796d0 100644 --- a/ReCaptchaAdminUi/etc/adminhtml/system.xml +++ b/ReCaptchaAdminUi/etc/adminhtml/system.xml @@ -19,7 +19,7 @@ security Magento_ReCaptcha::config - @@ -32,168 +32,10 @@ security Magento_ReCaptcha::config -
- -
- separator-top - - security - Magento_ReCaptcha::config - - - - - - - Magento\ReCaptchaApi\Model\Config\Source\Type - - - - - - - - - - - - - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. - - recaptcha_v3 - - - - - - Magento\ReCaptchaVersion2\Model\OptionSource\Theme - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - - Magento\ReCaptchaVersion2\Model\OptionSource\Size - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - - - 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 - -
- - - - Magento\ReCaptchaInvisibleVersion3\Model\OptionSource\Position - Applicable only to Invisible reCAPTCHA v2 or reCAPTCHA v3. - - recaptcha_v3,invisible - - -
- - - - - - - Magento\ReCaptchaAdminUi\Model\Config\Source\Type - - - - - - - - - - - - - From 0.0 to 1.0, where 0.0 is absolutely a robot and 1.0 is a human. - - recaptcha_v3 - - - - - - Magento\ReCaptchaVersion2\Model\OptionSource\Theme - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - - Magento\ReCaptchaVersion2\Model\OptionSource\Size - Applicable only to reCAPTCHA v2 ("I am not a robot"). - - recaptcha - - - - - - - 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 - -
- - - - Magento\ReCaptchaInvisibleVersion3\Model\OptionSource\Position - Applicable only to Invisible reCAPTCHA v2 or reCAPTCHA v3. - - recaptcha_v3,invisible - - -
-
diff --git a/ReCaptchaApi/Api/CaptchaConfigInterface.php b/ReCaptchaApi/Api/CaptchaConfigInterface.php index a98b85c4..7571f1e0 100644 --- a/ReCaptchaApi/Api/CaptchaConfigInterface.php +++ b/ReCaptchaApi/Api/CaptchaConfigInterface.php @@ -16,13 +16,6 @@ */ interface CaptchaConfigInterface { - /** - * Get Google API Website Key - * - * @return string - */ - public function getPublicKey(): string; - /** * Get Google API Secret Key * @@ -47,42 +40,6 @@ public function getCaptchaType(): string; */ public function getScoreThreshold(): float; - /** - * Get Invisible Badge Position - * - * Applicable only to Invisible reCAPTCHA types - * - * @return string - */ - public function getInvisibleBadgePosition(): string; - - /** - * Get theme - * - * Applicable only for visible captcha type (for example "reCAPTCHA v2") - * - * @return string - */ - public function getTheme(): string; - - /** - * Get size - * - * Applicable only for visible captcha type (for example "reCAPTCHA v2") - * - * @return string - */ - public function getSize(): string; - - /** - * Get language code - * - * Applicable only for visible captcha type (for example "reCAPTCHA v2") - * - * @return string - */ - public function getLanguageCode(): string; - /** * Get error message * @@ -97,4 +54,12 @@ public function getErrorMessage(): Phrase; * @return bool */ public function isCaptchaEnabledFor(string $key): bool; + + /** + * Get reCAPTCHA type for specific functionality. Return NULL id reCAPTCHA is disabled for this functionality + * + * @param string $key + * @return string|null + */ + public function getCaptchaTypeFor(string $key): ?string; } diff --git a/ReCaptchaApi/Model/Config/Source/Type.php b/ReCaptchaApi/Model/Config/Source/Type.php deleted file mode 100644 index f7767cd8..00000000 --- a/ReCaptchaApi/Model/Config/Source/Type.php +++ /dev/null @@ -1,42 +0,0 @@ -options = $options; - } - - /** - * @inheritDoc - */ - public function toOptionArray() - { - return array_map( - function (OptionInterface $option) { - return ['value' => $option->getValue(), 'label' => __($option->getLabel())]; - }, - $this->options); - } -} diff --git a/ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php b/ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php deleted file mode 100644 index 9b98c73d..00000000 --- a/ReCaptchaApi/Model/Config/Source/Type/OptionInterface.php +++ /dev/null @@ -1,30 +0,0 @@ -invisibleTypes = $invisibleTypes; - } - - /** - * @inheritdoc - */ - public function isInvisible(string $captchaType): bool - { - return in_array($captchaType, $this->invisibleTypes, true); - } -} diff --git a/ReCaptchaApi/Model/OptionSource/Type.php b/ReCaptchaApi/Model/OptionSource/Type.php new file mode 100644 index 00000000..cf8dbfdc --- /dev/null +++ b/ReCaptchaApi/Model/OptionSource/Type.php @@ -0,0 +1,41 @@ +options = $options; + } + + /** + * @inheritDoc + */ + public function toOptionArray(): array + { + return array_values($this->options); + } +} diff --git a/ReCaptchaApi/composer.json b/ReCaptchaApi/composer.json index 540470f3..91464d0c 100644 --- a/ReCaptchaApi/composer.json +++ b/ReCaptchaApi/composer.json @@ -1,7 +1,7 @@ { "name": "magento/module-re-captcha-api", "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.*" diff --git a/ReCaptchaApi/etc/acl.xml b/ReCaptchaApi/etc/acl.xml index 292eb6ae..287343b3 100644 --- a/ReCaptchaApi/etc/acl.xml +++ b/ReCaptchaApi/etc/acl.xml @@ -14,7 +14,7 @@ diff --git a/ReCaptchaApi/etc/di.xml b/ReCaptchaApi/etc/di.xml index 174b5ad4..c56cef10 100644 --- a/ReCaptchaApi/etc/di.xml +++ b/ReCaptchaApi/etc/di.xml @@ -7,13 +7,13 @@ --> - - - + - - invisible - recaptcha_v3 + + + No + + diff --git a/ReCaptchaCheckout/Block/LayoutProcessor/Checkout/Onepage.php b/ReCaptchaCheckout/Block/LayoutProcessor/Checkout/Onepage.php index 77b04d9f..98cfbba5 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\Framework\Exception\InputException; use Magento\ReCaptchaApi\Api\CaptchaConfigInterface; -use Magento\ReCaptchaUi\Model\CaptchaUiSettingsProviderInterface; +use Magento\ReCaptchaUi\Model\UiConfigResolverInterface; /** * Checkout layout processor @@ -17,9 +18,9 @@ class Onepage implements LayoutProcessorInterface { /** - * @var CaptchaUiSettingsProviderInterface + * @var UiConfigResolverInterface */ - private $captchaUiSettingsProvider; + private $captchaUiConfigResolver; /** * @var CaptchaConfigInterface @@ -27,29 +28,34 @@ class Onepage implements LayoutProcessorInterface private $captchaConfig; /** - * @param CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider + * @param UiConfigResolverInterface $captchaUiConfigResolver * @param CaptchaConfigInterface $captchaConfig */ public function __construct( - CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider, + UiConfigResolverInterface $captchaUiConfigResolver, CaptchaConfigInterface $captchaConfig ) { - $this->captchaUiSettingsProvider = $captchaUiSettingsProvider; + $this->captchaUiConfigResolver = $captchaUiConfigResolver; $this->captchaConfig = $captchaConfig; } /** - * @inheritDoc + * {@inheritdoc} + * + * @param array $jsLayout + * @return array + * @throws InputException */ public function process($jsLayout) { - if ($this->captchaConfig->isCaptchaEnabledFor('customer_login')) { + $key = 'customer_login'; + if ($this->captchaConfig->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..f455dead 100644 --- a/ReCaptchaCheckout/composer.json +++ b/ReCaptchaCheckout/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaContact/composer.json b/ReCaptchaContact/composer.json index dde78157..53d31f97 100644 --- a/ReCaptchaContact/composer.json +++ b/ReCaptchaContact/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaContact/etc/adminhtml/system.xml b/ReCaptchaContact/etc/adminhtml/system.xml index 57472b63..f2a55236 100644 --- a/ReCaptchaContact/etc/adminhtml/system.xml +++ b/ReCaptchaContact/etc/adminhtml/system.xml @@ -9,11 +9,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
- - + - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type
diff --git a/ReCaptchaContact/etc/config.xml b/ReCaptchaContact/etc/config.xml index 7dd58443..65ff2070 100644 --- a/ReCaptchaContact/etc/config.xml +++ b/ReCaptchaContact/etc/config.xml @@ -9,9 +9,9 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> - - 0 - + + null +
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/Observer/AjaxLoginObserver.php b/ReCaptchaCustomer/Observer/AjaxLoginObserver.php index 8eea6ea2..5d7e8eb0 100644 --- a/ReCaptchaCustomer/Observer/AjaxLoginObserver.php +++ b/ReCaptchaCustomer/Observer/AjaxLoginObserver.php @@ -94,7 +94,8 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('customer_login')) { + $key = 'customer_login'; + if ($this->captchaConfig->isCaptchaEnabledFor($key)) { /** @var Action $controller */ $controller = $observer->getControllerAction(); $request = $controller->getRequest(); @@ -105,7 +106,7 @@ public function execute(Observer $observer): void $validationConfig = $this->validationConfigFactory->create( [ 'privateKey' => $this->captchaConfig->getPrivateKey(), - 'captchaType' => $this->captchaConfig->getCaptchaType(), + 'captchaType' => $this->captchaConfig->getCaptchaTypeFor($key), 'remoteIp' => $this->remoteAddress->getRemoteAddress(), 'scoreThreshold' => $this->captchaConfig->getScoreThreshold(), 'extensionAttributes' => null, diff --git a/ReCaptchaCustomer/Plugin/Block/Account/InjectRecaptchaInAuthenticationPopup.php b/ReCaptchaCustomer/Plugin/Block/Account/InjectRecaptchaInAuthenticationPopup.php index f0ce5809..4b3e1205 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\UiConfigResolverInterface; /** * Inject authentication popup in layout @@ -18,9 +19,9 @@ class InjectRecaptchaInAuthenticationPopup { /** - * @var CaptchaUiSettingsProviderInterface + * @var UiConfigResolverInterface */ - private $captchaUiSettingsProvider; + private $captchaUiConfigResolver; /** * @var CaptchaConfigInterface @@ -33,16 +34,16 @@ class InjectRecaptchaInAuthenticationPopup private $serializer; /** - * @param CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider + * @param UiConfigResolverInterface $captchaUiConfigResolver * @param CaptchaConfigInterface $captchaConfig * @param Json $serializer */ public function __construct( - CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider, + UiConfigResolverInterface $captchaUiConfigResolver, CaptchaConfigInterface $captchaConfig, Json $serializer ) { - $this->captchaUiSettingsProvider = $captchaUiSettingsProvider; + $this->captchaUiConfigResolver = $captchaUiConfigResolver; $this->captchaConfig = $captchaConfig; $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->captchaConfig->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..6e7a4866 100644 --- a/ReCaptchaCustomer/composer.json +++ b/ReCaptchaCustomer/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaCustomer/etc/adminhtml/system.xml b/ReCaptchaCustomer/etc/adminhtml/system.xml index 23a6c3c7..c83b515a 100644 --- a/ReCaptchaCustomer/etc/adminhtml/system.xml +++ b/ReCaptchaCustomer/etc/adminhtml/system.xml @@ -9,21 +9,21 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
- - + - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type - - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type - - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type
diff --git a/ReCaptchaCustomer/etc/config.xml b/ReCaptchaCustomer/etc/config.xml index 8b73ce4a..144a6093 100644 --- a/ReCaptchaCustomer/etc/config.xml +++ b/ReCaptchaCustomer/etc/config.xml @@ -9,11 +9,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> - - 0 - 0 - 0 - + + null + null + null + diff --git a/ReCaptchaCustomer/view/frontend/layout/customer_account_create.xml b/ReCaptchaCustomer/view/frontend/layout/customer_account_create.xml index 1ffcf250..9107c3b4 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/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 index c1ed9c37..093dc5bb 100644 --- a/ReCaptchaFrontendUi/Model/CaptchaConfig.php +++ b/ReCaptchaFrontendUi/Model/CaptchaConfig.php @@ -17,28 +17,15 @@ */ class CaptchaConfig implements CaptchaConfigInterface { - private const XML_PATH_TYPE = 'recaptcha/frontend/type'; - private const XML_PATH_PUBLIC_KEY = 'recaptcha/frontend/public_key'; private const XML_PATH_PRIVATE_KEY = 'recaptcha/frontend/private_key'; - private const XML_PATH_SCORE_THRESHOLD = 'recaptcha/frontend/score_threshold'; - private const XML_PATH_SIZE = 'recaptcha/frontend/size'; - private const XML_PATH_THEME = 'recaptcha/frontend/theme'; - private const XML_PATH_POSITION = 'recaptcha/frontend/position'; - private const XML_PATH_LANGUAGE_CODE = 'recaptcha/frontend/lang'; - - private const XML_PATH_IS_ENABLED_FOR = 'recaptcha/frontend/enabled_for_'; + private const XML_PATH_TYPE_FOR = 'recaptcha_frontend/type_for/'; /** * @var ScopeConfigInterface */ private $scopeConfig; - /** - * @var array - */ - private $invisibleTypes; - /** * @var array */ @@ -46,27 +33,16 @@ class CaptchaConfig implements CaptchaConfigInterface /** * @param ScopeConfigInterface $scopeConfig - * @param array $invisibleTypes * @param array $captchaErrorMessages */ public function __construct( ScopeConfigInterface $scopeConfig, - array $invisibleTypes = [], array $captchaErrorMessages = [] ) { $this->scopeConfig = $scopeConfig; - $this->invisibleTypes = $invisibleTypes; $this->captchaErrorMessages = $captchaErrorMessages; } - /** - * @inheritdoc - */ - public function getPublicKey(): string - { - return trim((string)$this->scopeConfig->getValue(self::XML_PATH_PUBLIC_KEY, ScopeInterface::SCOPE_WEBSITE)); - } - /** * @inheritdoc */ @@ -75,14 +51,6 @@ public function getPrivateKey(): string return trim((string)$this->scopeConfig->getValue(self::XML_PATH_PRIVATE_KEY, ScopeInterface::SCOPE_WEBSITE)); } - /** - * @inheritdoc - */ - public function isInvisibleRecaptcha(): bool - { - return in_array($this->getCaptchaType(), $this->invisibleTypes, true); - } - /** * @inheritdoc */ @@ -91,51 +59,6 @@ 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 */ @@ -158,7 +81,7 @@ public function getErrorMessage(): Phrase } } - return __('Incorrect ReCaptcha validation'); + return __('Incorrect reCAPTCHA validation'); } /** @@ -166,12 +89,19 @@ public function getErrorMessage(): Phrase */ public function isCaptchaEnabledFor(string $key): bool { - if (!$this->areKeysConfigured()) { - return false; - } + return /*$this->areKeysConfigured() &&*/ (null !== $this->getCaptchaTypeFor($key)); + } - $flag = self::XML_PATH_IS_ENABLED_FOR . $key; - return $this->scopeConfig->isSetFlag($flag, ScopeInterface::SCOPE_WEBSITE); + /** + * @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..0daaa0d2 100644 --- a/ReCaptchaFrontendUi/composer.json +++ b/ReCaptchaFrontendUi/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js b/ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js index 38ca7473..0070b9e0 100644 --- a/ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js +++ b/ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js @@ -63,7 +63,7 @@ define( }, /** - * Checking that reCaptcha is invisible type + * Checking that reCAPTCHA is invisible type * @returns {Boolean} */ getIsInvisibleRecaptcha: function () { @@ -71,7 +71,7 @@ define( }, /** - * Recaptcha callback + * reCAPTCHA callback * @param {String} token */ reCaptchaCallback: function (token) { @@ -82,7 +82,7 @@ define( }, /** - * Initialize reCaptcha after first rendering + * Initialize reCAPTCHA after first rendering */ initCaptcha: function () { var me = this, @@ -105,7 +105,7 @@ define( * https://stackoverflow.com/questions/46657573/recaptcha-the-bind-parameter-must-be-an-element-or-id * * We create a wrapper element with a wrapping id and we inject the real ID with jQuery. - * In this way we have no data-bind attribute at all in our reCaptcha div + * In this way we have no data-bind attribute at all in our reCAPTCHA div */ $wrapper = $('#' + this.getReCaptchaId() + '-wrapper'); $reCaptcha = $wrapper.find('.g-recaptcha'); @@ -124,7 +124,7 @@ define( me.validateReCaptcha(false); } }, - this.settings.render + this.settings.rendering ); // eslint-disable-next-line no-undef @@ -165,14 +165,14 @@ define( }, /** - * Render reCaptcha + * Render reCAPTCHA */ renderReCaptcha: function () { var me = this; - if (window.grecaptcha && window.grecaptcha.render) { // Check if recaptcha is already loaded + if (window.grecaptcha && window.grecaptcha.render) { // Check if reCAPTCHA is already loaded me.initCaptcha(); - } else { // Wait for recaptcha to be loaded + } else { // Wait for reCAPTCHA to be loaded $(window).on('recaptchaapiready', function () { me.initCaptcha(); }); @@ -180,7 +180,7 @@ define( }, /** - * Get reCaptcha ID + * Get reCAPTCHA ID * @returns {String} */ getReCaptchaId: function () { diff --git a/ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php b/ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php deleted file mode 100644 index ff4af4a8..00000000 --- a/ReCaptchaInvisibleVersion2/Model/Config/Source/Type/Option.php +++ /dev/null @@ -1,36 +0,0 @@ - 'inline', 'label' => __('Inline')], - ['value' => 'bottomright', 'label' => __('Bottom Right')], - ['value' => 'bottomleft', 'label' => __('Bottom Left')], - ]; - } -} diff --git a/ReCaptchaInvisibleVersion2/etc/di.xml b/ReCaptchaInvisibleVersion2/etc/di.xml deleted file mode 100644 index 097a8961..00000000 --- a/ReCaptchaInvisibleVersion2/etc/di.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - Magento\ReCaptchaInvisibleVersion2\Model\CaptchaValidator - - - - - - - Magento\ReCaptchaInvisibleVersion2\Model\Config\Source\Type\Option - - - - - - - invisible - - - - - - - invisible - - - - diff --git a/ReCaptchaInvisibleVersion3/Model/Config/Source/Type/Option.php b/ReCaptchaInvisibleVersion3/Model/Config/Source/Type/Option.php deleted file mode 100644 index b50d38f6..00000000 --- a/ReCaptchaInvisibleVersion3/Model/Config/Source/Type/Option.php +++ /dev/null @@ -1,36 +0,0 @@ - 'inline', 'label' => __('Inline')], - ['value' => 'bottomright', 'label' => __('Bottom Right')], - ['value' => 'bottomleft', 'label' => __('Bottom Left')], - ]; - } -} diff --git a/ReCaptchaInvisibleVersion3/etc/di.xml b/ReCaptchaInvisibleVersion3/etc/di.xml deleted file mode 100644 index 2d15a721..00000000 --- a/ReCaptchaInvisibleVersion3/etc/di.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - Magento\ReCaptchaInvisibleVersion3\Model\CaptchaValidator - - - - - - - Magento\ReCaptchaInvisibleVersion3\Model\Config\Source\Type\Option - - - - - - - recaptcha_v3 - - - - recaptcha_v3 - You cannot proceed with such operation, your reCaptcha reputation is too low. - - - - - - - - recaptcha_v3 - - - - recaptcha_v3 - You cannot proceed with such operation, your reCaptcha reputation is too low. - - - - - 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..541c8ebe 100644 --- a/ReCaptchaNewsletter/composer.json +++ b/ReCaptchaNewsletter/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaNewsletter/etc/adminhtml/system.xml b/ReCaptchaNewsletter/etc/adminhtml/system.xml index 8758f15e..5c3464b5 100644 --- a/ReCaptchaNewsletter/etc/adminhtml/system.xml +++ b/ReCaptchaNewsletter/etc/adminhtml/system.xml @@ -9,12 +9,12 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
- - + If enabled, a badge will be displayed in every page. - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type
diff --git a/ReCaptchaNewsletter/etc/config.xml b/ReCaptchaNewsletter/etc/config.xml index ae0ce375..4641240a 100644 --- a/ReCaptchaNewsletter/etc/config.xml +++ b/ReCaptchaNewsletter/etc/config.xml @@ -9,9 +9,9 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> - - 0 - + + null + diff --git a/ReCaptchaNewsletter/etc/frontend/di.xml b/ReCaptchaNewsletter/etc/frontend/di.xml deleted file mode 100644 index 7c661b6f..00000000 --- a/ReCaptchaNewsletter/etc/frontend/di.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - 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/composer.json b/ReCaptchaReview/composer.json index c3efd1d5..7a139e9c 100644 --- a/ReCaptchaReview/composer.json +++ b/ReCaptchaReview/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaReview/etc/adminhtml/system.xml b/ReCaptchaReview/etc/adminhtml/system.xml index 10c45747..908a7341 100644 --- a/ReCaptchaReview/etc/adminhtml/system.xml +++ b/ReCaptchaReview/etc/adminhtml/system.xml @@ -9,11 +9,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
- - + - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type
diff --git a/ReCaptchaReview/etc/config.xml b/ReCaptchaReview/etc/config.xml index e6710139..ce3222e6 100644 --- a/ReCaptchaReview/etc/config.xml +++ b/ReCaptchaReview/etc/config.xml @@ -9,9 +9,9 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> - - 0 - + + null + 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/composer.json b/ReCaptchaSendFriend/composer.json index c1a04341..e3c0e82b 100644 --- a/ReCaptchaSendFriend/composer.json +++ b/ReCaptchaSendFriend/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaSendFriend/etc/adminhtml/system.xml b/ReCaptchaSendFriend/etc/adminhtml/system.xml index 14e71fc8..87c8cae8 100644 --- a/ReCaptchaSendFriend/etc/adminhtml/system.xml +++ b/ReCaptchaSendFriend/etc/adminhtml/system.xml @@ -9,11 +9,11 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
- - + - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type
diff --git a/ReCaptchaSendFriend/etc/config.xml b/ReCaptchaSendFriend/etc/config.xml index b0fe3f59..fa1f50d4 100644 --- a/ReCaptchaSendFriend/etc/config.xml +++ b/ReCaptchaSendFriend/etc/config.xml @@ -9,9 +9,9 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> - - 0 - + + null + 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..674e0df1 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\UiConfigResolverInterface; /** * @api @@ -18,9 +19,9 @@ class ReCaptcha extends Template { /** - * @var CaptchaUiSettingsProviderInterface + * @var UiConfigResolverInterface */ - private $captchaUiSettingsProvider; + private $captchaUiConfigResolver; /** * @var CaptchaConfigInterface @@ -34,26 +35,26 @@ class ReCaptcha extends Template /** * @param Template\Context $context - * @param CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider + * @param UiConfigResolverInterface $captchaUiConfigResolver * @param CaptchaConfigInterface $captchaConfig * @param Json $serializer * @param array $data */ public function __construct( Template\Context $context, - CaptchaUiSettingsProviderInterface $captchaUiSettingsProvider, + UiConfigResolverInterface $captchaUiConfigResolver, CaptchaConfigInterface $captchaConfig, Json $serializer, array $data = [] ) { parent::__construct($context, $data); - $this->captchaUiSettingsProvider = $captchaUiSettingsProvider; + $this->captchaUiConfigResolver = $captchaUiConfigResolver; $this->captchaConfig = $captchaConfig; $this->serializer = $serializer; } /** - * Get current recaptcha ID + * Get reCAPTCHA ID */ public function getRecaptchaId() { @@ -61,7 +62,10 @@ public function getRecaptchaId() } /** - * @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,18 +88,22 @@ 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; } @@ -104,8 +112,8 @@ public function getCaptchaSettings(): array */ public function toHtml() { - $enabledFor = $this->getData('enabled_for'); - if (empty($enabledFor) || !$this->captchaConfig->isCaptchaEnabledFor($enabledFor)) { + $key = $this->getData('recaptcha_for'); + if (empty($key) || !$this->captchaConfig->isCaptchaEnabledFor($key)) { return ''; } diff --git a/ReCaptchaUi/Model/CaptchaUiSettingsProvider.php b/ReCaptchaUi/Model/CaptchaUiSettingsProvider.php deleted file mode 100644 index 8936bac6..00000000 --- a/ReCaptchaUi/Model/CaptchaUiSettingsProvider.php +++ /dev/null @@ -1,57 +0,0 @@ -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/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..ee78640a --- /dev/null +++ b/ReCaptchaUi/Model/UiConfigResolver.php @@ -0,0 +1,71 @@ +captchaConfig = $captchaConfig; + + foreach ($captchaUiConfigProviders as $captchaUiConfigProvider) { + if (!$captchaUiConfigProvider instanceof UiConfigProviderInterface) { + throw new InputException( + __( + 'UI config provider must implement %interface.', + [ + 'interface' => UiConfigResolverInterface::class, + ] + ) + ); + } + } + $this->captchaUiConfigProviders = $captchaUiConfigProviders; + } + + /** + * @inheritdoc + */ + public function get(string $key): array + { + $captchaType = $this->captchaConfig->getCaptchaTypeFor($key); + + if (!isset($this->captchaUiConfigProviders[$captchaType])) { + throw new InputException( + __('UI config provider for "%type" does not configured.', ['type' => $captchaType]) + ); + } + return $this->captchaUiConfigProviders[$key]->get(); + } +} diff --git a/ReCaptchaUi/Model/UiConfigResolverInterface.php b/ReCaptchaUi/Model/UiConfigResolverInterface.php new file mode 100644 index 00000000..5d21b36f --- /dev/null +++ b/ReCaptchaUi/Model/UiConfigResolverInterface.php @@ -0,0 +1,27 @@ + - + 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/LoginObserver.php b/ReCaptchaUser/Observer/LoginObserver.php index 8a2d14ec..3da2078a 100644 --- a/ReCaptchaUser/Observer/LoginObserver.php +++ b/ReCaptchaUser/Observer/LoginObserver.php @@ -94,7 +94,8 @@ public function __construct( */ public function execute(Observer $observer): void { - if ($this->captchaConfig->isCaptchaEnabledFor('user_login') + $key = 'user_login'; + if ($this->captchaConfig->isCaptchaEnabledFor($key) && $this->request->getFullActionName() === $this->loginActionName ) { $reCaptchaResponse = $this->captchaResponseResolver->resolve($this->request); @@ -102,7 +103,7 @@ public function execute(Observer $observer): void $validationConfig = $this->validationConfigFactory->create( [ 'privateKey' => $this->captchaConfig->getPrivateKey(), - 'captchaType' => $this->captchaConfig->getCaptchaType(), + 'captchaType' => $this->captchaConfig->getCaptchaTypeFor($key), 'remoteIp' => $this->remoteAddress->getRemoteAddress(), 'scoreThreshold' => $this->captchaConfig->getScoreThreshold(), 'extensionAttributes' => null, diff --git a/ReCaptchaUser/composer.json b/ReCaptchaUser/composer.json index f9db4757..dc9f0458 100644 --- a/ReCaptchaUser/composer.json +++ b/ReCaptchaUser/composer.json @@ -1,7 +1,7 @@ { "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.*", diff --git a/ReCaptchaUser/etc/adminhtml/system.xml b/ReCaptchaUser/etc/adminhtml/system.xml index fad8d141..3aeb0e01 100644 --- a/ReCaptchaUser/etc/adminhtml/system.xml +++ b/ReCaptchaUser/etc/adminhtml/system.xml @@ -9,17 +9,17 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
- - + - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type - - Magento\Config\Model\Config\Source\Yesno + Magento\ReCaptchaApi\Model\OptionSource\Type
diff --git a/ReCaptchaUser/etc/config.xml b/ReCaptchaUser/etc/config.xml index 8f541706..437b1acc 100644 --- a/ReCaptchaUser/etc/config.xml +++ b/ReCaptchaUser/etc/config.xml @@ -9,10 +9,10 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> - - 0 - 0 - + + null + null + 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">