Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jobee committed Feb 20, 2023
0 parents commit 1ba52aa
Show file tree
Hide file tree
Showing 20 changed files with 450 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
71 changes: 71 additions & 0 deletions Classes/Validation/Validator/TurnstileValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace Tms\Cloudflare\Turnstile\Validation\Validator;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Client\Browser;
use Neos\Flow\Http\Client\CurlEngine;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Validation\Validator\AbstractValidator;
use Psr\Log\LoggerInterface;

/**
* Turnstile validator
*/
class TurnstileValidator extends AbstractValidator
{
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

/**
* @var boolean
*/
protected $acceptsEmptyValues = false;

/**
* @var array
*/
protected $supportedOptions = [
'endpoint' => ['', 'Turnstile API endpoint', 'string', true],
'secretKey' => ['', 'Secret key of your Turnstile site', 'string', true]
];

/**
* @param mixed $value The value that should be validated
*
* @return void
* @throws \Neos\Flow\Validation\Exception\InvalidValidationOptionsException
*/
protected function isValid($value)
{
if (!is_string($value) || empty($value)) {
$this->addError('We could not identify you as a human. Please try again.', 1676890456);
return;
}

$requestEngine = new CurlEngine();
$requestEngine->setOption(CURLOPT_TIMEOUT, 60);

$browser = new Browser();
$browser->setRequestEngine($requestEngine);

$arguments['secret'] = $this->getOptions()['secretKey'];
$arguments['response'] = $value;

try {
$response = $browser->request($this->getOptions()['endpoint'], 'POST', [], [], [], http_build_query($arguments));
$responseContent = json_decode($response->getBody()->getContents(), true);
$this->logger->debug(json_encode($responseContent), LogEnvironment::fromMethodName(__METHOD__));

if (!$responseContent['success']) {
$this->logger->error(json_encode($responseContent), LogEnvironment::fromMethodName(__METHOD__));
$this->addError('We could not identify you as a human. Please try again.', 1676890456);
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), LogEnvironment::fromMethodName(__METHOD__));
$this->addError('We could not identify you as a human. Please try again.', 1676890456);
}
}
}
7 changes: 7 additions & 0 deletions Configuration/Development/Settings.Cloudflare.Turnstile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Tms:
Cloudflare:
Turnstile:
# This key set always passes
# - variants can be found here: https://developers.cloudflare.com/turnstile/frequently-asked-questions/#are-there-sitekeys-and-secret-keys-that-can-be-used-for-testing
siteKey: '1x00000000000000000000AA'
secretKey: '1x0000000000000000000000000000000AA'
25 changes: 25 additions & 0 deletions Configuration/NodeTypes.FormElement.Turnstile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'Tms.Cloudflare.Turnstile:Turnstile':
superTypes:
'Neos.Form.Builder:FormElement': true
ui:
icon: 'icon-shield'
group: 'form.custom'
position: 999
label: 'Cloudflare Turnstile'
help:
message: 'Turnstile is Cloudflare’s smart CAPTCHA alternative.'
properties:
identifier:
defaultValue: 'cf-turnstile'
ui:
inspector:
editorOptions:
disabled: true
label:
defaultValue: ''
required:
defaultValue: true
ui:
inspector:
editorOptions:
disabled: true
15 changes: 15 additions & 0 deletions Configuration/Settings.Cloudflare.Turnstile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Tms:
Cloudflare:
Turnstile:
enabled: true

# Turnstile endpoint & credentials
endpoint: 'https://challenges.cloudflare.com/turnstile/v0/siteverify'
siteKey: '%env:CLOUDFLARE_TURNSTILE_SITE_KEY%'
secretKey: '%env:CLOUDFLARE_TURNSTILE_SECRET_KEY%'

# Turnstile configurations
theme: auto
language: auto
tabindex: 0
size: normal
23 changes: 23 additions & 0 deletions Configuration/Settings.Neos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Neos:
Form:
presets:
default:
formElementTypes:
'Tms.Cloudflare.Turnstile:Turnstile':
superTypes: ['Neos.Form:FormElement']
renderingOptions:
templatePathPattern: 'resource://Tms.Cloudflare.Turnstile/Private/Form/Turnstile.html'
validationErrorTranslationPackage: Tms.Cloudflare.Turnstile
validatorPresets:
'Tms.Cloudflare.Turnstile:TurnstileValidator':
implementationClassName: Tms\Cloudflare\Turnstile\Validation\Validator\TurnstileValidator
Neos:
fusion:
autoInclude:
'Tms.Cloudflare.Turnstile': true

