Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions ReCaptcha/Block/Frontend/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ class ReCaptcha extends Template

/**
* @param Template\Context $context
* @param null $decoder @deprecated
* @param null $encoder @deprecated
* @param LayoutSettings $layoutSettings
* @param array $data
* @param Config|null $config
*/
public function __construct(
Template\Context $context,
$decoder,
$encoder,
LayoutSettings $layoutSettings,
array $data = [],
Config $config = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
*/
declare(strict_types=1);

namespace Magento\ReCaptcha\Model\Provider\Failure\RedirectUrl;
namespace Magento\ReCaptcha\Model;

use Magento\Customer\Model\Url;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\ReCaptcha\Model\Provider\Failure\RedirectUrlProviderInterface;

/**
* @inheritDoc
*/
class BeforeAuthUrlProvider implements RedirectUrlProviderInterface
class BeforeAuthUrlProvider
{
/**
* @var SessionManagerInterface
Expand Down
90 changes: 90 additions & 0 deletions ReCaptcha/Model/CaptchaRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ReCaptcha\Model;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\Area;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\HttpInterface;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;

/**
* Captcha request handler
*/
class CaptchaRequestHandler implements CaptchaRequestHandlerInterface
{
/**
* @var ValidateInterface
*/
private $validate;

/**
* @var RemoteAddress
*/
private $remoteAddress;

/**
* @var MessageManagerInterface
*/
private $messageManager;

/**
* @var ActionFlag
*/
private $actionFlag;

/**
* @var Config
*/
private $config;

/**
* @param ValidateInterface $validate
* @param RemoteAddress $remoteAddress
* @param MessageManagerInterface $messageManager
* @param ActionFlag $actionFlag
* @param Config $config
*/
public function __construct(
ValidateInterface $validate,
RemoteAddress $remoteAddress,
MessageManagerInterface $messageManager,
ActionFlag $actionFlag,
Config $config
) {
$this->validate = $validate;
$this->remoteAddress = $remoteAddress;
$this->messageManager = $messageManager;
$this->actionFlag = $actionFlag;
$this->config = $config;
}

/**
* @inheritdoc
*/
public function execute(
string $area,
RequestInterface $request,
HttpInterface $response,
string $redirectOnFailureUrl
): void {
$reCaptchaResponse = $request->getParam(ValidateInterface::PARAM_RECAPTCHA_RESPONSE);
$remoteIp = $this->remoteAddress->getRemoteAddress();
$options['threshold'] = ($area === Area::AREA_ADMINHTML)
? $this->config->getMinBackendScore() : $this->config->getMinFrontendScore();

if (false === $this->validate->validate($reCaptchaResponse, $remoteIp, $options)) {
$this->messageManager->addErrorMessage($this->config->getErrorDescription());
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);

$response->setRedirect($redirectOnFailureUrl);
}
}
}
37 changes: 37 additions & 0 deletions ReCaptcha/Model/CaptchaRequestHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ReCaptcha\Model;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\HttpInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* Captcha request handler interface (sugar service for avoiding boilerplate code)
*
* Validate captcha data in request and set message and redirect if validation was failed
*
* @api
*/
interface CaptchaRequestHandlerInterface
{
/**
* @param string $area
* @param RequestInterface $request
* @param HttpInterface $response
* @param string $redirectOnFailureUrl
* @return void
* @throws LocalizedException
*/
public function execute(
string $area,
RequestInterface $request,
HttpInterface $response,
string $redirectOnFailureUrl
): void;
}
41 changes: 20 additions & 21 deletions ReCaptcha/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

namespace Magento\ReCaptcha\Model;

use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Phrase;
use Magento\Store\Model\ScopeInterface;

/**
* Read configuration from store config
*/
class Config
class Config implements ConfigInterface
{
public const XML_PATH_ENABLED_BACKEND = 'recaptcha/backend/enabled';
public const XML_PATH_ENABLED_FRONTEND = 'recaptcha/frontend/enabled';
Expand All @@ -40,7 +41,6 @@ class Config
public const XML_PATH_ENABLED_FRONTEND_CREATE = 'recaptcha/frontend/enabled_create';
public const XML_PATH_ENABLED_FRONTEND_REVIEW = 'recaptcha/frontend/enabled_review';
public const XML_PATH_ENABLED_FRONTEND_NEWSLETTER = 'recaptcha/frontend/enabled_newsletter';
public const XML_PATH_ENABLED_FRONTEND_SENDFRIEND = 'recaptcha/frontend/enabled_sendfriend';

/**
* @var ScopeConfigInterface
Expand Down Expand Up @@ -92,7 +92,7 @@ public function getPrivateKey(): string
*/
public function isEnabledBackend(): bool
{
if (!$this->getPrivateKey() || !$this->getPublicKey()) {
if (!$this->isAreaEnabled(Area::AREA_ADMINHTML) || !$this->getPrivateKey() || !$this->getPublicKey()) {
return false;
}

Expand All @@ -105,7 +105,7 @@ public function isEnabledBackend(): bool
*/
public function isEnabledFrontend(): bool
{
if (!$this->getPrivateKey() || !$this->getPublicKey()) {
if (!$this->isAreaEnabled(Area::AREA_FRONTEND) || !$this->getPrivateKey() || !$this->getPublicKey()) {
return false;
}

Expand Down Expand Up @@ -167,7 +167,7 @@ public function isEnabledFrontendContact(): bool
* Return true if enabled on frontend create user
* @return bool
*/
public function isEnabledFrontendCreate(): bool
public function isEnabledFrontendCreateUser(): bool
{
if (!$this->isEnabledFrontend()) {
return false;
Expand Down Expand Up @@ -211,22 +211,6 @@ public function isEnabledFrontendNewsletter(): bool
);
}

/**
* Return true if enabled on frontend send to friend
* @return bool
*/
public function isEnabledFrontendSendFriend(): bool
{
if (!$this->isEnabledFrontend()) {
return false;
}

return (bool) $this->scopeConfig->getValue(
static::XML_PATH_ENABLED_FRONTEND_SENDFRIEND,
ScopeInterface::SCOPE_WEBSITE
);
}

/**
* @return bool
*/
Expand Down Expand Up @@ -359,4 +343,19 @@ public function getMinBackendScore(): float
static::XML_PATH_SIZE_MIN_SCORE_BACKEND
)));
}

/**
* Return true if area is configured to be active
* @param string $area
* @return bool
*/
public function isAreaEnabled(string $area): bool
{
if (!in_array($area, [Area::AREA_FRONTEND, Area::AREA_ADMINHTML], true)) {
throw new \InvalidArgumentException('Area parameter must be one of frontend or adminhtml');
}

return (($area === Area::AREA_ADMINHTML) && $this->isEnabledBackend())
|| (($area === Area::AREA_FRONTEND) && $this->isEnabledFrontend());
}
}
22 changes: 22 additions & 0 deletions ReCaptcha/Model/ConfigEnabledInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ReCaptcha\Model;

/**
* Extension point of the ReCaptcha configuration
*
* @api
*/
interface ConfigEnabledInterface
{
/**
* Return true if functionality of corresponding point is enabled in configuration
* @return bool
*/
public function isEnabled(): bool;
}
107 changes: 107 additions & 0 deletions ReCaptcha/Model/ConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ReCaptcha\Model;

use Magento\Framework\Phrase;

/**
* Represents general ReCaptcha configuration
*
* @api
*/
interface ConfigInterface
{
/**
* Get error
* @return Phrase
*/
public function getErrorDescription(): Phrase;

/**
* Get google recaptcha public key
* @return string
*/
public function getPublicKey(): string;

/**
* Get google recaptcha private key
* @return string
*/
public function getPrivateKey(): string;

/**
* Return true if enabled on backend
* @return bool
*/
public function isEnabledBackend(): bool;

/**
* Return true if enabled on frontend
* @return bool
*/
public function isEnabledFrontend(): bool;

/**
* @return bool
*/
public function isInvisibleRecaptcha(): bool;

/**
* Get data size
* @return string
*/
public function getFrontendSize(): string;

/**
* Get data size
* @return string
*/
public function getBackendSize(): string;

/**
* Get data size
* @return string
*/
public function getFrontendTheme(): ?string;

/**
* Get data size
* @return string
*/
public function getBackendTheme(): string;

/**
* Get data size
* @return string
*/
public function getFrontendPosition(): ?string;

/**
* Get reCaptcha type
* @return string
*/
public function getType(): string;

/**
* Get language code
* @return string
*/
public function getLanguageCode(): string;

/**
* Get minimum frontend score
* @return float
*/
public function getMinFrontendScore(): float;

/**
* Get minimum frontend score
* @return float
*/
public function getMinBackendScore(): float;
}
Loading