Skip to content

Commit

Permalink
Merge branch 'release/v3'
Browse files Browse the repository at this point in the history
* release/v3:
  HHVM removed from travis yml
  Version renamed
  deprecated array_get helper replaced with Arr::get
  new ReCaptchaBuilder class constants: DEFAULT_API_VERSION and DEFAULT_CURL_TIMEOUT
  Update recaptcha.php
  make curl timeout of server token validation call configurable
  • Loading branch information
biscolab committed Jul 22, 2019
2 parents f3b5aa1 + 457939f commit 2636b05
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 25 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Expand Up @@ -4,14 +4,9 @@ php:
- 7.1
- 7.2
- 7.3
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction
- curl -sSfL -o ~/.phpenv/versions/hhvm/bin/phpunit https://phar.phpunit.de/phpunit-5.7.phar

matrix:
allow_failures:
- php: hhvm
fast_finish: true
10 changes: 9 additions & 1 deletion config/recaptcha.php
Expand Up @@ -39,6 +39,14 @@
*/
'version' => env('RECAPTCHA_DEFAULT_VERSION', 'v2'),

/**
*
* The curl timout in seconds to validate a recaptcha token
* @since v3.5.0
*
*/
'curl_timeout' => env('RECAPTCHA_CURL_TIMEOUT', 10),

/**
*
* IP addresses for which validation will be skipped
Expand All @@ -61,4 +69,4 @@
*
*/
'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token')
];
];
75 changes: 67 additions & 8 deletions src/ReCaptchaBuilder.php
Expand Up @@ -11,9 +11,24 @@
namespace Biscolab\ReCaptcha;

use Exception;
use Illuminate\Support\Arr;

