Skip to content

Commit

Permalink
[FEATURE][WIP] Friendlycaptcha V2
Browse files Browse the repository at this point in the history
  • Loading branch information
80Quattro committed Oct 17, 2024
1 parent 6f4be6f commit a07834d
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 39 deletions.
12 changes: 6 additions & 6 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

class Configuration
{
public const DEFAULT_JS_PATH = 'EXT:friendlycaptcha_official/Resources/Public/JavaScript/lib/widget-0.9.12.min.js';
public const DEFAULT_JS_PATH = 'EXT:friendlycaptcha_official/Resources/Public/JavaScript/lib/[email protected].min.js';

protected string $siteKey = '';
protected string $siteSecretKey = '';
protected string $puzzleUrl = '';
protected string $puzzleEndpoint = '';
protected string $verifyUrl = '';
protected string $jsPath = '';
protected bool $skipDevValidation = false;
Expand All @@ -31,15 +31,15 @@ public function __construct(Site $site = null)
$siteConfiguration = $site->getConfiguration();
$this->siteKey = trim($siteConfiguration['friendlycaptcha_site_key'] ?? '');
$this->siteSecretKey = trim($siteConfiguration['friendlycaptcha_secret_key'] ?? '');
$this->puzzleUrl = trim($siteConfiguration['friendlycaptcha_puzzle_url'] ?? '');
$this->puzzleEndpoint = trim($siteConfiguration['friendlycaptcha_puzzle_endpoint'] ?? '');
$this->verifyUrl = trim($siteConfiguration['friendlycaptcha_verify_url'] ?? '');
$this->jsPath = trim($siteConfiguration['friendlycaptcha_js_path'] ?? '');
$this->skipDevValidation = (bool)($siteConfiguration['friendlycaptcha_skip_dev_validation'] ?? false);
}

public function isEnabled(): bool
{
return $this->siteKey !== '' && $this->siteSecretKey !== '' && $this->puzzleUrl !== '' && $this->verifyUrl !== '' && !$this->hasSkipHeaderValidation();
return $this->siteKey !== '' && $this->siteSecretKey !== '' && $this->verifyUrl !== '' && !$this->hasSkipHeaderValidation();
}

public function getSiteKey(): string
Expand All @@ -52,9 +52,9 @@ public function getSiteSecretKey(): string
return $this->siteSecretKey;
}

public function getPuzzleUrl(): string
public function getPuzzleEndpoint(): string
{
return $this->puzzleUrl;
return $this->puzzleEndpoint;
}

public function getVerifyUrl(): string
Expand Down
18 changes: 10 additions & 8 deletions Classes/Service/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
$this->configuration = new Configuration();
}

