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

Commit

Permalink
Merge branch 'release/v4'
Browse files Browse the repository at this point in the history
  • Loading branch information
biscolab committed Sep 14, 2020
2 parents a02da2f + b260807 commit 47c941c
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 35 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Available reCAPTCHA versions:

## System requirements

| Package | reCAPTCHA | PHP | Laravel | Docs |
| ------- | ----------------------------- | --------------------- | ---------------------------- | ---------------------------------------------------------------------- |
| 4.2.x | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6, 7, 8 | [latest](https://laravel-recaptcha-docs.biscolab.com) |
| 4.1.x | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6, 7 | [v4.1.x](https://laravel-recaptcha-docs.biscolab.com/docs/4.1.x/intro) |
| 4.0.x | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6 | [v4.0.x](https://laravel-recaptcha-docs.biscolab.com/docs/4.0.x/intro) |
| 3.x | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6 ready (\*) | [v3.6.1](https://laravel-recaptcha-docs.biscolab.com/docs/3.6.1/intro) |
| 2.x | v2 Invisible, v2 Checkbox | 5.5.9, 7.0 or greater | 5.0 or greater | [v2.0.4](https://laravel-recaptcha-docs.biscolab.com/docs/2.0.4/intro) |
| Package | reCAPTCHA | PHP | Laravel | Docs |
| ---------------- | ----------------------------- | --------------------- | ---------------------------- | ---------------------------------------------------------------------- |
| 4.2.x or greater | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6, 7, 8 | [latest](https://laravel-recaptcha-docs.biscolab.com) |
| 4.1.x | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6, 7 | [v4.1.x](https://laravel-recaptcha-docs.biscolab.com/docs/4.1.x/intro) |
| 4.0.x | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6 | [v4.0.x](https://laravel-recaptcha-docs.biscolab.com/docs/4.0.x/intro) |
| 3.x | v3, v2 Invisible, v2 Checkbox | 7.1 or greater | 5.5 or greater, 6 ready (\*) | [v3.6.1](https://laravel-recaptcha-docs.biscolab.com/docs/3.6.1/intro) |
| 2.x | v2 Invisible, v2 Checkbox | 5.5.9, 7.0 or greater | 5.0 or greater | [v2.0.4](https://laravel-recaptcha-docs.biscolab.com/docs/2.0.4/intro) |

> (\*) Latest version (3.6.1) is Laravel 6 ready
Expand Down
11 changes: 11 additions & 0 deletions config/recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@
*/
'explicit' => false,

/**
*
* Set API domain. You can use "www.recaptcha.net" in case "www.google.com" is not accessible.
* (no check will be made on the entered value)
* @see https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally
* @since v4.3.0
* Default 'www.google.com' (ReCaptchaBuilder::DEFAULT_RECAPTCHA_API_DOMAIN)
*
*/
'api_domain' => 'www.google.com',

/**
*
* g-recaptcha tag attributes and grecaptcha.render parameters (v2 only)
Expand Down
62 changes: 58 additions & 4 deletions src/ReCaptchaBuilder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - ReCaptchaBuilder.php
Expand Down Expand Up @@ -44,6 +45,11 @@ class ReCaptchaBuilder
*/
const DEFAULT_RECAPTCHA_FIELD_NAME = 'g-recaptcha-response';

/**
* @var string
*/
const DEFAULT_RECAPTCHA_API_DOMAIN = 'www.google.com';

/**
* The Site key
* please visit https://developers.google.com/recaptcha/docs/start
Expand Down Expand Up @@ -71,10 +77,23 @@ class ReCaptchaBuilder
*/
protected $skip_by_ip = false;

/**
* The API domain (default: retrieved from config file)
* @var string
*/
protected $api_domain = '';

/**
* The API request URI
* @var string
*/
protected $api_url = 'https://www.google.com/recaptcha/api/siteverify';
protected $api_url = '';

/**
* The URI of the API Javascript file to embed in you pages
* @var string
*/
protected $api_js_url = '';

/**
* ReCaptchaBuilder constructor.
Expand All @@ -93,6 +112,8 @@ public function __construct(
$this->setApiSecretKey($api_secret_key);
$this->setVersion($version);
$this->setSkipByIp($this->skipByIp());
$this->setApiDomain();
$this->setApiUrls();
}

/**
Expand Down Expand Up @@ -165,6 +186,40 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder
return $this;
}

/**
* @param null|string $api_domain
*
* @return ReCaptchaBuilder
*/
public function setApiDomain(?string $api_domain = null): ReCaptchaBuilder
{

$this->api_domain = $api_domain ?? config('recaptcha.api_domain', self::DEFAULT_RECAPTCHA_API_DOMAIN);

return $this;
}

/**
* @return string
*/
public function getApiDomain(): string
{

return $this->api_domain;
}

/**
* @return ReCaptchaBuilder
*/
public function setApiUrls(): ReCaptchaBuilder
{

$this->api_url = 'https://' . $this->api_domain . '/recaptcha/api/siteverify';
$this->api_js_url = 'https://' . $this->api_domain . '/recaptcha/api.js';

return $this;
}

/**
* @return array|mixed
*/
Expand Down Expand Up @@ -233,7 +288,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string

// Create query string
$query = ($query) ? '?' . http_build_query($query) : "";
$html .= "<script src=\"https://www.google.com/recaptcha/api.js" . $query . "\" async defer></script>";
$html .= "<script src=\"" . $this->api_js_url . $query . "\" async defer></script>";

return $html;
}
Expand Down Expand Up @@ -300,7 +355,6 @@ public function validate($response)
}

return $response['success'];

}

/**
Expand Down Expand Up @@ -329,4 +383,4 @@ protected function returnArray(): bool

return ($this->version == 'v3');
}
}
}
16 changes: 10 additions & 6 deletions src/ReCaptchaBuilderV3.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - ReCaptchaBuilderV3.php
Expand Down Expand Up @@ -46,7 +47,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
return '';
}

$html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>";
$html = "<script src=\"" . $this->api_js_url . "?render={$this->api_site_key}\"></script>";

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

Expand All @@ -65,9 +66,13 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
$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 All @@ -92,5 +97,4 @@ public function htmlScriptTagJsApi(?array $configuration = []): string

return $html;
}

}
}
81 changes: 81 additions & 0 deletions tests/ReCaptchaCustomApiDomainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - ReCaptchaCustomApiDomainTest.php
* author: Roberto Belotti - [email protected]
* web : robertobelotti.com, github.com/biscolab
* Initial version created on: 13/9/2020
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
*/

namespace Biscolab\ReCaptcha\Tests;

use Biscolab\ReCaptcha\ReCaptchaBuilderInvisible;
use Biscolab\ReCaptcha\ReCaptchaBuilderV2;
use Biscolab\ReCaptcha\ReCaptchaBuilderV3;

class ReCaptchaCustomApiDomainTest extends TestCase
{

/**
* @var ReCaptchaBuilderInvisible
*/
protected $recaptcha_invisible;

/**
* @var ReCaptchaBuilderV2
*/
protected $recaptcha_v2;

/**
* @var ReCaptchaBuilderV3
*/
protected $recaptcha_v3;

/**
* @test
*/
public function testRecaptchaApiDomainChangesByConfig()
{
$this->app['config']->set('recaptcha.api_domain', 'www.recaptcha.net');
$this->assertEquals("www.recaptcha.net", $this->recaptcha_v2->getApiDomain());
$this->assertEquals("www.recaptcha.net", $this->recaptcha_invisible->getApiDomain());
$this->assertEquals("www.recaptcha.net", $this->recaptcha_v3->getApiDomain());
}

/**
* @test
*/
public function testRecaptchaApiDomainChangesByConfigInHtmlScriptTagJsApi()
{
$this->assertContains("https://www.recaptcha.net/recaptcha/api.js", $this->recaptcha_v2->htmlScriptTagJsApi());
$this->assertContains("https://www.recaptcha.net/recaptcha/api.js", $this->recaptcha_invisible->htmlScriptTagJsApi());
$this->assertContains("https://www.recaptcha.net/recaptcha/api.js", $this->recaptcha_v3->htmlScriptTagJsApi());
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{

$app['config']->set('recaptcha.api_domain', 'www.recaptcha.net');
}

/**
* @inheritdoc
*/
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_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
}
}
52 changes: 34 additions & 18 deletions tests/ReCaptchaTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - ReCaptchaTest.php
Expand Down Expand Up @@ -56,8 +57,10 @@ public function testReCaptchaInvisibleHtmlFormButtonDefault()

$recaptcha = $this->recaptcha_invisible;
$html_button = $recaptcha->htmlFormButton();
$this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Submit</button>',
$html_button);
$this->assertEquals(
'<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Submit</button>',
$html_button
);
}

/**
Expand All @@ -68,8 +71,10 @@ public function testReCaptchaInvisibleHtmlFormButtonCustom()

$recaptcha = $this->recaptcha_invisible;
$html_button = $recaptcha->htmlFormButton('Custom Text');
$this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Custom Text</button>',
$html_button);
$this->assertEquals(
'<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Custom Text</button>',
$html_button
);
}

/**
Expand Down Expand Up @@ -156,19 +161,31 @@ public function testReCaptchaV2htmlFormButtonShouldThrowError()
$this->recaptcha_v2->htmlFormButton();
}

/**
* @test
*/
public function testRecaptchaFieldNameHelperReturnsReCaptchaBuilderDefaultFieldName() {
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME, recaptchaFieldName());
}
/**
* @test
*/
public function testRecaptchaFieldNameHelperReturnsReCaptchaBuilderDefaultFieldName()
{
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME, recaptchaFieldName());
}

/**
* @test
*/
public function testRecaptchaRuleNameHelperReturnsReCaptchaBuilderDefaultRuleName()
{
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME, recaptchaRuleName());
}

/**
* @test
*/
public function testRecaptchaRuleNameHelperReturnsReCaptchaBuilderDefaultRuleName() {
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME, recaptchaRuleName());
}
/**
* @test
*/
public function testDefaultRecaptchaApiDomainIsGoogleDotCom()
{
$this->assertEquals("www.google.com", $this->recaptcha_v2->getApiDomain());
$this->assertEquals("www.google.com", $this->recaptcha_invisible->getApiDomain());
$this->assertEquals("www.google.com", $this->recaptcha_v3->getApiDomain());
}

protected function setSkipByIp(ReCaptchaBuilder $builder, bool $value)
{
Expand All @@ -190,6 +207,5 @@ protected function setUp(): void
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key');
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');

}
}
}

0 comments on commit 47c941c

Please sign in to comment.