Skip to content

Commit 496055e

Browse files
authored
Merge pull request #191 from magento/recaptcha-initialization
Recaptcha initialization
2 parents 12e8fd2 + 0debaf5 commit 496055e

File tree

18 files changed

+352
-295
lines changed

18 files changed

+352
-295
lines changed

ReCaptchaAdminUi/view/adminhtml/layout/recaptcha.xml

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

ReCaptchaCustomer/Observer/AjaxLoginObserver.php

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

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

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

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

9181
/**
@@ -108,30 +98,20 @@ public function execute(Observer $observer): void
10898
$reCaptchaResponse = $this->captchaResponseResolver->resolve($request);
10999
} catch (InputException $e) {
110100
$this->logger->error($e);
111-
$this->processError($response, $validationConfig->getValidationFailureMessage());
101+
$this->errorProcessor->processError(
102+
$response,
103+
$validationConfig->getValidationFailureMessage()
104+
);
112105
return;
113106
}
114107

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

ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ define(
1010
'uiComponent',
1111
'jquery',
1212
'ko',
13-
'Magento_ReCaptchaFrontendUi/js/registry'
13+
'Magento_ReCaptchaFrontendUi/js/registry',
14+
'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader'
1415
],
15-
function (Component, $, ko, registry, undefined) {
16+
function (Component, $, ko, registry, reCaptchaLoader, undefined) {
1617
'use strict';
1718

1819
return Component.extend({
@@ -50,16 +51,7 @@ define(
5051
$(window).trigger('recaptchaapiready');
5152
}.bind(this);
5253

53-
element = document.createElement('script');
54-
scriptTag = document.getElementsByTagName('script')[0];
55-
56-
element.async = true;
57-
element.src = 'https://www.google.com/recaptcha/api.js' +
58-
'?onload=globalOnRecaptchaOnLoadCallback&render=explicit' +
59-
(this.settings.rendering.lang ? '&hl=' + this.settings.rendering.lang : '');
60-
61-
scriptTag.parentNode.insertBefore(element, scriptTag);
62-
54+
reCaptchaLoader.addReCaptchaScriptTag();
6355
},
6456

6557
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([], function () {
7+
'use strict';
8+
9+
var scriptTagAdded = false;
10+
11+
return {
12+
/**
13+
* Add script tag. Script tag should be added once
14+
*/
15+
addReCaptchaScriptTag: function () {
16+
var element,
17+
scriptTag;
18+
19+
if (!scriptTagAdded) {
20+
element = document.createElement('script');
21+
scriptTag = document.getElementsByTagName('script')[0];
22+
23+
element.async = true;
24+
element.src = 'https://www.google.com/recaptcha/api.js' +
25+
'?onload=globalOnRecaptchaOnLoadCallback&render=explicit';
26+
27+
scriptTag.parentNode.insertBefore(element, scriptTag);
28+
scriptTagAdded = true;
29+
}
30+
}
31+
}
32+
}
33+
);

ReCaptchaUser/view/adminhtml/templates/recaptcha.phtml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
$config = $block->getCaptchaUiConfig();
88
$renderingOptions = $config['rendering'] ?? [];
99
$isInvisible = !empty($config['invisible']);
10-
$languageCode = $config['rendering']['lang'] ?? null;
1110
?>
1211
<div class="admin__field <?= /* @noEscape */ $isInvisible ? 'field-invisible-recaptcha' : 'field-recaptcha' ?>">
1312
<div id="admin-recaptcha"
14-
class="admin-recaptcha-content<?= /* @noEscape */ !empty($renderingOptions['size']) ? ' size-' . $renderingOptions['size'] : '' ?>"></div>
13+
class="admin-recaptcha-content<?=
14+
/* @noEscape */ !empty($renderingOptions['size']) ? ' size-' . $renderingOptions['size'] : '' ?>"></div>
1515
</div>
1616

1717
<script>
@@ -25,25 +25,24 @@ $languageCode = $config['rendering']['lang'] ?? null;
2525

2626
element.async = true;
2727
element.src = 'https://www.google.com/recaptcha/api.js'
28-
+ '?onload=globalOnRecaptchaOnLoadCallback&render=explicit'
29-
<?php if (null !== $languageCode): ?><?="+'&hl={$languageCode}'" ?><?php endif; ?>;
28+
+ '?onload=globalOnRecaptchaOnLoadCallback&render=explicit';
3029