public function verify(string $solution = ''): bool
public function verify(string $response = ''): bool
{
if ($this->configuration->hasSkipDevValidation()) {
return true;
Expand All @@ -39,17 +39,19 @@ public function verify(string $solution = ''): bool
return true;
}

$solution = $solution ?: $this->getSolutionFromRequest();
if (!$solution || !$this->configuration->isEnabled()) {
$response = $response ?: $this->getResponseFromRequest();
if (!$response || !$this->configuration->isEnabled()) {
return false;
}

$options = [
'headers' => ['Cache-Control' => 'no-cache'],
'headers' => [
'Cache-Control' => 'no-cache',
'X-API-Key' => $this->configuration->getSiteSecretKey(),
],
'allow_redirects' => true,
'form_params' => [
'secret' => $this->configuration->getSiteSecretKey(),
'solution' => $solution,
'response' => $response,
],
];

Expand Down Expand Up @@ -86,13 +88,13 @@ protected function request(string $method, string $url, array $options = [])
return $result->getBody()->getContents();
}

protected function getSolutionFromRequest(): string
protected function getResponseFromRequest(): string
{
/** @var ServerRequest $request */
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
if (!$request) {
return '';
}
return $request->getParsedBody()['frc-captcha-solution'] ?? $request->getQueryParams()['frc-captcha-solution'] ?? '';
return $request->getParsedBody()['frc-captcha-response'] ?? $request->getQueryParams()['frc-captcha-response'] ?? '';
}
}
3 changes: 1 addition & 2 deletions Classes/ViewHelpers/ConfigurationViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
{
$configuration = new Configuration();
return [
'languageIsoCode' => self::getLanguageIsoCode(),
'siteKey' => $configuration->getSiteKey(),
'verifyUrl' => $configuration->getVerifyUrl(),
'puzzleUrl' => $configuration->getPuzzleUrl(),
'puzzleEndpoint' => $configuration->getPuzzleEndpoint(),
'jsPath' => $configuration->getJsPath(),
'enabled' => $configuration->isEnabled(),
];
Expand Down
14 changes: 7 additions & 7 deletions Configuration/SiteConfiguration/Overrides/sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ static function () {
'placeholder' => 'A16UE6NICBMVQKG1I1BFMLBD56K53D3CS0L5N00AJNBT1R41P8O7N1KFMH',
],
];
$GLOBALS['SiteConfiguration']['site']['columns']['friendlycaptcha_puzzle_url'] = [
'label' => $lll . 'site.configuration.puzzle_url',
$GLOBALS['SiteConfiguration']['site']['columns']['friendlycaptcha_puzzle_endpoint'] = [
'label' => $lll . 'site.configuration.puzzle_endpoint',
'config' => [
'type' => 'input',
'placeholder' => 'https://eu-api.friendlycaptcha.eu/api/v1/puzzle',
'default' => 'https://eu-api.friendlycaptcha.eu/api/v1/puzzle',
'placeholder' => 'eu',
'default' => '',
],
];
$GLOBALS['SiteConfiguration']['site']['columns']['friendlycaptcha_verify_url'] = [
'label' => $lll . 'site.configuration.verify_url',
'config' => [
'type' => 'input',
'placeholder' => 'https://api.friendlycaptcha.com/api/v1/siteverify',
'default' => 'https://eu-api.friendlycaptcha.eu/api/v1/siteverify',
'placeholder' => 'https://global.frcapi.com/api/v2/captcha/siteverify',
'default' => 'https://global.frcapi.com/api/v2/captcha/siteverify',
],
];
$GLOBALS['SiteConfiguration']['site']['columns']['friendlycaptcha_js_path'] = [
Expand All @@ -48,6 +48,6 @@ static function () {
'type' => 'check',
],
];
$GLOBALS['SiteConfiguration']['site']['types']['0']['showitem'] .= ',--div--;' . $lll . 'site.configuration.tab, friendlycaptcha_site_key,friendlycaptcha_secret_key,friendlycaptcha_puzzle_url,friendlycaptcha_verify_url,friendlycaptcha_js_path,friendlycaptcha_skip_dev_validation,';
$GLOBALS['SiteConfiguration']['site']['types']['0']['showitem'] .= ',--div--;' . $lll . 'site.configuration.tab, friendlycaptcha_site_key,friendlycaptcha_secret_key,friendlycaptcha_puzzle_endpoint,friendlycaptcha_verify_url,friendlycaptcha_js_path,friendlycaptcha_skip_dev_validation,';
}
);
2 changes: 1 addition & 1 deletion Configuration/Yaml/FormSetup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TYPO3:

