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 1 commit
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
8 changes: 8 additions & 0 deletions src/Exceptions/InvalidScoreException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace Contributte\ReCaptcha\Exceptions;

class InvalidScoreException extends \Exception
{

}
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;
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 InvalidScoreException('Minimal score expects to be in range 0..1 (1.0 is very likely a good interaction, 0.0 is very likely a bot).');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use LogicException? There is no need to have a custom class for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

$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