Skip to content

Commit

Permalink
Initial commit for Cloudflare Turnstile plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
anselmh committed Nov 10, 2022
1 parent c1ec623 commit 5d13b2d
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 94 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Lukas Leitsch
Copyright (c) 2022 Anselm Hannemann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Uniform hCaptcha Guard
# Uniform Clouflare Turnstile Guard

A [Kirby 3](https://getkirby.com/) plugin implementing a [hCaptcha](https://www.hcaptcha.com/) guard for the [Uniform](https://github.com/mzur/kirby-uniform) plugin.
A [Kirby 3](https://getkirby.com/) plugin implementing a [Cloudflare Turnstile](https://developers.cloudflare.com/turnstile/) guard for the [Uniform](https://github.com/mzur/kirby-uniform) plugin.

## Installation

Expand All @@ -9,7 +9,7 @@ A [Kirby 3](https://getkirby.com/) plugin implementing a [hCaptcha](https://www.
Add the plugin to your project:

```
composer require leitsch/kirby-uniform-hcaptcha
composer require anselmh/kirby-uniform-turnstile
```

## Configuration
Expand All @@ -18,24 +18,24 @@ Set the configuration in your `config.php` file:

```php
return [
'leitsch.uniform-hcaptcha.siteKey' => 'my-site-key',
'leitsch.uniform-hcaptcha.secretKey' => 'my-secret-key',
'anselmh.uniform-turnstile.siteKey' => 'my-site-key',
'anselmh.uniform-turnstile.secretKey' => 'my-secret-key',
];
```

## Usage

### Template

You can use the provided helper function to embed the hCaptcha into your template:
You can use the provided helper function to embed the Turnstile into your template:

```html+php
<?= hcaptchaField() ?>
<?= turnstileField() ?>
```

In order for hCaptcha to work, you need to provide the hCaptcha JavaScript file.
In order for turnstile to work, you need to provide the Turnstile JavaScript file.

Use the helper function `hcaptchaScript()` in your template.
Use the helper function `turnstileScript()` in your template or add it to the template at right before the closing `</body>` tag.

**Example**

Expand All @@ -47,26 +47,27 @@ Use the helper function `hcaptchaScript()` in your template.
<!-- ... -->

<?= csrf_field() ?>
<?= hcaptchaField() ?>
<?= turnstileField() ?>
<input type="submit" value="Submit">
</form>
<?= hcaptchaScript() ?>
<?= turnstileScript() ?>
```

### Controller

In your controller you can use the [magic method](https://kirby-uniform.readthedocs.io/en/latest/guards/guards/#magic-methods) `hcaptchaGuard()` to enable the hCaptcha guard:
In your controller you can use the [magic method](https://kirby-uniform.readthedocs.io/en/latest/guards/guards/#magic-methods) `turnstileGuard()` to enable the turnstile guard:

```php
$form = new Form(/* ... */);

if ($kirby->request()->is('POST'))
{
$form->hcaptchaGuard()
$form->turnstileGuard()
->emailAction(/* ... */);
}
```

## Credits

- Thanks to Lukas Dürrenberger for the [Uniform reCAPTCHA Guard Plugin](https://github.com/eXpl0it3r/kirby-uniform-recaptcha)
- Thanks to Lukas Leitsch for the [Uniform hCaptcha Guard Plugin](https://github.com/lukasleitsch/kirby-uniform-hcaptcha)
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "leitsch/kirby-uniform-hcaptcha",
"description": "Kirby 3 hCaptcha guard for the Uniform plugin",
"name": "anselmh/kirby-uniform-turnstile",
"description": "Kirby 3 Cloudflare Turnstile guard for the Uniform plugin",
"type": "kirby-plugin",
"require": {
"php": "^8.0",
"getkirby/composer-installer": "^1.1",
"getkirby/composer-installer": "^1.2",
"mzur/kirby-uniform": "^5.0"
},
"license": "MIT",
"authors": [{
"name": "Lukas Leitsch",
"email": "[email protected]"
"name": "Anselm Hannemann ",
"email": "[email protected]"
}],
"autoload": {
"psr-4": {
Expand All @@ -24,6 +24,6 @@
"optimize-autoloader": true
},
"extra": {
"installer-name": "uniform-hcaptcha"
"installer-name": "uniform-turnstile"
}
}
4 changes: 2 additions & 2 deletions i18n/de.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

return [
'uniform-hcaptcha-empty' => 'Die hCaptcha Eingabe ist leer.',
'uniform-hcaptcha-invalid' => 'Die hCaptcha Eingabe ist ungültig.'
'uniform-turnstile-empty' => 'Die Cloudflare Turnstile Sicherheitsaufgabe wurde leider nicht aufgefüllt.',
'uniform-turnstile-invalid' => 'Die Lösung für die Cloudflare Turnstile Sicherheitsaufgabe ist leider ungültig, probieren Sie es erneut.'
];
4 changes: 2 additions & 2 deletions i18n/en.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

return [
'uniform-hcaptcha-empty' => 'The hCaptcha value is empty.',
'uniform-hcaptcha-invalid' => 'The hCaptcha value is invalid.'
'uniform-turnstile-empty' => 'The Cloudflare Turnstile challenge was not solved.',
'uniform-turnstile-invalid' => 'The Cloudflare Turnstile challenge response is invalid.'
];
5 changes: 2 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

@include_once __DIR__.'/vendor/autoload.php';

Kirby::plugin('leitsch/uniform-hcaptcha', [
Kirby::plugin('anselmh/uniform-turnstile', [
'options' => [
'siteKey' => '',
'secretKey' => '',
'size' => 'normal',
'secretKey' => ''
],
'translations' => [
'en' => @include_once __DIR__.'/i18n/en.php',
Expand Down
58 changes: 0 additions & 58 deletions src/Guards/HcaptchaGuard.php

This file was deleted.

58 changes: 58 additions & 0 deletions src/Guards/TurnstileGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Uniform\Guards;

use Kirby\Http\Remote;
use Uniform\Exceptions\Exception;

class TurnstileGuard extends Guard
{
/**
* Turnstile HTML input field name
*
* @var string
*/
const fieldName = 'turnstile-response';

/**
* URL for the Turnstile verification
*
* @var string
*/
const verificationUrl = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';

/**
* {@inheritDoc}
*
* Verify the turnstile challenge
* Remove the field from the form data if it was correct
*/
public function perform()
{
$turnstileChallenge = kirby()->request()->get(self::fieldName);

if (empty($turnstileChallenge)) {
$this->reject(t('uniform-turnstile-empty'), self::fieldName);
}

$secretKey = option('anselmh.uniform-turnstile.secretKey');

if (empty($secretKey)) {
throw new Exception('The Turnstile secret key for Uniform is not configured');
}

$response = Remote::request(self::verificationUrl, [
'method' => 'POST',
'data' => [
'secret' => $secretKey,
'response' => $turnstileChallenge,
],
]);

if ($response->code() !== 200 || $response->json()['success'] !== true) {
$this->reject(t('uniform-turnstile-invalid'), self::fieldName);
}

$this->form->forget(self::fieldName);
}
}
19 changes: 10 additions & 9 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

use Uniform\Exceptions\Exception as UniformException;

if (!function_exists('hcaptchaField')) {
function hcaptchaField(): string
if (!function_exists('turnstileField')) {
function turnstileField(): string
{
$siteKey = option('leitsch.uniform-hcaptcha.siteKey');
$size = option('leitsch.uniform-hcaptcha.size');
$siteKey = option('anselmh.uniform-turnstile.siteKey');

if (empty($siteKey)) {
throw new UniformException('The hCaptcha sitekey for Uniform is not configured');
throw new UniformException('The Turnstile sitekey for Uniform is not configured');
}

return '<div class="h-captcha" data-sitekey="'.$siteKey.'" data-size="' . $size . '"></div>';
return '<div class="cf-turnstile" data-sitekey="'.$siteKey.'" data-callback="javascriptCallback"></div>';
}
}

if (!function_exists('hcaptchaScript')) {
function hcaptchaScript(): string
if (!function_exists('turnstileScript')) {
function turnstileScript(): string
{
return '<script src="https://hcaptcha.com/1/api.js" async defer></script>';
return '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>';
}
}


0 comments on commit 5d13b2d

Please sign in to comment.