Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement option to set custom endpoints #29

Merged
merged 2 commits into from
Nov 29, 2023
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
7 changes: 2 additions & 5 deletions Block/Frontend/FriendlyCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ public function getSiteKey(): string
return $this->config->getSiteKey();
}

/**
* @return bool
*/
public function useEuEndpoint(): bool
public function getPuzzleEndpoint(): string
{
return $this->config->useEuEndpoint();
return $this->config->getPuzzleEndpoint();
}

/**
Expand Down
48 changes: 41 additions & 7 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace IMI\FriendlyCaptcha\Model;

use IMI\FriendlyCaptcha\Model\Config\Source\Endpoint;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Phrase;
use Magento\Store\Model\ScopeInterface;
Expand All @@ -18,7 +19,7 @@ class Config

public const CONFIG_PATH_APIEKEY = 'imi_friendly_captcha/general/apikey';

public const CONFIG_PATH_EU_ENDPOINT = 'imi_friendly_captcha/general/eu_endpoint';
public const CONFIG_PATH_ENDPOINT = 'imi_friendly_captcha/general/endpoint';

public const CONFIG_PATH_ENABLED_FRONTEND = 'imi_friendly_captcha/frontend/enabled';

Expand All @@ -36,6 +37,10 @@ class Config

public const CONFIG_PATH_ENABLED_FRONTEND_SENDFRIEND = 'imi_friendly_captcha/frontend/enabled_sendfriend';

protected const CONFIG_PATH_CUSTOM_PUZZLE = 'imi_friendly_captcha/general/custom_puzzle';

protected const CONFIG_PATH_CUSTOM_VERIFY = 'imi_friendly_captcha/general/custom_verify';

/**
* @var ScopeConfigInterface
*/
Expand Down Expand Up @@ -116,14 +121,43 @@ public function getApikey(): string
}

/**
* Get Friendly Captcha endpoint
*
* @return bool
* Get Friendly Captcha puzzle endpoint
*/
public function useEuEndpoint(): bool
public function getPuzzleEndpoint(): string
{
return (bool)$this->scopeConfig->getValue(static::CONFIG_PATH_EU_ENDPOINT,
ScopeInterface::SCOPE_WEBSITE);
$endpoint = (int)$this->scopeConfig->getValue(static::CONFIG_PATH_ENDPOINT, ScopeInterface::SCOPE_WEBSITE);

switch ($endpoint) {
case Endpoint::EU:
return 'https://eu-api.friendlycaptcha.eu/api/v1/puzzle';
case Endpoint::CUSTOM:
return (string)$this->scopeConfig->getValue(
static::CONFIG_PATH_CUSTOM_PUZZLE,
ScopeInterface::SCOPE_WEBSITE
);
default:
return '';
}
}

/**
* Get Friendly Captcha verify endpoint
*/
public function getVerifyEndpoint(): string
{
$endpoint = (int)$this->scopeConfig->getValue(static::CONFIG_PATH_ENDPOINT, ScopeInterface::SCOPE_WEBSITE);

switch ($endpoint) {
case Endpoint::EU:
return 'https://eu-api.friendlycaptcha.eu/api/v1/puzzle';
case Endpoint::CUSTOM:
return (string)$this->scopeConfig->getValue(
static::CONFIG_PATH_CUSTOM_VERIFY,
ScopeInterface::SCOPE_WEBSITE
);
default:
return 'https://api.friendlycaptcha.com/api/v1/siteverify';
}
}

/**
Expand Down
39 changes: 39 additions & 0 deletions Model/Config/Source/Endpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace IMI\FriendlyCaptcha\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;
use Magento\Framework\Phrase;

class Endpoint implements ArrayInterface
{
public const DEFAULT = 0;

public const EU = 1;

public const CUSTOM = 2;

/**
* @return array<array{value: int, label: Phrase}>
*/
public function toOptionArray()
{
return [
['value' => self::DEFAULT, 'label' => __('Default')],
['value' => self::EU, 'label' => __('EU Endpoint')],
['value' => self::CUSTOM, 'label' => __('Custom Endpoint')],
];
}

