Skip to content
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
26 changes: 13 additions & 13 deletions src/ReCaptcha/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ class ReCaptcha
*/
private RequestMethod $requestMethod;

private string $hostname;
private string $apkPackageName;
private string $action;
private float $threshold;
private int $timeoutSeconds;
private ?string $hostname = null;
private ?string $apkPackageName = null;
private ?string $action = null;
private ?float $threshold = null;
private ?int $timeoutSeconds = null;

/**
* Create a configured instance to use the reCAPTCHA service.
Expand All @@ -161,13 +161,13 @@ class ReCaptcha
*/
public function __construct(string $secret, ?RequestMethod $requestMethod = null)
{
if (empty($secret)) {
if ('' === $secret) {
throw new \RuntimeException('No secret provided');
}

$this->secret = $secret;

if (!is_null($requestMethod)) {
if (null !== $requestMethod) {
$this->requestMethod = $requestMethod;
} elseif (function_exists('curl_version')) {
$this->requestMethod = new RequestMethod\CurlPost();
Expand All @@ -188,7 +188,7 @@ public function __construct(string $secret, ?RequestMethod $requestMethod = null
public function verify(string $response, ?string $remoteIp = null): Response
{
// Discard empty solution submissions
if (empty($response)) {
if ('' === $response) {
return new Response(false, [self::E_MISSING_INPUT_RESPONSE]);
}

Expand All @@ -197,23 +197,23 @@ public function verify(string $response, ?string $remoteIp = null): Response
$initialResponse = Response::fromJson($rawResponse);
$validationErrors = [];

if (isset($this->hostname) && 0 !== strcasecmp($this->hostname, $initialResponse->getHostname())) {
if (null !== $this->hostname && 0 !== strcasecmp($this->hostname, $initialResponse->getHostname())) {
$validationErrors[] = self::E_HOSTNAME_MISMATCH;
}

if (isset($this->apkPackageName) && 0 !== strcasecmp($this->apkPackageName, $initialResponse->getApkPackageName())) {
if (null !== $this->apkPackageName && 0 !== strcasecmp($this->apkPackageName, $initialResponse->getApkPackageName())) {
$validationErrors[] = self::E_APK_PACKAGE_NAME_MISMATCH;
}

if (isset($this->action) && 0 !== strcasecmp($this->action, $initialResponse->getAction())) {
if (null !== $this->action && 0 !== strcasecmp($this->action, $initialResponse->getAction())) {
$validationErrors[] = self::E_ACTION_MISMATCH;
}

if (isset($this->threshold) && $this->threshold > $initialResponse->getScore()) {
if (null !== $this->threshold && $this->threshold > $initialResponse->getScore()) {
$validationErrors[] = self::E_SCORE_THRESHOLD_NOT_MET;
}

if (isset($this->timeoutSeconds)) {
if (null !== $this->timeoutSeconds) {
$challengeTs = strtotime($initialResponse->getChallengeTs());

if ($challengeTs > 0 && time() - $challengeTs > $this->timeoutSeconds) {
Expand Down
14 changes: 14 additions & 0 deletions tests/ReCaptcha/ReCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ public function testVerifyReturnsErrorOnMissingResponse(): void
$this->assertEquals([ReCaptcha::E_MISSING_INPUT_RESPONSE], $response->getErrorCodes());
}

public function testZeroAsStringIsValidSecret(): void
{
$rc = new ReCaptcha('0');
$this->assertInstanceOf(ReCaptcha::class, $rc);
}

public function testZeroAsStringIsValidResponse(): void
{
$method = $this->getMockRequestMethod('{"success": true}');
$rc = new ReCaptcha('secret', $method);
$response = $rc->verify('0');
$this->assertTrue($response->isSuccess());
}

public function testDefaultRequestMethodWithCurl(): void
{
GlobalState::$isCurlAvailable = true;
Expand Down
Loading