Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 06d8ad5

Browse files
committed
Merge branch 'develop' into release/v3
* develop: new ReCaptchaConfigurationTest test class getIpWhitelist method becomes public Default validate method response is of type array when reCAPTCHA v3 is used
2 parents bf7be19 + ef4a928 commit 06d8ad5

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

src/ReCaptchaBuilder.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,27 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder {
110110
return $this;
111111
}
112112

113+
/**
114+
* @return array|mixed
115+
*/
116+
public function getIpWhitelist() {
117+
$whitelist = config('recaptcha.skip_ip', []);
118+
119+
if(!is_array($whitelist)) {
120+
$whitelist = explode(',', $whitelist);
121+
}
122+
123+
return $whitelist;
124+
}
125+
113126
/**
114127
* Checks whether the user IP address is among IPs "to be skipped"
115128
*
116129
* @return boolean
117130
*/
118131
public function skipByIp(): bool {
119132

120-
return (in_array(request()->ip(), config('recaptcha.skip_ip', [])));
133+
return (in_array(request()->ip(), $this->getIpWhitelist()));
121134
}
122135

123136
/**
@@ -221,6 +234,15 @@ public function htmlScriptTagJsApiV3(?array $configuration = []): string {
221234
public function validate($response) {
222235

223236
if ($this->skip_by_ip) {
237+
if ($this->returnArray()) {
238+
// Add 'skip_by_ip' field to response
239+
return [
240+
'skip_by_ip' => true,
241+
'score' => 0.9,
242+
'success' => true
243+
];
244+
}
245+
224246
return true;
225247
}
226248

@@ -243,16 +265,34 @@ public function validate($response) {
243265
else {
244266
$curl_response = file_get_contents($url);
245267
}
268+
246269
if (is_null($curl_response) || empty($curl_response)) {
270+
if ($this->returnArray()) {
271+
// Add 'error' field to response
272+
return [
273+
'error' => 'cURL response empty',
274+
'score' => 0.1,
275+
'success' => false
276+
];
277+
}
278+
247279
return false;
248280
}
249281
$response = json_decode(trim($curl_response), true);
250282

251-
if ($this->version == 'v3') {
283+
if ($this->returnArray()) {
252284
return $response;
253285
}
254286

255287
return $response['success'];
256288

257289
}
290+
291+
/**
292+
* @return bool
293+
*/
294+
protected function returnArray(): bool {
295+
296+
return ($this->version == 'v3');
297+
}
258298
}

tests/ReCaptchaConfigurationTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 - present
4+
* LaravelGoogleRecaptcha - ReCaptchaConfigurationTest.php
5+
* author: Roberto Belotti - [email protected]
6+
* web : robertobelotti.com, github.com/biscolab
7+
* Initial version created on: 13/2/2019
8+
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
9+
*/
10+
11+
namespace Biscolab\ReCaptcha\Tests;
12+
13+
use Biscolab\ReCaptcha\Controllers\ReCaptchaController;
14+
use Biscolab\ReCaptcha\ReCaptchaBuilder;
15+
use Biscolab\ReCaptcha\ReCaptchaBuilderV2;
16+
use Biscolab\ReCaptcha\ReCaptchaBuilderV3;
17+
use Illuminate\Support\Facades\App;
18+
19+
/**
20+
* Class ReCaptchaConfigurationTest
21+
* @package Biscolab\ReCaptcha\Tests
22+
*/
23+
class ReCaptchaConfigurationTest extends TestCase {
24+
25+
/**
26+
* @var ReCaptchaBuilder
27+
*/
28+
protected $recaptcha;
29+
30+
/**
31+
* @test
32+
*/
33+
public function testSkipIpWhiteListIsArray() {
34+
35+
$ip_whitelist = $this->recaptcha->getIpWhitelist();
36+
$this->assertTrue(is_array($ip_whitelist));
37+
$this->assertCount(2, $ip_whitelist);
38+
}
39+
40+
/**
41+
* Define environment setup.
42+
*
43+
* @param \Illuminate\Foundation\Application $app
44+
*
45+
* @return void
46+
*/
47+
protected function getEnvironmentSetUp($app) {
48+
49+
$app['config']->set('recaptcha.skip_ip', '10.0.0.1,10.0.0.2');
50+
}
51+
52+
/**
53+
* Setup the test environment.
54+
*/
55+
protected function setUp() {
56+
57+
parent::setUp(); // TODO: Change the autogenerated stub
58+
59+
$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
60+
}
61+
}

0 commit comments

Comments
 (0)