Skip to content

Commit 4655e39

Browse files
committed
Merge remote-tracking branch 'origin/develop' into 2.4.6-develop-sync
2 parents aa55aae + c767587 commit 4655e39

File tree

6 files changed

+495
-0
lines changed

6 files changed

+495
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\ReCaptchaVersion3Invisible\Model;
10+
11+
use Magento\ReCaptchaValidationApi\Api\Data\ValidationConfigInterface;
12+
use Magento\ReCaptchaVersion3Invisible\Model\Frontend\UiConfigProvider;
13+
use Magento\ReCaptchaVersion3Invisible\Model\Frontend\ValidationConfigProvider;
14+
15+
class Config
16+
{
17+
/**
18+
* @var string|null
19+
*/
20+
private ?string $websiteKey = null;
21+
22+
/**
23+
* @var float|null
24+
*/
25+
private ?float $minimumScore = null;
26+
27+
/**
28+
* @var string|null
29+
*/
30+
private ?string $badgePosition = null;
31+
32+
/**
33+
* @var string|null
34+
*/
35+
private ?string $languageCode = null;
36+
37+
/**
38+
* @var UiConfigProvider
39+
*/
40+
private UiConfigProvider $uiConfigProvider;
41+
42+
/**
43+
* @var array
44+
*/
45+
private array $uiConfig;
46+
47+
/**
48+
* @var ValidationConfigProvider
49+
*/
50+
private ValidationConfigProvider $validationConfigProvider;
51+
52+
/**
53+
* @var ValidationConfigInterface|null
54+
*/
55+
private ?ValidationConfigInterface $validationConfig = null;
56+
57+
/**
58+
* @var array
59+
*/
60+
private array $formTypes;
61+
62+
/**
63+
* @param UiConfigProvider $uiConfigProvider
64+
* @param ValidationConfigProvider $validationConfigProvider
65+
* @param array $formTypes
66+
*/
67+
public function __construct(
68+
UiConfigProvider $uiConfigProvider,
69+
ValidationConfigProvider $validationConfigProvider,
70+
array $formTypes = []
71+
) {
72+
$this->formTypes = $formTypes;
73+
$this->uiConfigProvider = $uiConfigProvider;
74+
$this->validationConfigProvider = $validationConfigProvider;
75+
}
76+
77+
/**
78+
* Get website's Google API public key
79+
*
80+
* @return string
81+
*/
82+
public function getWebsiteKey(): string
83+
{
84+
if (!$this->websiteKey) {
85+
$this->websiteKey = $this->getUiConfig()['rendering']['sitekey'];
86+
}
87+
return $this->websiteKey;
88+
}
89+
90+
/**
91+
* Get configured minimum score value
92+
*
93+
* @return float
94+
*/
95+
public function getMinimumScore(): float
96+
{
97+
if (!$this->minimumScore) {
98+
$this->minimumScore = $this->validationConfig->getExtensionAttributes()->getScoreThreshold();
99+
}
100+
return $this->minimumScore;
101+
}
102+
103+
/**
104+
* Get configured captcha's badge position
105+
*
106+
* @return string
107+
*/
108+
public function getBadgePosition(): string
109+
{
110+
if (!$this->badgePosition) {
111+
$this->badgePosition = $this->getUiConfig()['rendering']['badge'];
112+
}
113+
return $this->badgePosition;
114+
}
115+
116+
/**
117+
* Get code of language to send notifications
118+
*
119+
* @return string
120+
*/
121+
public function getLanguageCode(): string
122+
{
123+
if (!$this->languageCode) {
124+
$this->languageCode = $this->getUiConfig()['rendering']['hl'];
125+
}
126+
return $this->languageCode;
127+
}
128+
129+
/**
130+
* Get ReCaptchaV3's available form types
131+
*
132+
* @return array
133+
*/
134+
public function getFormTypes(): array
135+
{
136+
return $this->formTypes;
137+
}
138+
139+
/**
140+
* Get front-end's validation configurations
141+
*
142+
* @return ValidationConfigInterface
143+
*/
144+
public function getValidationConfig(): ValidationConfigInterface
145+
{
146+
if (!$this->validationConfig) {
147+
$this->validationConfig = $this->validationConfigProvider->get();
148+
}
149+
return $this->validationConfig;
150+
}
151+
152+
/**
153+
* Get front-end's UI configurations
154+
*
155+
* @return array
156+
*/
157+
private function getUiConfig(): array
158+
{
159+
if (empty($this->uiConfig)) {
160+
$this->uiConfig = $this->uiConfigProvider->get();
161+
}
162+
return $this->uiConfig;
163+
}
164+
}

