Skip to content

Commit 81d3ff0

Browse files
committed
added skip ip support
1 parent 64fa595 commit 81d3ff0

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

config/googlerecaptchav3.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@
9191
'score_comparision' => false,
9292
],
9393
],
94+
95+
/*
96+
|--------------------------------------------------------------------------
97+
| Setting
98+
|--------------------------------------------------------------------------
99+
| Type: array
100+
| Define a list of ip that you want to skip
101+
*/
102+
'skip_ips'=>[
103+
104+
],
94105
/*
95106
|--------------------------------------------------------------------------
96107
| Options

src/Configurations/ReCaptchaConfigV3.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@ public function getLanguage()
9393
{
9494
return config('googlerecaptchav3.language');
9595
}
96+
97+
/**
98+
* @return string
99+
*/
100+
public function getSkipIps()
101+
{
102+
return config('googlerecaptchav3.skip_ips');
103+
}
96104
}

src/Interfaces/ReCaptchaConfigV3Interface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ public function isInline();
6464
* @return string
6565
*/
6666
public function getLanguage();
67+
68+
/**
69+
* @return array
70+
*/
71+
public function getSkipIps();
6772
}

src/Services/GoogleReCaptchaV3Service.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@ public function __construct(ReCaptchaConfigV3Interface $config, RequestClientInt
2525
$this->requestClient = $requestClient;
2626
}
2727

28+
/**
29+
* @param $ip
30+
* @return bool
31+
*/
32+
public function ifInSkippedIps($ip)
33+
{
34+
$ips = $this->config->getSkipIps();
35+
return in_array($ip, $ips);
36+
}
37+
2838
/**
2939
* @param $response
3040
* @param null $ip
3141
* @return GoogleReCaptchaV3Response
3242
*/
3343
public function verifyResponse($response, $ip = null)
3444
{
35-
if (! $this->config->isServiceEnabled()) {
45+
if (!$this->config->isServiceEnabled() || ($ip && $this->ifInSkippedIps($ip)) === true) {
3646
$res = new GoogleReCaptchaV3Response([], $ip);
3747
$res->setSuccess(true);
3848

3949
return $res;
4050
}
4151

52+
4253
if (empty($response)) {
4354
$res = new GoogleReCaptchaV3Response([], $ip, GoogleReCaptchaV3Response::MISSING_INPUT_ERROR);
4455
$res->setSuccess(false);
@@ -67,7 +78,7 @@ public function verifyResponse($response, $ip = null)
6778
return $rawResponse;
6879
}
6980

70-
if (! empty($this->config->getHostName()) && strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) {
81+
if (!empty($this->config->getHostName()) && strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) {
7182
$rawResponse->setMessage(GoogleReCaptchaV3Response::ERROR_HOSTNAME);
7283
$rawResponse->setSuccess(false);
7384

tests/ConfigTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,57 @@ public function testActionSkip()
220220
$response = $service->verifyResponse('test response');
221221
$this->assertEquals(true, $response->isSuccess());
222222
}
223+
224+
225+
226+
public function testIpSkip1()
227+
{
228+
// Create a stub for the SomeClass class.
229+
$configStub = $this->createMock(ReCaptchaConfigV3::class);
230+
231+
// Configure the stub.
232+
$configStub->method('isServiceEnabled')
233+
->willReturn(true);
234+
235+
$configStub->method('getSkipIps')
236+
->willReturn(['1.1.1.1']);
237+
238+
$testJson = '{ "success": false, "challenge_ts": "2018-12-25T03:35:32Z", "hostname": "ryandeng.test", "score": 0.9, "action": "contact_us" }';
239+
240+
$clientStub = $this->createMock(GuzzleRequestClient::class);
241+
$clientStub->method('post')
242+
->willReturn($testJson);
243+
244+
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
245+
$service = new GoogleReCaptchaV3($_service);
246+
247+
$response = $service->verifyResponse('test response','1.1.1.1');
248+
$this->assertEquals(true, $response->isSuccess());
249+
}
250+
251+
252+
public function testIpSkip2()
253+
{
254+
// Create a stub for the SomeClass class.
255+
$configStub = $this->createMock(ReCaptchaConfigV3::class);
256+
257+
// Configure the stub.
258+
$configStub->method('isServiceEnabled')
259+
->willReturn(true);
260+
261+
$configStub->method('getSkipIps')
262+
->willReturn(['1.1.1.1']);
263+
264+
$testJson = '{ "success": false, "challenge_ts": "2018-12-25T03:35:32Z", "hostname": "ryandeng.test", "score": 0.9, "action": "contact_us" }';
265+
266+
$clientStub = $this->createMock(GuzzleRequestClient::class);
267+
$clientStub->method('post')
268+
->willReturn($testJson);
269+
270+
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
271+
$service = new GoogleReCaptchaV3($_service);
272+
273+
$response = $service->verifyResponse('test response','1.1.1.2');
274+
$this->assertEquals(false, $response->isSuccess());
275+
}
223276
}

0 commit comments

Comments
 (0)