Skip to content

Commit

Permalink
[2.x] New version
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Mar 6, 2024
2 parents e1e0d9c + a43a749 commit 71917ce
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 62 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"illuminate/http": "10.*|11.*",
"illuminate/routing": "10.*|11.*",
"illuminate/container": "10.*|11.*",
"illuminate/events": "10.*|11.*"
"illuminate/events": "10.*|11.*",
"guzzlehttp/guzzle": "^7.5"
},
"require-dev": {
"orchestra/testbench": "8.*|9.*"
Expand Down
12 changes: 6 additions & 6 deletions config/recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
|
*/

'hostname' => env('RECAPTCHA_HOSTNAME'),
'hostname' => env('RECAPTCHA_HOSTNAME'),
'apk_package_name' => env('RECAPTCHA_APK_PACKAGE_NAME'),

/*
Expand Down Expand Up @@ -73,7 +73,7 @@

'remember' => [
'enabled' => false,
'key' => '_recaptcha',
'key' => '_recaptcha',
'minutes' => 10,
],

Expand Down Expand Up @@ -107,19 +107,19 @@
'credentials' => [
ReCaptcha::CHECKBOX => [
'secret' => env('RECAPTCHA_CHECKBOX_SECRET', ReCaptcha::TEST_V2_SECRET),
'key' => env('RECAPTCHA_CHECKBOX_KEY', ReCaptcha::TEST_V2_KEY),
'key' => env('RECAPTCHA_CHECKBOX_KEY', ReCaptcha::TEST_V2_KEY),
],
ReCaptcha::INVISIBLE => [
'secret' => env('RECAPTCHA_INVISIBLE_SECRET', ReCaptcha::TEST_V2_SECRET),
'key' => env('RECAPTCHA_INVISIBLE_KEY', ReCaptcha::TEST_V2_KEY),
'key' => env('RECAPTCHA_INVISIBLE_KEY', ReCaptcha::TEST_V2_KEY),
],
ReCaptcha::ANDROID => [
'secret' => env('RECAPTCHA_ANDROID_SECRET', ReCaptcha::TEST_V2_SECRET),
'key' => env('RECAPTCHA_ANDROID_KEY', ReCaptcha::TEST_V2_KEY),
'key' => env('RECAPTCHA_ANDROID_KEY', ReCaptcha::TEST_V2_KEY),
],
ReCaptcha::SCORE => [
'secret' => env('RECAPTCHA_SCORE_SECRET'),
'key' => env('RECAPTCHA_SCORE_KEY'),
'key' => env('RECAPTCHA_SCORE_KEY'),
],
],
];
4 changes: 2 additions & 2 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
'match' => 'The reCAPTCHA challenge does not matches this action.',
'match' => 'The reCAPTCHA challenge does not matches this action.',
'missing' => 'The reCAPTCHA challenge was not completed or is missing.',
'error' => 'Error resolving the reCAPTCHA challenge: :errors.',
'error' => 'Error resolving the reCAPTCHA challenge: :errors.',
];
1 change: 1 addition & 0 deletions src/Http/Controllers/ConfirmationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Laragear\ReCaptcha\Http\Middleware\Builders\ReCaptcha;

use function redirect;

class ConfirmationController extends Controller
Expand Down
1 change: 1 addition & 0 deletions src/Http/Middleware/Builders/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laragear\ReCaptcha\Http\Middleware\VerifyReCaptchaV3;
use Laragear\ReCaptcha\ReCaptcha as BaseReCaptcha;
use LogicException;

use function config;
use function debug_backtrace;
use function max;
Expand Down
1 change: 1 addition & 0 deletions src/Http/Middleware/ConfirmReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Http\Request;

use function redirect;
use function url;

Expand Down
1 change: 1 addition & 0 deletions src/Http/Middleware/VerifyHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laragear\ReCaptcha\Facades\ReCaptcha as ReCaptchaFacade;
use Laragear\ReCaptcha\Http\ReCaptchaResponse;
use Laragear\ReCaptcha\ReCaptcha;

use function app;
use function back;
use function strtolower;
Expand Down
19 changes: 6 additions & 13 deletions src/Http/Middleware/VerifyReCaptchaV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,12 @@ protected function shouldCheckRemember(string $remember): bool
/**
* Stores the ReCaptcha remember expiration time in the session.
*/
protected function storeRememberInSession(string|int $offset): void
protected function storeRememberInSession(string $offset): void
{
$offset = strtolower($offset);

if ($offset === 'null') {
$offset = $this->rememberMinutes();
}

$offset = match ($offset) {
INF, 'inf', 'infinite', 'forever' => INF,
default => now()->addMinutes($offset)->getTimestamp(),
};

session()->put($this->rememberKey(), $offset);
session()->put($this->rememberKey(), match (strtolower($offset)) {
'null' => $this->rememberMinutes(),
'inf', 'infinite', 'forever' => INF,
default => now()->addMinutes((int) $offset)->getTimestamp(),
});
}
}
1 change: 1 addition & 0 deletions src/Http/Middleware/VerifyReCaptchaV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laragear\ReCaptcha\Facades\ReCaptcha as ReCaptchaFacade;
use Laragear\ReCaptcha\Http\ReCaptchaResponse;
use Laragear\ReCaptcha\ReCaptcha;

