Skip to content

Commit

Permalink
Set minimal score for validation (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelWeirich authored Jan 2, 2024
1 parent 17c9d93 commit 06ac468
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/DI/ReCaptchaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function getConfigSchema(): Schema
return Expect::structure([
'siteKey' => Expect::string()->required(),
'secretKey' => Expect::string()->required(),
'minimalScore' => Expect::anyOf(Expect::float()->min(0)->max(1), Expect::int()->min(0)->max(1))->default(0),
]);
}

Expand All @@ -30,7 +31,7 @@ public function loadConfiguration(): void
$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('provider'))
->setFactory(ReCaptchaProvider::class, [$config['siteKey'], $config['secretKey']]);
->setFactory(ReCaptchaProvider::class, [$config['siteKey'], $config['secretKey'], $config['minimalScore']]);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Forms/InvisibleReCaptchaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Contributte\ReCaptcha\Forms;

use Contributte\ReCaptcha\Exceptions\InvalidScoreException;

Check failure on line 5 in src/Forms/InvisibleReCaptchaField.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Type Contributte\ReCaptcha\Exceptions\InvalidScoreException is not used in this file.
use Contributte\ReCaptcha\ReCaptchaProvider;
use Nette\Forms\Controls\HiddenField;
use Nette\Forms\Form;
Expand Down Expand Up @@ -46,6 +47,17 @@ public function setMessage(string $message): self
return $this;
}

public function setMinimalScore(float $score): self
{
if ($score < 0 || $score > 1) {
throw new \LogicException('Minimal score expects to be in range 0..1 (1.0 is very likely a good interaction, 0.0 is very likely a bot).');
}

$this->provider->setMinimalScore($score);

return $this;
}

public function validate(): void
{
$this->configureValidation();
Expand Down
13 changes: 11 additions & 2 deletions src/ReCaptchaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class ReCaptchaProvider

private string $secretKey;

public function __construct(string $siteKey, string $secretKey)
// Range 0..1 (1.0 is very likely a good interaction, 0.0 is very likely a bot)
private float $minimalScore;

Check failure on line 32 in src/ReCaptchaProvider.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

You must use "/**" style comments for a member variable comment

public function __construct(string $siteKey, string $secretKey, float $minimalScore)
{
$this->siteKey = $siteKey;
$this->secretKey = $secretKey;
$this->setMinimalScore($minimalScore);
}

public function getSiteKey(): string
Expand All @@ -57,7 +61,7 @@ public function validate(string $response): ?ReCaptchaResponse
$answer = json_decode($response, true);

// Return response
return $answer['success'] === true ? new ReCaptchaResponse(true) : new ReCaptchaResponse(false, $answer['error-codes'] ?? null);
return $answer['success'] === true && $answer['score'] >= $this->minimalScore ? new ReCaptchaResponse(true) : new ReCaptchaResponse(false, $answer['error-codes'] ?? null);
}

public function validateControl(BaseControl $control): bool
Expand All @@ -77,6 +81,11 @@ public function validateControl(BaseControl $control): bool
return false;
}

public function setMinimalScore(float $score): void
{
$this->minimalScore = $score;
}

protected function makeRequest(?string $response, ?string $remoteIp = null): string|null
{
if ($response === null || $response === '') {
Expand Down

0 comments on commit 06ac468

Please sign in to comment.