Skip to content

Commit 06ac468

Browse files
authored
Set minimal score for validation (#64)
1 parent 17c9d93 commit 06ac468

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/DI/ReCaptchaExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function getConfigSchema(): Schema
1818
return Expect::structure([
1919
'siteKey' => Expect::string()->required(),
2020
'secretKey' => Expect::string()->required(),
21+
'minimalScore' => Expect::anyOf(Expect::float()->min(0)->max(1), Expect::int()->min(0)->max(1))->default(0),
2122
]);
2223
}
2324

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

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

3637
/**

src/Forms/InvisibleReCaptchaField.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Contributte\ReCaptcha\Forms;
44

5+
use Contributte\ReCaptcha\Exceptions\InvalidScoreException;
56
use Contributte\ReCaptcha\ReCaptchaProvider;
67
use Nette\Forms\Controls\HiddenField;
78
use Nette\Forms\Form;
@@ -46,6 +47,17 @@ public function setMessage(string $message): self
4647
return $this;
4748
}
4849

50+
public function setMinimalScore(float $score): self
51+
{
52+
if ($score < 0 || $score > 1) {
53+
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).');
54+
}
55+
56+
$this->provider->setMinimalScore($score);
57+
58+
return $this;
59+
}
60+
4961
public function validate(): void
5062
{
5163
$this->configureValidation();

src/ReCaptchaProvider.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class ReCaptchaProvider
2828

2929
private string $secretKey;
3030

31-
public function __construct(string $siteKey, string $secretKey)
31+
// Range 0..1 (1.0 is very likely a good interaction, 0.0 is very likely a bot)
32+
private float $minimalScore;
33+
34+
public function __construct(string $siteKey, string $secretKey, float $minimalScore)
3235
{
3336
$this->siteKey = $siteKey;
3437
$this->secretKey = $secretKey;
38+
$this->setMinimalScore($minimalScore);
3539
}
3640

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

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

6367
public function validateControl(BaseControl $control): bool
@@ -77,6 +81,11 @@ public function validateControl(BaseControl $control): bool
7781
return false;
7882
}
7983

84+
public function setMinimalScore(float $score): void
85+
{
86+
$this->minimalScore = $score;
87+
}
88+
8089
protected function makeRequest(?string $response, ?string $remoteIp = null): string|null
8190
{
8291
if ($response === null || $response === '') {

0 commit comments

Comments
 (0)