Skip to content

Commit

Permalink
Updated verify signature of RecaptchaService.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed May 6, 2024
1 parent ecf146b commit 916a3f2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/Service/RecaptchaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ public function translateErrors(array $codes): array
return \array_map(fn (mixed $code): string => $this->translateError("recaptcha.$code"), $codes);
}

public function verify(string $response, ?Request $request = null): Response
public function verify(string $response, Request $request): Response
{
$request ??= Request::createFromGlobals();
$recaptcha = new ReCaptcha($this->secretKey);
$recaptcha->setChallengeTimeout($this->challengeTimeout)
->setScoreThreshold($this->scoreThreshold)
Expand Down
14 changes: 12 additions & 2 deletions src/Validator/RecaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace App\Validator;

use App\Service\RecaptchaService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;

/**
Expand All @@ -22,7 +24,7 @@
*/
class RecaptchaValidator extends AbstractConstraintValidator
{
public function __construct(private readonly RecaptchaService $service)
public function __construct(private readonly RecaptchaService $service, private readonly RequestStack $requestStack)
{
parent::__construct(Recaptcha::class);
}
Expand All @@ -32,7 +34,15 @@ public function __construct(private readonly RecaptchaService $service)
*/
protected function doValidate(string $value, Constraint $constraint): void
{
$response = $this->service->verify($value);
$request = $this->requestStack->getCurrentRequest();
if (!$request instanceof Request) {
$this->context->buildViolation('recaptcha.no-request')
->addViolation();

return;
}

$response = $this->service->verify($value, $request);
if ($response->isSuccess()) {
return;
}
Expand Down
24 changes: 22 additions & 2 deletions tests/Validator/RecaptchaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use App\Validator\RecaptchaValidator;
use PHPUnit\Framework\MockObject\Exception;
use ReCaptcha\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
Expand Down Expand Up @@ -83,14 +85,31 @@ public function testSuccess(): void
*/
protected function createValidator(): RecaptchaValidator
{
return new RecaptchaValidator($this->createService());
$service = $this->createService();
$requestStack = $this->createRequestStack();

return new RecaptchaValidator($service, $requestStack);
}

private function createConstraint(): Recaptcha
{
return new Recaptcha();
}

/**
* @throws Exception
*/
private function createRequestStack(): RequestStack
{
$request = $this->createMock(Request::class);
$requestStack = $this->createMock(RequestStack::class);
$requestStack->expects(self::any())
->method('getCurrentRequest')
->willReturn($request);

return $requestStack;
}

/**
* @throws Exception
*/
Expand All @@ -114,7 +133,8 @@ private function createService(string $code = ''): RecaptchaService
private function initValidator(string $code = ''): RecaptchaValidator
{
$service = $this->createService($code);
$this->validator = new RecaptchaValidator($service);
$requestStack = $this->createRequestStack();
$this->validator = new RecaptchaValidator($service, $requestStack);
$this->validator->initialize($this->context);

return $this->validator;
Expand Down
1 change: 1 addition & 0 deletions translations/validators.fr_CH.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ recaptcha:
timeout-or-duplicate: Le délai d'attente est atteint ou la validation a été appelée deux fois.
unknown-error: Une erreur inconnue s'est produite durant de la validation.
incorrect-captcha-sol: Une erreur inconnue s'est produite durant de la validation.
no-request: Aucune requête valide n'est définie.
state:
unique_code: Ce nom est déjà utilisé par un autre statut.
task:
Expand Down

0 comments on commit 916a3f2

Please sign in to comment.