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 9c3db11
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 64 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.',
];
10 changes: 6 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
Expand All @@ -17,4 +14,9 @@
<env name="APP_DEBUG" value="true"/>
<env name="APP_KEY" value="base64:Q8Ey6mJ7UJ26St3s5XrJa62zfjRzJQEie8NBBc0aJqk="/>
</php>
<source>
<include>
<directory>src/</directory>
</include>
</source>
</phpunit>
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
4 changes: 2 additions & 2 deletions src/Http/Middleware/VerifyReCaptchaV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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);

Expand All @@ -110,7 +110,7 @@ protected function storeRememberInSession(string|int $offset): void

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

session()->put($this->rememberKey(), $offset);
Expand Down
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
8 changes: 0 additions & 8 deletions src/Http/ReCaptchaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class ReCaptchaResponse implements JsonSerializable, Arrayable, Jsonable

/**
* The data from the ReCaptcha response.
*
* @var array{success: bool, hostname: string, challenge_ts: int, apk_package_name: string, action: string, score: float, error_codes: string[]}
*/
protected array $attributes = [];

Expand Down Expand Up @@ -94,8 +92,6 @@ public function terminate(): void

/**
* Returns the raw attributes of the response, bypassing the promise resolving.
*
* @return array{success: bool, hostname: string, challenge_ts: int, apk_package_name: string, action: string, score: float, error_codes: string[]}
*/
public function getAttributes(): array
{
Expand All @@ -114,8 +110,6 @@ public function get(string $key, mixed $default = null): mixed

/**
* Convert the instance to an array.
*
* @return array{success: bool, hostname: string, challenge_ts: int, apk_package_name: string, action: string, score: float, error_codes: string[]}
*/
public function toArray(): array
{
Expand All @@ -126,8 +120,6 @@ public function toArray(): array

/**
* Convert the object into something JSON serializable.
*
* @return array{success: bool, hostname: string, challenge_ts: int, apk_package_name: string, action: string, score: float, error_codes: string[]}
*/
public function jsonSerialize(): array
{
Expand Down
3 changes: 2 additions & 1 deletion src/Http/ValidatesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function validate(): void
if (Arr::get($this->attributes, 'success') !== true) {
throw $this->validationException([
$this->input => trans('recaptcha::validation.error', [
'errors' => implode(', ', Arr::wrap($this->attributes['errors'] ?? [])),
'errors' => implode(', ', (array) Arr::get($this->attributes, 'errors')),
]),
]);
}
Expand All @@ -38,6 +38,7 @@ public function validate(): void
}

if (! empty($errors)) {
// @phpstan-ignore-next-line
throw $this->validationException([$this->input => $errors]);
}
}
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 9c3db11

Please sign in to comment.