ReCaptchaVersion3Invisible/etc/di.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,20 @@
8484
</argument>
8585
</arguments>
8686
</type>
87+
<type name="Magento\ReCaptchaVersion3Invisible\Model\Config">
88+
<arguments>
89+
<argument name="formTypes" xsi:type="array">
90+
<item name="place_order" xsi:type="string">place_order</item>
91+
<item name="contact" xsi:type="string">contact</item>
92+
<item name="customer_forgot_password" xsi:type="string">customer_forgot_password</item>
93+
<item name="customer_edit" xsi:type="string">customer_edit</item>
94+
<item name="customer_login" xsi:type="string">customer_login</item>
95+
<item name="customer_create" xsi:type="string">customer_create</item>
96+
<item name="newsletter" xsi:type="string">newsletter</item>
97+
<item name="product_review" xsi:type="string">product_review</item>
98+
<item name="sendfriend" xsi:type="string">sendfriend</item>
99+
<item name="braintree" xsi:type="string">braintree</item>
100+
</argument>
101+
</arguments>
102+
</type>
87103
</config>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\ReCaptchaWebapiGraphQl\Model\Resolver;
10+
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\ReCaptchaFrontendUi\Model\CaptchaTypeResolver;
16+
use Magento\ReCaptchaFrontendUi\Model\ErrorMessageConfig;
17+
use Magento\ReCaptchaVersion3Invisible\Model\Config;
18+
19+
class ReCaptchaV3 implements ResolverInterface
20+
{
21+
private const RECAPTCHA_TYPE = 'recaptcha_v3';
22+
23+
/**
24+
* @var bool|null
25+
*/
26+
private ?bool $isEnabled = null;
27+
28+
/**
29+
* @var Config
30+
*/
31+
private Config $reCaptchaV3Config;
32+
33+
/**
34+
* @var CaptchaTypeResolver
35+
*/
36+
private CaptchaTypeResolver $captchaTypeResolver;
37+
38+
/**
39+
* @var string|null
40+
*/
41+
private ?string $failureMessage = null;
42+
43+
/**
44+
* @var ErrorMessageConfig $errorMessageConfig
45+
*/
46+
private ErrorMessageConfig $errorMessageConfig;
47+
48+
/**
49+
* @param Config $reCaptchaV3Config
50+
* @param CaptchaTypeResolver $captchaTypeResolver
51+
* @param ErrorMessageConfig $errorMessageConfig
52+
*/
53+
public function __construct(
54+
Config $reCaptchaV3Config,
55+
CaptchaTypeResolver $captchaTypeResolver,
56+
ErrorMessageConfig $errorMessageConfig
57+
) {
58+
$this->reCaptchaV3Config = $reCaptchaV3Config;
59+
$this->captchaTypeResolver = $captchaTypeResolver;
60+
$this->errorMessageConfig = $errorMessageConfig;
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function resolve(
67+
Field $field,
68+
$context,
69+
ResolveInfo $info,
70+
array $value = null,
71+
array $args = null
72+
) {
73+
return [
74+
'is_enabled' => $this->isEnabled(),
75+
'website_key' => $this->reCaptchaV3Config->getWebsiteKey(),
76+
'minimum_score' => $this->reCaptchaV3Config->getMinimumScore(),
77+
'badge_position' => $this->reCaptchaV3Config->getBadgePosition(),
78+
'language_code' => $this->reCaptchaV3Config->getLanguageCode(),
79+
'failure_message' => $this->getFailureMessage(),
80+
'forms' => $this->getEnumFormTypes()
81+
];
82+
}
83+
84+
/**
85+
* Get whether service has all the required settings set up to be enabled or not
86+
*
87+
* @return bool
88+
* @throws InputException
89+
*/
90+
public function isEnabled(): bool
91+
{
92+
if ($this->isEnabled === null) {
93+
$this->isEnabled = $this->reCaptchaV3Config->getValidationConfig()->getPrivateKey() &&
94+
!empty($this->reCaptchaV3Config->getWebsiteKey()) &&
95+
!empty($this->getEnumFormTypes());
96+
}
97+
return $this->isEnabled;
98+
}
99+
100+
/**
101+
* Get form keys that are configured to ReCaptcha V3
102+
*
103+
* @return array
104+
* @throws InputException
105+
*/
106+
private function getEnumFormTypes(): array
107+
{
108+
$forms = [];
109+
if (empty($this->forms)) {
110+
foreach ($this->reCaptchaV3Config->getFormTypes() as $formType) {
111+
if ($this->captchaTypeResolver->getCaptchaTypeFor($formType) === self::RECAPTCHA_TYPE) {
112+
$forms[] = $formType;
113+
}
114+
}
115+
}
116+
return array_map('strtoupper', $forms);
117+
}
118+
119+
/**
120+
* Get configured message sent in case of failure
121+
*
122+
* @return string
123+
*/
124+
public function getFailureMessage(): string
125+
{
126+
if (!$this->failureMessage) {
127+
$this->failureMessage = $this->errorMessageConfig->getValidationFailureMessage();
128+
}
129+
return $this->failureMessage;
130+
}
131+
}

0 commit comments

Comments
 (0)