userInterface:
translation:
autoInclude:
'Tms.Cloudflare.Turnstile':
- 'Main'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 tms.development

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Tms.Cloudflare.Turnstile

This package provides a form element for the CAPTCHA alternative [Turnstile by Cloudflare](https://developers.cloudflare.com/turnstile/) and can be used on your Neos CMS site with [Neos.Form](https://github.com/neos/form) & [Neos.Form.Builder](https://github.com/neos/form-builder).


> Turnstile is Cloudflare’s smart CAPTCHA alternative. It can be embedded into any website without sending traffic through Cloudflare and works without showing visitors a CAPTCHA.
Cloudflare Turnstile documentation: https://developers.cloudflare.com/turnstile/

## Install

```bash
composer require tms/cloudflare-turnstile
```

## Usage

1.) [Create a free Cloudflare account](https://dash.cloudflare.com/sign-up) or [log in](https://dash.cloudflare.com/login) to your existing one

2.) Go to **Turnstile**

3.) Select **Add a site** and fill out the form

4.) Copy the `site key` and `secret key` and add the following environment variables

```yaml
# Configuration/Settings.yaml
Tms:
Cloudflare:
Turnstile:
siteKey: '%env:CLOUDFLARE_TURNSTILE_SITE_KEY%'
secretKey: '%env:CLOUDFLARE_TURNSTILE_SECRET_KEY%'
```
**Note:** In `Development` context we automatically set a [test key pair](https://developers.cloudflare.com/turnstile/frequently-asked-questions/#are-there-sitekeys-and-secret-keys-that-can-be-used-for-testing)

5.) Add the Turnstile form element to your form configuration or use the Turnstile content element in your node-based forms

