Skip to content

Commit 585ab8f

Browse files
author
ogorkun
committed
MC-38984: Merge all ReCaptcha web API coverage stories
1 parent ad3ef68 commit 585ab8f

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

ReCaptchaWebapiGraphQl/Plugin/GraphQlValidator.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
use Magento\Framework\GraphQl\Config\ConfigElementInterface;
1212
use Magento\Framework\GraphQl\Config\Element\TypeInterface;
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
1415
use Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\Formatter\Fields;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1517
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
1618
use Magento\ReCaptchaWebapiApi\Api\WebapiValidationConfigProviderInterface;
1719
use Magento\ReCaptchaWebapiApi\Model\Data\EndpointFactory;
20+
use Magento\Framework\GraphQl\Config\Element\Field;
1821

1922
/**
2023
* Validate ReCaptcha for GraphQl mutations.
@@ -62,35 +65,39 @@ public function __construct(
6265
/**
6366
* Validate ReCaptcha for mutations if needed.
6467
*
65-
* @param Fields $subject
66-
* @param ConfigElementInterface $configElement
68+
* @param ResolverInterface $subject
69+
* @param Field $fieldInfo
70+
* @param mixed $context
71+
* @param ResolveInfo $resolveInfo
6772
* @throws GraphQlInputException
6873
* @return void
6974
*
7075
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7176
*/
72-
public function beforeFormat(
73-
Fields $subject,
74-
ConfigElementInterface $configElement
77+
public function beforeResolve(
78+
ResolverInterface $subject,
79+
Field $fieldInfo,
80+
$context,
81+
ResolveInfo $resolveInfo
7582
): void {
76-
if ($configElement->getName() === 'Mutation' && $configElement instanceof TypeInterface) {
77-
foreach ($configElement->getFields() as $field) {
78-
$reCaptchaConfig = $this->configProvider->getConfigFor(
79-
$this->endpointFactory->create([
80-
'class' => ltrim($field->getResolver(), '\\'),
81-
'method' => 'resolve',
82-
'name' => $field->getName()
83-
])
84-
);
85-
if ($reCaptchaConfig
86-
&& !$this->validator->isValid(
87-
(string)$this->request->getHeader('X-ReCaptcha'),
88-
$reCaptchaConfig
89-
)->isValid()
90-
) {
91-
throw new GraphQlInputException(__('ReCaptcha validation failed, please try again'));
92-
}
93-
}
83+
if ($resolveInfo->operation->operation !== 'mutation') {
84+
return;
85+
}
86+
87+
$reCaptchaConfig = $this->configProvider->getConfigFor(
88+
$this->endpointFactory->create([
89+
'class' => ltrim($fieldInfo->getResolver(), '\\'),
90+
'method' => 'resolve',
91+
'name' => $fieldInfo->getName()
92+
])
93+
);
94+
if ($reCaptchaConfig
95+
&& !$this->validator->isValid(
96+
(string)$this->request->getHeader('X-ReCaptcha'),
97+
$reCaptchaConfig
98+
)->isValid()
99+
) {
100+
throw new GraphQlInputException(__('ReCaptcha validation failed, please try again'));
94101
}
95102
}
96103
}

ReCaptchaWebapiGraphQl/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-->
88

99
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10-
<type name="Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\Formatter\Fields">
10+
<type name="Magento\Framework\GraphQl\Query\ResolverInterface">
1111
<plugin name="graphql_recaptcha_validation" type="Magento\ReCaptchaWebapiGraphQl\Plugin\GraphQlValidator" />
1212
</type>
1313
</config>

0 commit comments

Comments
 (0)