-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.4.0' into main
- Loading branch information
Showing
19 changed files
with
335 additions
and
103 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
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
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
1 change: 1 addition & 0 deletions
1
src/DependencyInjection/PixelOpenCloudflareTurnstileExtension.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PixelOpen\CloudflareTurnstileBundle\Http; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class CloudflareTurnstileHttpClient | ||
{ | ||
private const SITEVERIFY_ENDPOINT = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'; | ||
|
||
private HttpClientInterface $httpClient; | ||
|
||
private string $secret; | ||
|
||
private LoggerInterface $logger; | ||
|
||
public function __construct(string $secret, HttpClientInterface $httpClient, LoggerInterface $logger) | ||
{ | ||
$this->secret = $secret; | ||
$this->httpClient = $httpClient; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function verifyResponse(string $turnstileResponse): bool | ||
{ | ||
$response = $this->httpClient->request( | ||
Request::METHOD_POST, | ||
self::SITEVERIFY_ENDPOINT, | ||
[ | ||
'body' => [ | ||
'response' => $turnstileResponse, | ||
'secret' => $this->secret, | ||
], | ||
] | ||
); | ||
|
||
try { | ||
$content = $response->toArray(); | ||
} catch (ExceptionInterface $e) { | ||
$this->logger->error( | ||
\sprintf( | ||
'Cloudflare Turnstile HTTP exception (%s) with a message: %s', | ||
\get_class($e), | ||
$e->getMessage(), | ||
), | ||
); | ||
|
||
return false; | ||
} | ||
|
||
return \array_key_exists('success', $content) && $content['success'] === true; | ||
} | ||
} |
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
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
5 changes: 3 additions & 2 deletions
5
src/Constraints/CloudflareTurnstile.php → src/Validator/CloudflareTurnstile.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PixelOpen\CloudflareTurnstileBundle\Validator; | ||
|
||
use PixelOpen\CloudflareTurnstileBundle\Http\CloudflareTurnstileHttpClient; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
final class CloudflareTurnstileValidator extends ConstraintValidator | ||
{ | ||
private bool $enable; | ||
|
||
private RequestStack $requestStack; | ||
|
||
private CloudflareTurnstileHttpClient $turnstileHttpClient; | ||
|
||
public function __construct( | ||
bool $enable, | ||
RequestStack $requestStack, | ||
CloudflareTurnstileHttpClient $turnstileHttpClient | ||
) { | ||
$this->enable = $enable; | ||
$this->requestStack = $requestStack; | ||
$this->turnstileHttpClient = $turnstileHttpClient; | ||
} | ||
|
||
/** | ||
* Checks if the passed value is valid. | ||
* | ||
* @param mixed $value The value that should be validated | ||
* @param Constraint $constraint The constraint for the validation | ||
*/ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
if ($this->enable === false) { | ||
return; | ||
} | ||
|
||
$request = $this->requestStack->getCurrentRequest(); | ||
\assert($request !== null); | ||
$turnstileResponse = (string) $request->request->get('cf-turnstile-response'); | ||
|
||
if ($turnstileResponse === '') { | ||
$this->context->buildViolation($constraint->message) | ||
->addviolation(); | ||
|
||
return; | ||
} | ||
|
||
if ($this->turnstileHttpClient->verifyResponse($turnstileResponse) === false) { | ||
$this->context->buildViolation($constraint->message) | ||
->addviolation(); | ||
} | ||
} | ||
} |
Oops, something went wrong.