```
prototype(Vendor.PackageName:MyForm) < prototype(Neos.Form.Builder:Form) {
firstPage.elements {
turnstile = Tms.Cloudflare.Turnstile:Turnstile.Definition
}
}
```
## Acknowledgments
Development sponsored by [tms.development - Online Marketing and Neos CMS Agency](https://www.tms-development.de/)
16 changes: 16 additions & 0 deletions Resources/Private/Form/Turnstile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<f:layout name="Neos.Form:Field" />
<f:section name="field">
<noscript>
{f:translate(id: 'widget.noScript.message', package: 'Tms.Cloudflare.Turnstile')}
</noscript>
<div id="{element.uniqueIdentifier}"
class="cf-turnstile"
data-action="{element.uniqueIdentifier}"
data-sitekey="{element.properties.siteKey}"
data-theme="{element.properties.theme}"
data-language="{element.properties.language}"
data-tabindex="{element.properties.tabindex}"
data-size="{element.properties.size}"
data-response-field-name="--{element.parentRenderable.parentRenderable.identifier}[{element.identifier}]"
></div>
</f:section>
9 changes: 9 additions & 0 deletions Resources/Private/Fusion/Elements/Turnstile.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
prototype(Tms.Cloudflare.Turnstile:Turnstile.Definition) < prototype(Neos.Form.Builder:FormElement.Definition) {
@if.enabled = ${Configuration.setting('Tms.Cloudflare.Turnstile.enabled')}
@if.inLiveWorkspace = ${!node.context.inBackend && node.context.workspaceName == 'live'}

formElementType = 'Tms.Cloudflare.Turnstile:Turnstile'
properties = ${Configuration.setting('Tms.Cloudflare.Turnstile')}

validators.turnstile = Tms.Cloudflare.Turnstile:TurnstileValidator.Definition
}
14 changes: 14 additions & 0 deletions Resources/Private/Fusion/Override.Page.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
prototype(Neos.Neos:Page) {
body.javascripts {
cloudflareTurnstileApi = Neos.Fusion:Tag {
@if.enabled = ${Configuration.setting('Tms.Cloudflare.Turnstile.enabled')}
@if.inLiveWorkspace = ${!node.context.inBackend && node.context.workspaceName == 'live'}
tagName = 'script'
attributes {
src = 'https://challenges.cloudflare.com/turnstile/v0/api.js'
async = true
defer = true
}
}
}
}
1 change: 1 addition & 0 deletions Resources/Private/Fusion/Root.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: **/*.fusion
7 changes: 7 additions & 0 deletions Resources/Private/Fusion/Validators/TurnstileValidator.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
prototype(Tms.Cloudflare.Turnstile:TurnstileValidator.Definition) < prototype(Neos.Form.Builder:Validator.Definition) {
formElementType = 'Tms.Cloudflare.Turnstile:TurnstileValidator'
options {
endpoint = ${Configuration.setting('Tms.Cloudflare.Turnstile.endpoint')}
secretKey = ${Configuration.setting('Tms.Cloudflare.Turnstile.secretKey')}
}
}
11 changes: 11 additions & 0 deletions Resources/Private/Translations/de/Main.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="" product-name="Tms.Cloudflare.Turnstile" source-language="en" target-language="de" datatype="plaintext">
<body>
<trans-unit id="widget.noScript.message">
<source>This form is protected by Cloudflare Turnstile. Please enable JavaScript for this page to identify you as a human.</source>
<target xml:lang="de" state="translated">Dieses Formular ist durch Cloudflare Turnstile geschützt. Bitte aktivieren Sie JavaScript, damit diese Seite Sie als Mensch erkennen kann.</target>
</trans-unit>
</body>
</file>
</xliff>
19 changes: 19 additions & 0 deletions Resources/Private/Translations/de/ValidationErrors.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="" product-name="Tms.Cloudflare.Turnstile" source-language="en" target-language="de" datatype="plaintext">
<body>
<trans-unit id="1221560910" xml:space="preserve" approved="yes">
<source>This property is required</source>
<target state="final">Diese Eigenschaft ist erforderlich</target>
</trans-unit>
<trans-unit id="1221560718" xml:space="preserve" approved="yes">
<source>This property is required</source>
<target state="final">Diese Eigenschaft ist erforderlich</target>
</trans-unit>
<trans-unit id="1676890456">
<source>We could not identify you as a human. Please try again.</source>
<target xml:lang="de" state="translated">Wir konnten Sie nicht als Mensch identifizieren. Bitte versuchen Sie es erneut.</target>
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions Resources/Private/Translations/en/Main.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="" product-name="Tms.Cloudflare.Turnstile" source-language="en" datatype="plaintext">
<body>
<trans-unit id="widget.noScript.message">
<source>This form is protected by Cloudflare Turnstile. Please enable JavaScript for this page to identify you as a human.</source>
</trans-unit>
</body>
</file>
</xliff>
17 changes: 17 additions & 0 deletions Resources/Private/Translations/en/ValidationErrors.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="" product-name="Tms.Cloudflare.Turnstile" source-language="en" datatype="plaintext">
<body>
<trans-unit id="1221560910" xml:space="preserve" approved="yes">
<source>This property is required</source>
</trans-unit>
<trans-unit id="1221560718" xml:space="preserve" approved="yes">
<source>This property is required</source>
</trans-unit>
<trans-unit id="1676890456">
<source>We could not identify you as a human. Please try again.</source>
<target xml:lang="de" state="translated">Wir konnten Sie nicht als Mensch identifizieren. Bitte versuchen Sie es erneut.</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions Resources/Private/Translations/es/Main.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="" product-name="Tms.Cloudflare.Turnstile" source-language="en" target-language="es" datatype="plaintext">
<body>
<trans-unit id="widget.noScript.message">
<source>This form is protected by Cloudflare Turnstile. Please enable JavaScript for this page to identify you as a human.</source>
<target xml:lang="es" state="translated">Este formulario está protegido por Cloudflare Turnstile. Por favor, active JavaScript para que esta página le identifique como humano.</target>
</trans-unit>
</body>
</file>
</xliff>
19 changes: 19 additions & 0 deletions Resources/Private/Translations/es/ValidationErrors.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="" product-name="Tms.Cloudflare.Turnstile" source-language="en" target-language="es" datatype="plaintext">
<body>
<trans-unit id="1221560910" xml:space="preserve" approved="yes">
<source>This property is required</source>
<target state="final">Esta propiedad es obligatoria</target>
</trans-unit>
<trans-unit id="1221560718" xml:space="preserve" approved="yes">
<source>This property is required</source>
<target state="final">Esta propiedad es obligatoria</target>
</trans-unit>
<trans-unit id="1676890456">
<source>We could not identify you as a human. Please try again.</source>
<target xml:lang="es" state="translated">No hemos podido identificarle como humano. Por favor, inténtelo de nuevo.</target>
</trans-unit>
</body>
</file>
</xliff>
Loading

0 comments on commit 1ba52aa

Please sign in to comment.