/**
* @return array<int, Phrase>
*/
public function toArray()
{
return [
self::DEFAULT => __('Default'),
self::EU => __('EU Endpoint'),
self::CUSTOM => __('Custom Endpoint'),
];
}
}
6 changes: 3 additions & 3 deletions Model/Provider/Failure/AjaxResponseFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace IMI\FriendlyCaptcha\Model\Provider\Failure;

use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Json\EncoderInterface;
use IMI\FriendlyCaptcha\Model\Config;
Expand Down Expand Up @@ -48,10 +48,10 @@ public function __construct(

/**
* Handle friendlyCaptcha failure
* @param ResponseInterface $response
* @param Http $response
* @return void
*/
public function execute(ResponseInterface $response = null)
public function execute(Http $response)
{
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);

Expand Down
6 changes: 3 additions & 3 deletions Model/Provider/Failure/AuthenticationExceptionFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace IMI\FriendlyCaptcha\Model\Provider\Failure;

use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\Exception\Plugin\AuthenticationException;
use IMI\FriendlyCaptcha\Model\Config;
use IMI\FriendlyCaptcha\Model\Provider\FailureProviderInterface;
Expand All @@ -30,12 +30,12 @@ public function __construct(

/**
* Handle friendlyCaptcha failure
* @param ResponseInterface $response
* @param Http $response
* @return void
* @throws AuthenticationException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(ResponseInterface $response = null)
public function execute(Http $response)
{
throw new AuthenticationException($this->config->getErrorDescription());
}
Expand Down
6 changes: 3 additions & 3 deletions Model/Provider/Failure/ObserverRedirectFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace IMI\FriendlyCaptcha\Model\Provider\Failure;

use Magento\Framework\App\ActionFlag;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
use Magento\Framework\App\Action\Action;
use Magento\Framework\UrlInterface;
Expand Down Expand Up @@ -74,10 +74,10 @@ private function getUrl()

/**
* Handle friendlyCaptcha failure
* @param ResponseInterface $response
* @param Http $response
* @return void
*/
public function execute(ResponseInterface $response = null)
public function execute(Http $response)
{
$this->messageManager->addErrorMessage($this->config->getErrorDescription());
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
Expand Down
5 changes: 3 additions & 2 deletions Model/Provider/FailureProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

namespace IMI\FriendlyCaptcha\Model\Provider;

use Magento\Framework\App\Response\Http;
use Magento\Framework\App\ResponseInterface;

interface FailureProviderInterface
{
/**
* Handle friendlyCaptcha failure
*
* @param ResponseInterface|null $response
* @param Http $response
*
* @return void
*/
public function execute(ResponseInterface $response = null);
public function execute(Http $response);
}
6 changes: 1 addition & 5 deletions Model/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ public function validate(string $friendlyCaptchaSolution): bool

private function getSiteVerifyUrl(): string
{
if ($this->config->useEuEndpoint()) {
return 'https://eu-api.friendlycaptcha.eu/api/v1/siteverify';
}

return 'https://api.friendlycaptcha.com/api/v1/siteverify';
return $this->config->getVerifyEndpoint();
}

private function shouldUseResponse(Curl $curl, $response): bool
Expand Down
13 changes: 11 additions & 2 deletions Observer/FriendlyCaptchaObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

namespace IMI\FriendlyCaptcha\Observer;

use Magento\Framework\App\Action\AbstractAction;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Response\Http as HttpResponse;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use IMI\FriendlyCaptcha\Api\ValidateInterface;
Expand Down Expand Up @@ -37,6 +39,11 @@ class FriendlyCaptchaObserver implements ObserverInterface
*/
private $isCheckRequired;

/**
* @var HttpResponse
*/
private $response;

/**
* @param SolutionProviderInterface $responseProvider
* @param ValidateInterface $validate
Expand All @@ -47,12 +54,14 @@ public function __construct(
SolutionProviderInterface $responseProvider,
ValidateInterface $validate,
FailureProviderInterface $failureProvider,
IsCheckRequiredInterface $isCheckRequired
IsCheckRequiredInterface $isCheckRequired,
HttpResponse $response
) {
$this->responseProvider = $responseProvider;
$this->validate = $validate;
$this->failureProvider = $failureProvider;
$this->isCheckRequired = $isCheckRequired;
$this->response = $response;
}

/**
Expand All @@ -68,7 +77,7 @@ public function execute(Observer $observer): void
if (!$this->validate->validate($friendlyCaptchaResponse)) {
/** @var Action $controller */
$controller = $observer->getControllerAction();
$this->failureProvider->execute($controller ? $controller->getResponse() : null);
$this->failureProvider->execute($controller instanceof AbstractAction ? $controller->getResponse() : $this->response);
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions Setup/Patch/Data/MigrateEuEndpointConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace IMI\FriendlyCaptcha\Setup\Patch\Data;

use Magento\Config\Model\ResourceModel\Config\Data as ConfigResource;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory as ConfigCollectionFactory;

use Magento\Framework\App\Config\Value;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class MigrateEuEndpointConfig implements DataPatchInterface
{
public function __construct(
private readonly ConfigResource $configResource,
private readonly ConfigCollectionFactory $configCollectionFactory
) {
}


/**
* @inheritDoc
*/
public static function getDependencies()
{
return [];
}

/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}

/**
* @inheritDoc
*/
public function apply()
{
$collection = $this->configCollectionFactory->create();
$collection->addFieldToFilter('path', ['eq' => 'imi_friendly_captcha/general/eu_endpoint']);
$collection->getItems();

/** @var Value $config */
foreach ($collection as $config) {
$config->setPath('imi_friendly_captcha/general/endpoint');
$this->configResource->save($config);
}
}
}
22 changes: 19 additions & 3 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,25 @@
<label>Friendly Captcha API Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="eu_endpoint" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<label>Use EU API Endpoint</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<field id="endpoint" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
<label>API Endpoint</label>
<source_model>IMI\FriendlyCaptcha\Model\Config\Source\Endpoint</source_model>
</field>
<field id="custom_puzzle" translate="label" type="text" sortOrder="31" showInDefault="1"
showInWebsite="1" showInStore="0" canRestore="1">
<label>Custom Puzzle Endpoint URL</label>
<depends>
<field id="endpoint">2</field>
</depends>
<validate>required-entry validate-url validate-no-html-tags</validate>
</field>
<field id="custom_verify" translate="label" type="text" sortOrder="32" showInDefault="1"
showInWebsite="1" showInStore="0" canRestore="1">
<label>Custom Verify Endpoint URL</label>
<depends>
<field id="endpoint">2</field>
</depends>
<validate>required-entry validate-url validate-no-html-tags</validate>
</field>
</group>
<group id="frontend" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1"
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<general>
<sitekey/>
<apikey backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
<eu_endpoint>0</eu_endpoint>
<endpoint>0</endpoint>
</general>
<frontend>
<enabled>0</enabled>
Expand Down
4 changes: 3 additions & 1 deletion view/frontend/templates/imi_friendly_captcha.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
*/

/** @var $block IMI\FriendlyCaptcha\Block\Frontend\FriendlyCaptcha */
/** @var $escaper \Magento\Framework\Escaper */
$endpoint = $block->getPuzzleEndpoint();
?>
<div id="<?= $block->getWidgetId() ?>"
class="frc-captcha"
data-sitekey="<?= $block->getSiteKey() ?>"
<?php if ($block->useEuEndpoint()): ?>data-puzzle-endpoint="https://eu-api.friendlycaptcha.eu/api/v1/puzzle"<?php endif; ?>
<?php if ($endpoint !== ''): ?>data-puzzle-endpoint="<?= $escaper->escapeUrl($endpoint) ?>"<?php endif; ?>
data-lang="<?= $block->getLang() ?>"
data-callback="captchaSolved_<?= $block->getWidgetId() ?>"
></div>
Expand Down