From 251594d5cd1f1cd309f7a1e5a56e96994ab101ed Mon Sep 17 00:00:00 2001 From: RyanDaDeng Date: Sun, 29 Aug 2021 20:53:56 +1000 Subject: [PATCH] Fixed lang for url, and fixed typo and backward compitablity --- README.md | 6 +++--- config/googlerecaptchav3.php | 4 ++-- src/Services/GoogleReCaptchaV3Service.php | 12 +++++++++--- tests/RequestTest.php | 4 ++-- tests/ScoreTest.php | 20 ++++++++++---------- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ab4123d..0005f64 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ This package requires the following dependencies: Via Composer ``` sh - $ composer require timehunter/laravel-google-recaptcha-v3 "~2.4.4" -vvv + $ composer require timehunter/laravel-google-recaptcha-v3 "~2.5" -vvv ``` If your Laravel framework version <= 5.4, please register the service provider under your config file: /config/app.php, otherwise please skip it. @@ -156,12 +156,12 @@ Specify your Score threshold and action in 'setting', e.g. [ 'action' => 'contact_us', // Google reCAPTCHA required paramater 'threshold' => 0.2, // score threshold - 'score_comparision' => false // if this is true, the system will do score comparsion against your threshold for the action + 'score_comparison' => false // if this is true, the system will do score comparsion against your threshold for the action ], [ 'action' => 'signup', 'threshold' => 0.2, - 'score_comparision' => true + 'score_comparison' => true ], ] diff --git a/config/googlerecaptchav3.php b/config/googlerecaptchav3.php index cf14fe4..22120e9 100644 --- a/config/googlerecaptchav3.php +++ b/config/googlerecaptchav3.php @@ -105,13 +105,13 @@ | Define your score threshold, define your action | action: Google reCAPTCHA required parameter | threshold: score threshold - | score_comparision: true/false, if this is true, the system will do score comparision against your threshold for the action + | score_comparison: true/false, if this is true, the system will do score comparision against your threshold for the action */ 'setting' => [ [ 'action' => 'contact_us', 'threshold' => 0, - 'score_comparision' => false, + 'score_comparison' => false, ], ], diff --git a/src/Services/GoogleReCaptchaV3Service.php b/src/Services/GoogleReCaptchaV3Service.php index 81530fc..203aae0 100644 --- a/src/Services/GoogleReCaptchaV3Service.php +++ b/src/Services/GoogleReCaptchaV3Service.php @@ -44,7 +44,7 @@ public function ifInSkippedIps($ip) */ public function verifyResponse($response, $ip = null) { - if (! $this->config->isServiceEnabled() || ($ip && $this->ifInSkippedIps($ip)) === true) { + if (!$this->config->isServiceEnabled() || ($ip && $this->ifInSkippedIps($ip)) === true) { $res = new GoogleReCaptchaV3Response([], $ip); $res->setSuccess(true); @@ -85,7 +85,7 @@ public function verifyResponse($response, $ip = null) return $rawResponse; } - if (! empty($this->config->getHostName()) && strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) { + if (!empty($this->config->getHostName()) && strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) { $rawResponse->setMessage(Lang::get(GoogleReCaptchaV3Response::ERROR_HOSTNAME)); $rawResponse->setSuccess(false); @@ -107,12 +107,18 @@ public function verifyResponse($response, $ip = null) } else { if ($this->getConfig()->isScoreEnabled()) { $count = collect($this->getConfig()->getSetting()) + ->where('action', '=', $rawResponse->getAction()) + ->where('score_comparison', '=', true) + ->where('threshold', '>', $rawResponse->getScore()) + ->count(); + + $oldCount = collect($this->getConfig()->getSetting()) ->where('action', '=', $rawResponse->getAction()) ->where('score_comparision', '=', true) ->where('threshold', '>', $rawResponse->getScore()) ->count(); - if ($count > 0) { + if ($count > 0 || $oldCount > 0) { $rawResponse->setSuccess(false); $rawResponse->setMessage(Lang::get(GoogleReCaptchaV3Response::ERROR_SCORE_THRESHOLD)); diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 8cf1bd4..0120273 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -21,7 +21,7 @@ public function testCurlRequest() $response = new GoogleReCaptchaV3Response(json_decode($response, 1), null, ''); $this->assertEquals(false, $response->isSuccess()); - $this->assertEquals(2, count($response->getErrorCodes())); + $this->assertEquals(1, count($response->getErrorCodes())); } public function testCurlRequest2() @@ -50,7 +50,7 @@ public function testGuzzleRequest() $response = new GoogleReCaptchaV3Response(json_decode($response, 1), null, ''); $this->assertEquals(false, $response->toArray()['success']); - $this->assertEquals(2, count($response->getErrorCodes())); + $this->assertEquals(1, count($response->getErrorCodes())); } public function testGuzzleRequest2() diff --git a/tests/ScoreTest.php b/tests/ScoreTest.php index f542248..280e804 100644 --- a/tests/ScoreTest.php +++ b/tests/ScoreTest.php @@ -28,7 +28,7 @@ public function testScore1() [ 'action' => 'contact_us', 'threshold' => 1, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -63,7 +63,7 @@ public function testScore2() [ 'action' => 'contact_us', 'threshold' => 1, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -97,7 +97,7 @@ public function testScore3() [ 'action' => 'contact_us', 'threshold' => 1, - 'score_comparision' => false, + 'score_comparison' => false, ], ]); @@ -131,7 +131,7 @@ public function testScore4() [ 'action' => 'contact_us', 'threshold' => 0.9, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -165,7 +165,7 @@ public function testScore5() [ 'action' => 'contact_us', 'threshold' => 0.91, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -200,7 +200,7 @@ public function testScore6() [ 'action' => 'contact_us_test', 'threshold' => 0.91, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -234,7 +234,7 @@ public function testScore7() [ 'action' => 'contact_us_test', 'threshold' => 0.91, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -268,7 +268,7 @@ public function testScore11() [ 'action' => 'contact_us', 'threshold' => 0.91, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -302,7 +302,7 @@ public function testScore8() [ 'action' => 'contact_us', 'threshold' => 0.91, - 'score_comparision' => true, + 'score_comparison' => true, ], ]); @@ -336,7 +336,7 @@ public function testScore12() [ 'action' => 'contact_us', 'threshold' => 0.6, - 'score_comparision' => true, + 'score_comparison' => true, ], ]);