-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting different captcha providers and hCaptcha support (#9)
Supporting different captcha providers hCaptcha provider --------- Co-authored-by: Aleks S <avshelestov@gmail.com>
- Loading branch information
Showing
15 changed files
with
292 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
0.2.2 | ||
== | ||
26 Frb 2024 | ||
|
||
**Added:** | ||
|
||
* Supporting different captcha providers | ||
* hCaptcha provider | ||
|
||
**Migration guide:** | ||
* Create and run BD migrations to apply new changes (see on https://phpform.dev) | ||
* Check forms and update captcha settings if needed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 11 additions & 2 deletions
13
src/Admin/Form/FormRecaptchaType.php → src/Admin/Form/FormCaptchaType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{% extends '@Admin/forms/page.html.twig' %} | ||
|
||
{% block title %}API / {{ formEntity.name }} / Forms{% endblock %} | ||
|
||
{% block section %} | ||
<div class="card"> | ||
<header class="card-header"> | ||
<p class="card-header-title"> | ||
Protect form with captcha | ||
</p> | ||
</header> | ||
<div class="card-content" x-data="providersData()"> | ||
<div class="content"> | ||
<form method="post"> | ||
{{ form_start(form) }} | ||
<p> | ||
Generate your Secret Key at <a target="_blank" :href="currentProvider.homepageUrl"><span x-html="currentProvider.name"></span></a>. | ||
Only the Secret Key is required here. | ||
Ensure to implement token retrieval on the frontend and include it in the PHPForm endpoint request under the 'captchaResponse' key. | ||
</p> | ||
{{ form_row(form.captcha_provider, { | ||
'attr': {'@change': 'change($event.target.value)'} | ||
}) }} | ||
{{ form_row(form.captcha_token) }} | ||
<p> | ||
Keep the field empty to disable Captcha Protection. | ||
</p> | ||
<p> | ||
Example of how to include the token in the request: | ||
</p> | ||
<div class="mb-4"> | ||
<pre style="overflow:auto; width:100%; max-width: 550px;"><code>fetch('https://your-domain.com/api/forms/{{ formEntity.hash }}', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
captchaResponse: 'captcha-token-here', | ||
// other form fields | ||
}), | ||
})</code></pre> | ||
</div> | ||
<p> | ||
More information on how to use it can be found <a target="_blank" :href="currentProvider.documentationUrl">here</a>. | ||
</p> | ||
{{ form_row(form.save) }} | ||
{{ form_end(form) }} | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
function providersData() { | ||
const providers = {{ providersInfo|json_encode|raw }}; | ||
return { | ||
providers, | ||
currentProvider: providers[0], | ||
change: function (value) { | ||
this.currentProvider = providers[value]; | ||
} | ||
}; | ||
} | ||
</script> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
namespace App\Captcha; | ||
|
||
use App\Captcha\Providers\HCaptchaProvider; | ||
use App\Captcha\Providers\ReCaptchaProvider; | ||
|
||
final class Captcha | ||
{ | ||
public const CAPTCHA_PROVIDER_RECAPTCHA = 0; | ||
public const CAPTCHA_PROVIDER_HCAPTCHA = 1; | ||
|
||
public function getProviders(): array | ||
{ | ||
return [ | ||
self::CAPTCHA_PROVIDER_RECAPTCHA => new ReCaptchaProvider(), | ||
self::CAPTCHA_PROVIDER_HCAPTCHA => new HCaptchaProvider(), | ||
]; | ||
} | ||
|
||
public function getProvider(int $provider): ?CaptchaProviderInterface | ||
{ | ||
return $this->getProviders()[$provider] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace App\Captcha; | ||
|
||
interface CaptchaProviderInterface | ||
{ | ||
public function validate(string $response, string $secretKey, ?string $userIp = null): bool; | ||
|
||
public function getName(): string; | ||
|
||
public function getHomePageUrl(): string; | ||
|
||
public function getDocumentationUrl(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace App\Captcha\Providers; | ||
|
||
use App\Captcha\CaptchaProviderInterface; | ||
|
||
class HCaptchaProvider implements CaptchaProviderInterface | ||
{ | ||
private string $verifyUrl = 'https://api.hcaptcha.com/siteverify'; | ||
|
||
public function validate(string $response, string $secretKey, ?string $userIp = null): bool | ||
{ | ||
if (empty($response)) { | ||
return false; | ||
} | ||
|
||
$data = [ | ||
'secret' => $secretKey, | ||
'response' => $response, | ||
'remoteip' => $userIp | ||
]; | ||
|
||
$options = [ | ||
'http' => [ | ||
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | ||
'method' => 'POST', | ||
'content' => http_build_query($data) | ||
] | ||
]; | ||
|
||
$context = stream_context_create($options); | ||
$response = file_get_contents($this->verifyUrl, false, $context); | ||
if ($response === false) { | ||
return false; | ||
} | ||
|
||
$result = json_decode($response); | ||
return $result->success; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'hCaptcha'; | ||
} | ||
|
||
public function getHomePageUrl(): string | ||
{ | ||
return 'https://www.hcaptcha.com/'; | ||
} | ||
|
||
public function getDocumentationUrl(): string | ||
{ | ||
return 'https://docs.hcaptcha.com/'; | ||
} | ||
} |
Oops, something went wrong.