Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set minimal score for validation #64

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 @@
$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 @@
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
Loading