3130
window.globalOnRecaptchaOnLoadCallback = function () {
3231
let token = '';
3332

3433
this.widgetId = grecaptcha.render('admin-recaptcha', {
35-
<?php foreach($renderingOptions as $key => $value):?>
34+
<?php foreach ($renderingOptions as $key => $value): ?>
3635
'<?= $block->escapeJs($key) ?>': '<?= $block->escapeJs($value) ?>',
3736
<?php endforeach; ?>
3837
'callback': function (token) { // jscs:ignore jsDoc
39-
<?php if ($isInvisible) : ?>
38+
<?php if ($isInvisible): ?>
4039
this.token = token;
4140
$('#login-form').submit();
4241
<?php endif; ?>
4342
}.bind(this)
4443
});
4544

46-
<?php if ($isInvisible) : ?>
45+
<?php if ($isInvisible): ?>
4746
$('#login-form').submit(function (event) {
4847
if (!this.token) {
4948
event.preventDefault(event);

ReCaptchaVersion2Checkbox/Model/Adminhtml/UiConfigProvider.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,6 @@ public function __construct(
3434
$this->scopeConfig = $scopeConfig;
3535
}
3636

37-
/**
38-
* @inheritdoc
39-
*/
40-
public function get(): array
41-
{
42-
$config = [
43-
'rendering' => [
44-
'sitekey' => $this->getPublicKey(),
45-
'size' => $this->getSize(),
46-
'theme' => $this->getTheme(),
47-
'lang' => $this->getLanguageCode(),
48-
],
49-
'invisible' => false,
50-
];
51-
return $config;
52-
}
53-
5437
/**
5538
* Get Google API Website Key
5639
*
@@ -96,4 +79,21 @@ private function getLanguageCode(): string
9679
self::XML_PATH_LANGUAGE_CODE
9780
);
9881
}
82+
83+
/**
84+
* @inheritdoc
85+
*/
86+
public function get(): array
87+
{
88+
$config = [
89+
'rendering' => [
90+
'sitekey' => $this->getPublicKey(),
91+
'size' => $this->getSize(),
92+
'theme' => $this->getTheme(),
93+
'hl' => $this->getLanguageCode(),
94+
],
95+
'invisible' => false,
96+
];
97+
return $config;
98+
}
9999
}

ReCaptchaVersion2Checkbox/Model/Adminhtml/ValidationConfigProvider.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,6 @@ public function __construct(
5151
$this->validationConfigFactory = $validationConfigFactory;
5252
}
5353

54-
/**
55-
* @inheritdoc
56-
*/
57-
public function get(): ValidationConfigInterface
58-
{
59-
/** @var ValidationConfigInterface $validationConfig */
60-
$validationConfig = $this->validationConfigFactory->create(
61-
[
62-
'privateKey' => $this->getPrivateKey(),
63-
'remoteIp' => $this->remoteAddress->getRemoteAddress(),
64-
'validationFailureMessage' => $this->getValidationFailureMessage(),
65-
'extensionAttributes' => null,
66-
]
67-
);
68-
return $validationConfig;
69-
}
70-
7154
/**
7255
* Get Google API Secret Key
7356
*
@@ -89,4 +72,21 @@ private function getValidationFailureMessage(): string
8972
(string)$this->scopeConfig->getValue(self::XML_PATH_VALIDATION_FAILURE)
9073
);
9174
}
75+
76+
/**
77+
* @inheritdoc
78+
*/
79+
public function get(): ValidationConfigInterface
80+
{
81+
/** @var ValidationConfigInterface $validationConfig */
82+
$validationConfig = $this->validationConfigFactory->create(
83+
[
84+
'privateKey' => $this->getPrivateKey(),
85+
'remoteIp' => $this->remoteAddress->getRemoteAddress(),
86+
'validationFailureMessage' => $this->getValidationFailureMessage(),
87+
'extensionAttributes' => null,
88+
]
89+
);
90+
return $validationConfig;
91+
}
9292
}

0 commit comments

Comments
 (0)