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
48 changes: 48 additions & 0 deletions Classes/Controller/FrontendUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
use TYPO3\CMS\Core\Crypto\Random;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Http\ForwardResponse;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
Expand Down Expand Up @@ -43,6 +44,16 @@ public function newAction(?FrontendUser $newFrontendUser = null): ResponseInterf

public function createAction(FrontendUser $newFrontendUser): ResponseInterface
{
if ($this->settings['captcha']['sitekey'] !== '' && $this->settings['captcha']['secretkey'] !== '') {
if ($this->verifyCaptcha() === false) {
$this->addFlashMessage(
LocalizationUtility::translate('error.captcha', 'newsletterregistration'),
'',
ContextualFeedbackSeverity::ERROR
);
return new ForwardResponse('new');
}
}
/** @var FrontendUser $existingFrontendUser */
$existingFrontendUser = $this->frontendUserRepository->findOneByEmailAndStoragePageId(
$newFrontendUser->getEmail(),
Expand Down Expand Up @@ -292,6 +303,43 @@ protected function getRandomPassword(): string
return $hashInstance->getHashedPassword($randomPassword);
}

protected function verifyCaptcha(): bool
{
$sitekey = $this->settings['captcha']['sitekey'] ?? '';
$secretkey = $this->settings['captcha']['secretkey'] ?? '';

if (empty($secretkey)) {
return true;
}

$parsedBody = $this->request->getParsedBody();
$parsedBody = is_array($parsedBody) ? $parsedBody : [];
$solution = $parsedBody['frc-captcha-solution'] ?? '';
if (empty($solution) || $solution === '.UNSTARTED' || $solution === '.FETCHING') {
return false;
}

try {
$requestFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\RequestFactory::class);
$response = $requestFactory->request(
'https://eu-api.friendlycaptcha.eu/api/v1/siteverify',
'POST',
[
'headers' => ['Content-Type' => 'application/json; charset=UTF-8'],
'body' => json_encode([
'solution' => $solution,
'secret' => $secretkey,
'sitekey' => $sitekey,
]),
]
);
$data = json_decode((string)$response->getBody(), true);
return ($data['success'] ?? false) === true;
} catch (\Throwable $e) {
return false;
}
}

protected function getHashedPassword(string $password): string
{
$hashInstance = GeneralUtility::makeInstance(PasswordHashFactory::class)->getDefaultHashInstance('FE');
Expand Down
4 changes: 4 additions & 0 deletions Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ plugin.tx_newsletterregistration {
skipDefaultArguments = 1
}
settings {
captcha {
sitekey =
secretkey =
}
sender {
name = My organization
email = no_reply@example.org
Expand Down
4 changes: 4 additions & 0 deletions Resources/Private/Language/de.locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@
<source>Organisation</source>
<target>Organisation</target>
</trans-unit>
<trans-unit id="error.captcha">
<source>Captcha verification failed. Please try again.</source>
<target>Captcha-Überprüfung fehlgeschlagen. Bitte versuchen Sie es erneut.</target>
</trans-unit>
</body>
</file>
</xliff>
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<trans-unit id="labels.company">
<source>Organisation</source>
</trans-unit>
<trans-unit id="error.captcha">
<source>Captcha verification failed. Please try again.</source>
</trans-unit>
</body>
</file>
</xliff>
26 changes: 26 additions & 0 deletions Resources/Private/Templates/FrontendUser/New.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@
</label>
<f:form.textfield id="email" class="form-control" property="email" required="required" />
</div>
<f:if condition="{settings.captcha.sitekey} !== '' && {settings.captcha.secretkey} !== ''">
<f:asset.script identifier="friendly-challenge-widget-module"
src="https://cdn.jsdelivr.net/npm/friendly-challenge@0.9.8/widget.module.min.js"
type="module"
integrity="sha384-qT4bq9FARVVEQZ+u0J/IEGVVCM/d/aU6p2anikjcUPeSd9n5QP8xJ5wxyf3kvrUU"
crossorigin="anonymous"
async="true"
defer="true"
useNonce="true"
/>
<f:asset.script identifier="friendly-challenge-widget"
src="https://cdn.jsdelivr.net/npm/friendly-challenge@0.9.8/widget.min.js"
nomodule="true"
integrity="sha384-tBCxcMwFwX1Y+iL38Da2IZLdhA3HPO3ztLc4sD5ZYK4CgH4mTb0fRPwOw/4GEb12"
crossorigin="anonymous"
async="true"
defer="true"
useNonce="true"
/>
<div class="form-group">
<div class="frc-captcha"
data-puzzle-endpoint="https://eu-api.friendlycaptcha.eu/api/v1/puzzle"
data-sitekey="{settings.captcha.sitekey}">
</div>
</div>
</f:if>
<div class="form-group">
<f:form.submit value="{f:translate(key:'newFrontendUser.submit')}" />
</div>
Expand Down
Loading