/**
* Class ReCaptchaBuilder
* @package Biscolab\ReCaptcha
*/
class ReCaptchaBuilder {

/**
* @var string
*/
const DEFAULT_API_VERSION = 'v2';

/**
* @var int
*/
const DEFAULT_CURL_TIMEOUT = 10;

/**
* The Site key
* please visit https://developers.google.com/recaptcha/docs/start
Expand All @@ -35,6 +50,13 @@ class ReCaptchaBuilder {
*/
protected $version;

/**
* The curl timeout
* please visit https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html
* @var int
*/
protected $curl_timeout;

/**
* Whether is true the ReCAPTCHA is inactive
* @var boolean
Expand All @@ -46,11 +68,25 @@ class ReCaptchaBuilder {
*/
protected $api_url = 'https://www.google.com/recaptcha/api/siteverify';

public function __construct($api_site_key, $api_secret_key, $version = 'v2') {
/**
* ReCaptchaBuilder constructor.
*
* @param string $api_site_key
* @param string $api_secret_key
* @param null|string $version
* @param int|null $curl_timeout
*/
public function __construct(
string $api_site_key,
string $api_secret_key,
?string $version = self::DEFAULT_API_VERSION,
?int $curl_timeout = self::DEFAULT_CURL_TIMEOUT
) {

$this->setApiSiteKey($api_site_key);
$this->setApiSecretKey($api_secret_key);
$this->setVersion($version);
$this->setCurlTimeout($curl_timeout);
$this->setSkipByIp($this->skipByIp());
}

Expand Down Expand Up @@ -78,6 +114,26 @@ public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder {
return $this;
}

/**
* @param int $curl_timeout
*
* @return ReCaptchaBuilder
*/
public function setCurlTimeout(int $curl_timeout): ReCaptchaBuilder {

$this->curl_timeout = $curl_timeout;

return $this;
}

/**
* @return int
*/
public function getCurlTimeout(): int {

return $this->curl_timeout;
}

/**
* @param string $version
*
Expand Down Expand Up @@ -114,9 +170,10 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder {
* @return array|mixed
*/
public function getIpWhitelist() {

$whitelist = config('recaptcha.skip_ip', []);

if(!is_array($whitelist)) {
if (!is_array($whitelist)) {
$whitelist = explode(',', $whitelist);
}

Expand Down Expand Up @@ -169,9 +226,9 @@ function biscolabLaravelReCaptcha(token) {
}
elseif ($this->version == 'v3') {

$action = array_get($configuration, 'action', 'homepage');
$action = Arr::get($configuration, 'action', 'homepage');

$js_custom_validation = array_get($configuration, 'custom_validation', '');
$js_custom_validation = Arr::get($configuration, 'custom_validation', '');

// Check if set custom_validation. That function will override default fetch validation function
if ($js_custom_validation) {
Expand All @@ -180,14 +237,16 @@ function biscolabLaravelReCaptcha(token) {
}
else {

$js_then_callback = array_get($configuration, 'callback_then', '');
$js_callback_catch = array_get($configuration, 'callback_catch', '');
$js_then_callback = Arr::get($configuration, 'callback_then', '');
$js_callback_catch = Arr::get($configuration, 'callback_catch', '');

$js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : '';
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';

$validate_function = "
fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, {
fetch('/" . config('recaptcha.default_validation_route',
'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name',
'token') . "=' + token, {
headers: {
\"X-Requested-With\": \"XMLHttpRequest\",
\"X-CSRF-TOKEN\": csrfToken.content
Expand Down Expand Up @@ -258,7 +317,7 @@ public function validate($response) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curl_timeout);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
}
Expand Down
5 changes: 3 additions & 2 deletions src/ReCaptchaBuilderInvisible.php
Expand Up @@ -21,10 +21,11 @@ class ReCaptchaBuilderInvisible extends ReCaptchaBuilder {
*
* @param string $api_site_key
* @param string $api_secret_key
* @param int $curl_timeout
*/
public function __construct(string $api_site_key, string $api_secret_key) {
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {

parent::__construct($api_site_key, $api_secret_key, 'invisible');
parent::__construct($api_site_key, $api_secret_key, 'invisible', $curl_timeout);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/ReCaptchaBuilderV2.php
Expand Up @@ -21,10 +21,11 @@ class ReCaptchaBuilderV2 extends ReCaptchaBuilder {
*
* @param string $api_site_key
* @param string $api_secret_key
* @param int $curl_timeout
*/
public function __construct(string $api_site_key, string $api_secret_key) {
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {

parent::__construct($api_site_key, $api_secret_key, 'v2');
parent::__construct($api_site_key, $api_secret_key, 'v2', $curl_timeout);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/ReCaptchaBuilderV3.php
Expand Up @@ -21,10 +21,11 @@ class ReCaptchaBuilderV3 extends ReCaptchaBuilder {
*
* @param string $api_site_key
* @param string $api_secret_key
* @param int $curl_timeout
*/
public function __construct(string $api_site_key, string $api_secret_key) {
public function __construct(string $api_site_key, string $api_secret_key, int $curl_timeout) {

parent::__construct($api_site_key, $api_secret_key, 'v3');
parent::__construct($api_site_key, $api_secret_key, 'v3', $curl_timeout);
}

}
2 changes: 1 addition & 1 deletion src/ReCaptchaServiceProvider.php
Expand Up @@ -114,7 +114,7 @@ protected function registerReCaptchaBuilder() {
break;
}

return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'));
return new $recaptcha_class(config('recaptcha.api_site_key'), config('recaptcha.api_secret_key'), config('recaptcha.curl_timeout'));

});
}
Expand Down
9 changes: 8 additions & 1 deletion tests/ReCaptchaConfigurationTest.php
Expand Up @@ -37,6 +37,13 @@ public function testSkipIpWhiteListIsArray() {
$this->assertCount(2, $ip_whitelist);
}

/**
* @test
*/
public function testCurlTimeoutIsSet() {
$this->assertEquals($this->recaptcha->getCurlTimeout(), 3);
}

/**
* Define environment setup.
*
Expand All @@ -56,6 +63,6 @@ protected function setUp(): void {

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

$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key', 3);
}
}
4 changes: 2 additions & 2 deletions tests/ReCaptchaTest.php
Expand Up @@ -85,8 +85,8 @@ protected function setUp(): void {

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

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

}
}
2 changes: 1 addition & 1 deletion tests/ReCaptchaV3Test.php
Expand Up @@ -106,7 +106,7 @@ protected function setUp(): void {

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

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

}
}

0 comments on commit 2636b05

Please sign in to comment.