-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReCaptcha compatability with Magento 2.4.x
- Loading branch information
1 parent
a04ba1d
commit 4c2613e
Showing
12 changed files
with
279 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magefan (support@magefan.com). All rights reserved. | ||
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magefan\BlogCommentsReCaptcha\Model; | ||
|
||
use Magento\Framework\App\PlainTextRequestInterface; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class CaptchaResponseResolver implements CaptchaResponseResolverInterface | ||
{ | ||
/** | ||
* @var SerializerInterface | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @param SerializerInterface $serializer | ||
*/ | ||
public function __construct(SerializerInterface $serializer) | ||
{ | ||
$this->serializer = $serializer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param RequestInterface|PlainTextRequestInterface $request | ||
* @return string | ||
* @throws InputException | ||
*/ | ||
public function resolve(RequestInterface $request): string | ||
{ | ||
$content = $request->getContent(); | ||
|
||
if (empty($content)) { | ||
throw new InputException(__('Can not resolve reCAPTCHA response.')); | ||
} | ||
|
||
try { | ||
//$params = $this->serializer->unserialize($content); | ||
$params = []; | ||
parse_str($content, $params); | ||
} catch (\InvalidArgumentException $e) { | ||
throw new InputException(__('Can not resolve reCAPTCHA response.'), $e); | ||
} | ||
|
||
if (empty($params[self::PARAM_RECAPTCHA])) { | ||
throw new InputException(__('Can not resolve reCAPTCHA response.')); | ||
} | ||
|
||
return $params[self::PARAM_RECAPTCHA]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magefan (support@magefan.com). All rights reserved. | ||
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magefan\BlogCommentsReCaptcha\Model; | ||
|
||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\App\ActionFlag; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
|
||
/** | ||
* Process error during ajax login | ||
* | ||
* Set "no dispatch" flag and error message to Response | ||
*/ | ||
class ErrorProcessor | ||
{ | ||
/** | ||
* @var ActionFlag | ||
*/ | ||
private $actionFlag; | ||
|
||
/** | ||
* @var SerializerInterface | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @param ActionFlag $actionFlag | ||
* @param SerializerInterface $serializer | ||
*/ | ||
public function __construct( | ||
ActionFlag $actionFlag, | ||
SerializerInterface $serializer | ||
) { | ||
$this->actionFlag = $actionFlag; | ||
$this->serializer = $serializer; | ||
} | ||
|
||
/** | ||
* Set "no dispatch" flag and error message to Response | ||
* | ||
* @param ResponseInterface $response | ||
* @param string $message | ||
* @return void | ||
*/ | ||
public function processError(ResponseInterface $response, string $message): void | ||
{ | ||
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); | ||
|
||
$jsonPayload = $this->serializer->serialize( | ||
[ | ||
'success' => false, | ||
'message' => $message, | ||
] | ||
); | ||
$response->representJson($jsonPayload); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* Copyright © Magefan (support@magefan.com). All rights reserved. | ||
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magefan\BlogCommentsReCaptcha\Observer; | ||
|
||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magefan\BlogCommentsReCaptcha\Model\ErrorProcessor; | ||
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; | ||
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; | ||
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface; | ||
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* ReviewFormObserver | ||
*/ | ||
class CommentFormObserver implements ObserverInterface | ||
{ | ||
/** | ||
* @var CaptchaResponseResolverInterface | ||
*/ | ||
private $captchaResponseResolver; | ||
|
||
/** | ||
* @var ValidationConfigResolverInterface | ||
*/ | ||
private $validationConfigResolver; | ||
|
||
/** | ||
* @var ValidatorInterface | ||
*/ | ||
private $captchaValidator; | ||
|
||
/** | ||
* @var IsCaptchaEnabledInterface | ||
*/ | ||
private $isCaptchaEnabled; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @var ErrorProcessor | ||
*/ | ||
private $errorProcessor; | ||
|
||
/** | ||
* @param CaptchaResponseResolverInterface $captchaResponseResolver | ||
* @param ValidationConfigResolverInterface $validationConfigResolver | ||
* @param ValidatorInterface $captchaValidator | ||
* @param IsCaptchaEnabledInterface $isCaptchaEnabled | ||
* @param LoggerInterface $logger | ||
* @param ErrorProcessor $errorProcessor | ||
*/ | ||
public function __construct( | ||
CaptchaResponseResolverInterface $captchaResponseResolver, | ||
ValidationConfigResolverInterface $validationConfigResolver, | ||
ValidatorInterface $captchaValidator, | ||
IsCaptchaEnabledInterface $isCaptchaEnabled, | ||
LoggerInterface $logger, | ||
ErrorProcessor $errorProcessor | ||
) { | ||
$this->captchaResponseResolver = $captchaResponseResolver; | ||
$this->validationConfigResolver = $validationConfigResolver; | ||
$this->captchaValidator = $captchaValidator; | ||
$this->isCaptchaEnabled = $isCaptchaEnabled; | ||
$this->logger = $logger; | ||
$this->errorProcessor = $errorProcessor; | ||
} | ||
|
||
/** | ||
* @param Observer $observer | ||
* @return void | ||
* @throws LocalizedException | ||
*/ | ||
public function execute(Observer $observer): void | ||
{ | ||
$key = 'mfblog_comment'; | ||
if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { | ||
$controller = $observer->getControllerAction(); | ||
$request = $controller->getRequest(); | ||
$response = $controller->getResponse(); | ||
|
||
$validationConfig = $this->validationConfigResolver->get($key); | ||
|
||
try { | ||
$reCaptchaResponse = $this->captchaResponseResolver->resolve($request); | ||
} catch (InputException $e) { | ||
var_dump($e->getMessage()); | ||
$this->logger->error($e); | ||
$this->errorProcessor->processError( | ||
$response, | ||
$validationConfig->getValidationFailureMessage() | ||
); | ||
return; | ||
} | ||
|
||
$validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig); | ||
if (false === $validationResult->isValid()) { | ||
$this->errorProcessor->processError( | ||
$response, | ||
$validationConfig->getValidationFailureMessage() | ||
); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.