use function app;

class VerifyReCaptchaV3
Expand Down
2 changes: 2 additions & 0 deletions src/Http/ReCaptchaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use Illuminate\Http\Client\Response;
use Illuminate\Support\Carbon;
use JsonSerializable;

use function array_key_exists;
use function json_encode;
use function value;

use const JSON_THROW_ON_ERROR;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Http/ValidatesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;

use function array_filter;
use function back;
use function config;
Expand Down
3 changes: 2 additions & 1 deletion src/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Client\Factory;
use Laragear\ReCaptcha\Http\ReCaptchaResponse;
use LogicException;

use function app;

class ReCaptcha
Expand Down Expand Up @@ -87,7 +88,7 @@ protected function request(string $challenge, string $ip, string $version): Prom
->async()
->withOptions($this->config->get('recaptcha.client'))
->post(static::SERVER_ENDPOINT, [
'secret' => $this->secret($version),
'secret' => $this->secret($version),
'response' => $challenge,
'remoteip' => $ip,
]);
Expand Down
2 changes: 2 additions & 0 deletions src/ReCaptchaFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Illuminate\Http\Client\Response;
use Laragear\ReCaptcha\Http\ReCaptchaResponse;

use function json_encode;
use function now;

use const JSON_THROW_ON_ERROR;

/**
Expand Down
1 change: 1 addition & 0 deletions src/RequestMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laragear\ReCaptcha;

use Laragear\ReCaptcha\Http\ReCaptchaResponse;

use function app;

/**
Expand Down
34 changes: 17 additions & 17 deletions tests/Http/Middleware/ScoreMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public function test_local_fakes_human_response_automatically(): void
->assertOk()
->assertExactJson(
[
'success' => true,
'score' => 1,
'action' => null,
'hostname' => null,
'success' => true,
'score' => 1,
'action' => null,
'hostname' => null,
'apk_package_name' => null,
'challenge_ts' => now()->toAtomString(),
'challenge_ts' => now()->toAtomString(),
]
);
}
Expand All @@ -60,12 +60,12 @@ public function test_local_fakes_robot_response_if_input_is_robot_present(): voi
->assertOk()
->assertExactJson(
[
'success' => true,
'score' => 0,
'action' => null,
'hostname' => null,
'success' => true,
'score' => 0,
'action' => null,
'hostname' => null,
'apk_package_name' => null,
'challenge_ts' => now()->toAtomString(),
'challenge_ts' => now()->toAtomString(),
]
);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public function test_faking_on_production_will_fake_the_response_anyway(): void
->assertOk()
->assertJsonFragment([
'success' => true,
'score' => 1.0,
'score' => 1.0,
]);

static::assertInstanceOf(ReCaptchaFake::class, $this->app->make(ReCaptcha::class));
Expand All @@ -118,17 +118,17 @@ public function test_validates_if_real(): void
->andReturn(
$this->fulfilledResponse([
'success' => true,
'score' => 0.5,
'foo' => 'bar',
'score' => 0.5,
'foo' => 'bar',
])
);

$this->post('v3/default', [ReCaptcha::INPUT => 'token'])
->assertOk()
->assertExactJson([
'success' => true,
'score' => 0.5,
'foo' => 'bar',
'score' => 0.5,
'foo' => 'bar',
]);
}

Expand Down Expand Up @@ -437,7 +437,7 @@ public function test_checks_for_human_score(): void
->with(
ReCaptcha::SERVER_ENDPOINT,
[
'secret' => 'secret',
'secret' => 'secret',
'response' => 'token',
'remoteip' => '127.0.0.1',
]
Expand All @@ -446,7 +446,7 @@ public function test_checks_for_human_score(): void
->andReturn(
$this->fulfilledPromise([
'success' => true,
'score' => 0.5,
'score' => 0.5,
])
);

Expand Down
12 changes: 6 additions & 6 deletions tests/Http/ReCaptchaResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function test_checks_resolve(): void
new Response(
new GuzzleResponse(200, ['Content-type' => 'application/json'], json_encode([
'success' => true,
'foo' => 'bar',
'foo' => 'bar',
], JSON_THROW_ON_ERROR))
)
);
Expand All @@ -80,7 +80,7 @@ public function test_always_returns_human_if_not_score_response(): void
{
$response = $this->fulfilledResponse([
'success' => true,
'foo' => 'bar',
'foo' => 'bar',
]);

static::assertTrue($response->isHuman());
Expand All @@ -90,8 +90,8 @@ public function test_always_returns_human_if_not_score_response(): void
public function test_returns_carbon_of_challenge_ts(): void
{
$response = $this->fulfilledResponse([
'success' => true,
'foo' => 'bar',
'success' => true,
'foo' => 'bar',
'challenge_ts' => ($now = now())->toIso8601ZuluString(),
]);

Expand Down Expand Up @@ -127,8 +127,8 @@ public function test_validation_fails_if_no_success(): void

$response = $this->fulfilledResponse([
'success' => false,
'foo' => 'bar',
'errors' => ['quz', 'cougar'],
'foo' => 'bar',
'errors' => ['quz', 'cougar'],
]);

try {
Expand Down
Loading

0 comments on commit 71917ce

Please sign in to comment.