Skip to content

Commit 721762a

Browse files
authored
LYNX-363: Implement getting configurations for reCAPTCHA in GraphQL
LYNX-363: Implement getting configurations for reCAPTCHA in GraphQL
2 parents 937cd5a + 6619280 commit 721762a

File tree

13 files changed

+681
-66
lines changed

13 files changed

+681
-66
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\ReCaptchaVersion2Checkbox\Model;
18+
19+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
20+
use Magento\ReCaptchaVersion2Checkbox\Model\Frontend\UiConfigProvider;
21+
use Magento\ReCaptchaWebapiGraphQl\Model\Adapter\ReCaptchaConfigInterface;
22+
23+
class Config implements ReCaptchaConfigInterface, ResetAfterRequestInterface
24+
{
25+
/**
26+
* @var array
27+
*/
28+
private array $uiConfig = [];
29+
30+
/**
31+
* @param UiConfigProvider $uiConfigProvider
32+
*/
33+
public function __construct(
34+
private readonly UiConfigProvider $uiConfigProvider,
35+
) {
36+
}
37+
38+
/**
39+
* Get front-end's UI configurations
40+
*
41+
* @return array
42+
*/
43+
private function getUiConfig(): array
44+
{
45+
if (empty($this->uiConfig)) {
46+
$this->uiConfig = $this->uiConfigProvider->get() ?? [];
47+
}
48+
return $this->uiConfig;
49+
}
50+
51+
/**
52+
* Get website's Google API public key
53+
*
54+
* @return string
55+
*/
56+
public function getWebsiteKey(): string
57+
{
58+
return $this->getUiConfig()['rendering']['sitekey'] ?? '';
59+
}
60+
61+
/**
62+
* Get configured captcha's theme
63+
*
64+
* @return string
65+
*/
66+
public function getTheme(): string
67+
{
68+
return $this->getUiConfig()['rendering']['theme'] ?? '';
69+
}
70+
71+
/**
72+
* Get code of language to send notifications
73+
*
74+
* @return string
75+
*/
76+
public function getLanguageCode(): string
77+
{
78+
return $this->getUiConfig()['rendering']['hl'] ?? '';
79+
}
80+
81+
/**
82+
* "I am not a robot" captcha does not provide configurable minimum score setting
83+
*
84+
* @return null|float
85+
*/
86+
public function getMinimumScore(): ?float
87+
{
88+
return null;
89+
}
90+
91+
/**
92+
* ReCaptcha V2 does not provide configurable badge_position setting
93+
*
94+
* @return string
95+
*/
96+
public function getBadgePosition(): string
97+
{
98+
return '';
99+
}
100+
101+
/**
102+
* @inheritDoc
103+
*/
104+
public function _resetState(): void
105+
{
106+
$this->uiConfig = [];
107+
}
108+
}

