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

Commit 62f9e6e

Browse files
authored
Merge pull request #23 from matthiasgrube/curl_timeout
make curl timeout configurable
2 parents 87b8419 + 78e3d13 commit 62f9e6e

9 files changed

+59
-14
lines changed

config/recaptcha.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
*/
4040
'version' => env('RECAPTCHA_DEFAULT_VERSION', 'v2'),
4141

42+
/**
43+
*
44+
* The curl timout in seconds to validate a recaptcha token
45+
* @since v3.4.3
46+
*
47+
*/
48+
'curl_timeout' => env('RECAPTCHA_CURL_TIMEOUT', 10),
49+
4250
/**
4351
*
4452
* IP addresses for which validation will be skipped
@@ -61,4 +69,4 @@
6169
*
6270
*/
6371
'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token')
64-
];
72+
];

src/ReCaptchaBuilder.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class ReCaptchaBuilder {
3535
*/
3636
protected $version;
3737

38+
/**
39+
* The curl timeout
40+
* please visit https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html
41+
* @var int
42+
*/
43+
protected $curl_timeout;
44+
3845
/**
3946
* Whether is true the ReCAPTCHA is inactive
4047
* @var boolean
@@ -46,11 +53,12 @@ class ReCaptchaBuilder {
4653
*/
4754
protected $api_url = 'https://www.google.com/recaptcha/api/siteverify';
4855

49-
public function __construct($api_site_key, $api_secret_key, $version = 'v2') {
56+
public function __construct($api_site_key, $api_secret_key, $version = 'v2', $curl_timeout = 3) {
5057

5158
$this->setApiSiteKey($api_site_key);
5259
$this->setApiSecretKey($api_secret_key);
5360
$this->setVersion($version);
61+
$this->setCurlTimeout($curl_timeout);
5462
$this->setSkipByIp($this->skipByIp());
5563
}
5664

@@ -78,6 +86,25 @@ public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder {
7886
return $this;
7987
}
8088

89+
/**
90+
* @param int $curl_timeout
91+
* @return ReCaptchaBuilder
92+
*/
93+
public function setCurlTimeout(int $curl_timeout): ReCaptchaBuilder {
94+
95+
$this->curl_timeout = $curl_timeout;
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* @return int
102+
*/
103+
public function getCurlTimeout(): int {
104+
105+
return $this->curl_timeout;
106+
}
107+
81108
/**
82109
* @param string $version
83110
*
@@ -258,7 +285,7 @@ public function validate($response) {
258285
$curl = curl_init($url);
259286
curl_setopt($curl, CURLOPT_HEADER, false);
260287
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
261-
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
288+
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curl_timeout);
262289
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
263290
$curl_response = curl_exec($curl);
264291
}

src/ReCaptchaBuilderInvisible.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class ReCaptchaBuilderInvisible extends ReCaptchaBuilder {
2121
*
2222
* @param string $api_site_key
2323
* @param string $api_secret_key
24+
* @param int $curl_timeout
2425
*/
25-
public function __construct(string $api_site_key, string $api_secret_key) {
26+
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {
2627

27-
parent::__construct($api_site_key, $api_secret_key, 'invisible');
28+
parent::__construct($api_site_key, $api_secret_key, 'invisible', $curl_timeout);
2829
}
2930

3031
/**

src/ReCaptchaBuilderV2.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class ReCaptchaBuilderV2 extends ReCaptchaBuilder {
2121
*
2222
* @param string $api_site_key
2323
* @param string $api_secret_key
24+
* @param int $curl_timeout
2425
*/
25-
public function __construct(string $api_site_key, string $api_secret_key) {
26+
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {
2627

27-
parent::__construct($api_site_key, $api_secret_key, 'v2');
28+
parent::__construct($api_site_key, $api_secret_key, 'v2', $curl_timeout);
2829
}
2930

3031
/**

src/ReCaptchaBuilderV3.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class ReCaptchaBuilderV3 extends ReCaptchaBuilder {
2121
*
2222
* @param string $api_site_key
2323
* @param string $api_secret_key
24+
* @param int $curl_timeout
2425
*/
25-
public function __construct(string $api_site_key, string $api_secret_key) {
26+
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {
2627

27-
parent::__construct($api_site_key, $api_secret_key, 'v3');
28+
parent::__construct($api_site_key, $api_secret_key, 'v3', $curl_timeout);
2829
}
2930

3031
}

src/ReCaptchaServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function registerReCaptchaBuilder() {
114114
break;
115115
}
116116

117-
return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'));
117+
return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'), config('recaptcha.curl_timeout'));
118118

119119
});
120120
}

tests/ReCaptchaConfigurationTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public function testSkipIpWhiteListIsArray() {
3737
$this->assertCount(2, $ip_whitelist);
3838
}
3939

40+
/**
41+
* @test
42+
*/
43+
public function testCurlTimeoutIsSet() {
44+
$this->assertEquals($this->recaptcha->getCurlTimeout(), 3);
45+
}
46+
4047
/**
4148
* Define environment setup.
4249
*
@@ -56,6 +63,6 @@ protected function setUp(): void {
5663

5764
parent::setUp(); // TODO: Change the autogenerated stub
5865

59-
$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
66+
$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key', 3);
6067
}
6168
}

tests/ReCaptchaTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ protected function setUp(): void {
8585

8686
parent::setUp(); // TODO: Change the autogenerated stub
8787

88-
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key');
89-
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
88+
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key', 3);
89+
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key', 3);
9090

9191
}
9292
}

tests/ReCaptchaV3Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function setUp(): void {
106106

107107
parent::setUp(); // TODO: Change the autogenerated stub
108108

109-
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
109+
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key', 3);
110110

111111
}
112112
}

0 commit comments

Comments
 (0)