# Headline (appears as a title in the overview and above the form)
formEditor:
label: 'Friendly Captcha'
label: 'Friendly Captcha V2'
group: custom
groupSorting: 1010

Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Form/Partials/Friendlycaptcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
value="1"
additionalAttributes="{formvh:translateElementProperty(element: element, property: 'fluidAdditionalAttributes')}"
/>
<div class="frc-captcha" data-sitekey="{captchaConfiguration.siteKey}" data-lang="{captchaConfiguration.languageIsoCode}" data-puzzle-endpoint="{captchaConfiguration.puzzleUrl}"></div>
<div class="frc-captcha" data-sitekey="{captchaConfiguration.siteKey}" {f:if(condition: '{captchaConfiguration.puzzleEndpoint}', then: 'data-api-endpoint="{captchaConfiguration.puzzleEndpoint}"')}></div>
</f:render>
</f:then>
<f:else>
Expand Down
4 changes: 2 additions & 2 deletions Resources/Private/Language/Configuration.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<trans-unit id="site.configuration.verify_url">
<source>Verify URL</source>
</trans-unit>
<trans-unit id="site.configuration.puzzle_url">
<source>Puzzle URL</source>
<trans-unit id="site.configuration.puzzle_endpoint">
<source>Puzzle Endpoint</source>
</trans-unit>
<trans-unit id="site.configuration.js_path">
<source>JavaScript Path</source>
Expand Down
7 changes: 7 additions & 0 deletions Resources/Public/JavaScript/lib/[email protected]

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Resources/Public/JavaScript/lib/widget-0.9.12.min.js

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function configurationIsBuiltCorrectly(): void
$configuration = new Configuration();
self::assertEquals('12345', $configuration->getSiteKey());
self::assertEquals('ABCDEF', $configuration->getSiteSecretKey());
self::assertEquals('https://puzzle', $configuration->getPuzzleUrl());
self::assertEquals('', $configuration->getPuzzleEndpoint());
self::assertEquals('https://verify,https://verify2', $configuration->getVerifyUrl());
self::assertEquals('https://verify', $configuration->getFirstVerifyUrl());
self::assertTrue($configuration->isEnabled());
Expand Down
12 changes: 6 additions & 6 deletions Tests/Unit/Service/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function verifyUrlIsCalledWithProperData(): void
{
self::setupRequest();
$GLOBALS['TYPO3_REQUEST'] = $GLOBALS['TYPO3_REQUEST']
->withParsedBody(['frc-captcha-solution' => '1234']);
->withParsedBody(['frc-captcha-response' => '1234']);
$historyContainer = [];
$client = $this->createClientWithHistory(
[new Response(200, [], '{"success": true}')],
Expand All @@ -52,15 +52,15 @@ public function solutionIsRetrieved(): void
self::setupRequest();
$mockedApi = $this->getAccessibleMock(Api::class, null, [], '', false);

self::assertSame('', $mockedApi->_call('getSolutionFromRequest'));
self::assertSame('', $mockedApi->_call('getResponseFromRequest'));

$GLOBALS['TYPO3_REQUEST'] = $GLOBALS['TYPO3_REQUEST']
->withQueryParams(['frc-captcha-solution' => '12345']);
self::assertSame('12345', $mockedApi->_call('getSolutionFromRequest'));
->withQueryParams(['frc-captcha-response' => '12345']);
self::assertSame('12345', $mockedApi->_call('getResponseFromRequest'));

$GLOBALS['TYPO3_REQUEST'] = $GLOBALS['TYPO3_REQUEST']
->withParsedBody(['frc-captcha-solution' => '1234']);
self::assertSame('1234', $mockedApi->_call('getSolutionFromRequest'));
->withParsedBody(['frc-captcha-response' => '1234']);
self::assertSame('1234', $mockedApi->_call('getResponseFromRequest'));
}

private function createClientWithHistory(array $responses, array &$historyContainer): Client
Expand Down
5 changes: 2 additions & 3 deletions Tests/Unit/ViewHelpers/ConfigurationViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ public function viewHelperReturnsProperConfiguration()
public function __construct() {}
};
self::assertSame([
'languageIsoCode' => 'en',
'siteKey' => '1234',
'verifyUrl' => 'https://verify,https://verify2',
'puzzleUrl' => 'https://puzzle',
'jsPath' => 'EXT:friendlycaptcha_official/Resources/Public/JavaScript/lib/widget-0.9.12.min.js',
'puzzleEndpoint' => '',
'jsPath' => 'EXT:friendlycaptcha_official/Resources/Public/JavaScript/lib/[email protected].min.js',
'enabled' => true,
], ConfigurationViewHelper::renderStatic([], static fn() => '', $renderingContext));
}
Expand Down

0 comments on commit a07834d

Please sign in to comment.