ReCaptchaVersion2Checkbox/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"magento/framework": "*",
77
"magento/module-store": "*",
88
"magento/module-re-captcha-ui": "*",
9-
"magento/module-re-captcha-validation-api": "*"
9+
"magento/module-re-captcha-validation-api": "*",
10+
"magento/module-re-captcha-webapi-graph-ql": "*"
1011
},
1112
"suggest": {
1213
"magento/module-config": "*",
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\ReCaptchaVersion2Invisible\Model;
18+
19+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
20+
use Magento\ReCaptchaVersion2Invisible\Model\Frontend\UiConfigProvider;
21+
use Magento\ReCaptchaWebapiGraphQl\Model\Adapter\ReCaptchaConfigInterface;
22+
23+
class Config implements ReCaptchaConfigInterface, ResetAfterRequestInterface
24+
{
25+
/**
26+
* @var array
27+
*/
28+
private array $uiConfig = [];
29+
30+
/**
31+
* @param UiConfigProvider $uiConfigProvider
32+
*/
33+
public function __construct(
34+
private readonly UiConfigProvider $uiConfigProvider,
35+
) {
36+
}
37+
38+
/**
39+
* Get website's Google API public key
40+
*
41+
* @return string
42+
*/
43+
public function getWebsiteKey(): string
44+
{
45+
return $this->getUiConfig()['rendering']['sitekey'] ?? '';
46+
}
47+
48+
/**
49+
* ReCaptcha v2 Invisible does not provide configuration for minimum score
50+
*
51+
* @return null|float
52+
*/
53+
public function getMinimumScore(): ?float
54+
{
55+
return null;
56+
}
57+
58+
/**
59+
* Get configured captcha's badge position
60+
*
61+
* @return string
62+
*/
63+
public function getBadgePosition(): string
64+
{
65+
return $this->getUiConfig()['rendering']['badge'] ?? '';
66+
}
67+
68+
/**
69+
* Get configured captcha's theme
70+
*
71+
* @return string
72+
*/
73+
public function getTheme(): string
74+
{
75+
return $this->getUiConfig()['rendering']['theme'] ?? '';
76+
}
77+
78+
/**
79+
* Get code of language to send notifications
80+
*
81+
* @return string
82+
*/
83+
public function getLanguageCode(): string
84+
{
85+
return $this->getUiConfig()['rendering']['hl'] ?? '';
86+
}
87+
88+
/**
89+
* Get front-end's UI configurations
90+
*
91+
* @return array
92+
*/
93+
private function getUiConfig(): array
94+
{
95+
if (empty($this->uiConfig)) {
96+
$this->uiConfig = $this->uiConfigProvider->get() ?? [];
97+
}
98+
return $this->uiConfig;
99+
}
100+
101+
/**
102+
* @inheritDoc
103+
*/
104+
public function _resetState(): void
105+
{
106+
$this->uiConfig = [];
107+
}
108+
}

ReCaptchaVersion2Invisible/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"magento/framework": "*",
77
"magento/module-store": "*",
88
"magento/module-re-captcha-ui": "*",
9-
"magento/module-re-captcha-validation-api": "*"
9+
"magento/module-re-captcha-validation-api": "*",
10+
"magento/module-re-captcha-webapi-graph-ql": "*"
1011
},
1112
"suggest": {
1213
"magento/module-config": "*",
Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
@@ -12,67 +11,35 @@
1211
use Magento\ReCaptchaValidationApi\Api\Data\ValidationConfigInterface;
1312
use Magento\ReCaptchaVersion3Invisible\Model\Frontend\UiConfigProvider;
1413
use Magento\ReCaptchaVersion3Invisible\Model\Frontend\ValidationConfigProvider;
14+
use Magento\ReCaptchaWebapiGraphQl\Model\Adapter\ReCaptchaConfigInterface;
1515

16-
class Config implements ResetAfterRequestInterface
16+
class Config implements ReCaptchaConfigInterface, ResetAfterRequestInterface
1717
{
18-
/**
19-
* @var string|null
20-
*/
21-
private ?string $websiteKey = null;
22-
2318
/**
2419
* @var float|null
2520
*/
2621
private ?float $minimumScore = null;
2722

28-
/**
29-
* @var string|null
30-
*/
31-
private ?string $badgePosition = null;
32-
33-
/**
34-
* @var string|null
35-
*/
36-
private ?string $languageCode = null;
37-
38-
/**
39-
* @var UiConfigProvider
40-
*/
41-
private UiConfigProvider $uiConfigProvider;
42-
4323
/**
4424
* @var array
4525
*/
4626
private array $uiConfig = [];
4727

48-
/**
49-
* @var ValidationConfigProvider
50-
*/
51-
private ValidationConfigProvider $validationConfigProvider;
52-
5328
/**
5429
* @var ValidationConfigInterface|null
5530
*/
5631
private ?ValidationConfigInterface $validationConfig = null;
5732

58-
/**
59-
* @var array
60-
*/
61-
private array $formTypes;
62-
6333
/**
6434
* @param UiConfigProvider $uiConfigProvider
6535
* @param ValidationConfigProvider $validationConfigProvider
6636
* @param array $formTypes
6737
*/
6838
public function __construct(
69-
UiConfigProvider $uiConfigProvider,
70-
ValidationConfigProvider $validationConfigProvider,
71-
array $formTypes = []
39+
private readonly UiConfigProvider $uiConfigProvider,
40+
private readonly ValidationConfigProvider $validationConfigProvider,
41+
private readonly array $formTypes
7242
) {
73-
$this->formTypes = $formTypes;
74-
$this->uiConfigProvider = $uiConfigProvider;
75-
$this->validationConfigProvider = $validationConfigProvider;
7643
}
7744

7845
/**
@@ -82,21 +49,22 @@ public function __construct(
8249
*/
8350
public function getWebsiteKey(): string
8451
{
85-
if (!$this->websiteKey) {
86-
$this->websiteKey = $this->getUiConfig()['rendering']['sitekey'];
87-
}
88-
return $this->websiteKey;
52+
return $this->getUiConfig()['rendering']['sitekey'] ?? '';
8953
}
9054

9155
/**
9256
* Get configured minimum score value
9357
*
94-
* @return float
58+
* @return float|null
9559
*/
96-
public function getMinimumScore(): float
60+
public function getMinimumScore(): ?float
9761
{
9862
if (!$this->minimumScore) {
99-
$this->minimumScore = $this->validationConfig->getExtensionAttributes()->getScoreThreshold();
63+
$validationProvider = $this->validationConfigProvider->get();
64+
if ($validationProvider->getExtensionAttributes() === null) {
65+
return $this->minimumScore;
66+
}
67+
$this->minimumScore = $validationProvider->getExtensionAttributes()->getScoreThreshold();
10068
}
10169
return $this->minimumScore;
10270
}
@@ -108,10 +76,7 @@ public function getMinimumScore(): float
10876
*/
10977
public function getBadgePosition(): string
11078
{
111-
if (!$this->badgePosition) {
112-
$this->badgePosition = $this->getUiConfig()['rendering']['badge'];
113-
}
114-
return $this->badgePosition;
79+
return $this->getUiConfig()['rendering']['badge'] ?? '';
11580
}
11681

11782
/**
@@ -121,10 +86,17 @@ public function getBadgePosition(): string
12186
*/
12287
public function getLanguageCode(): string
12388
{
124-
if (!$this->languageCode) {
125-
$this->languageCode = $this->getUiConfig()['rendering']['hl'];
126-
}
127-
return $this->languageCode;
89+
return $this->getUiConfig()['rendering']['hl'] ?? '';
90+
}
91+
92+
/**
93+
* Get configured captcha's theme
94+
*
95+
* @return string
96+
*/
97+
public function getTheme(): string
98+
{
99+
return $this->getUiConfig()['rendering']['theme'] ?? '';
128100
}
129101

130102
/**
@@ -158,7 +130,7 @@ public function getValidationConfig(): ValidationConfigInterface
158130
private function getUiConfig(): array
159131
{
160132
if (empty($this->uiConfig)) {
161-
$this->uiConfig = $this->uiConfigProvider->get();
133+
$this->uiConfig = $this->uiConfigProvider->get() ?? [];
162134
}
163135
return $this->uiConfig;
164136
}
@@ -168,8 +140,8 @@ private function getUiConfig(): array
168140
*/
169141
public function _resetState(): void
170142
{
171-
$this->websiteKey = null;
172143
$this->uiConfig = [];
144+
$this->minimumScore = null;
173145
$this->validationConfig = null;
174146
}
175147
}

0 commit comments

Comments
 (0)