From f613b36cff1348dc36c27f2f7efbf125a8cfae8c Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Sun, 13 Sep 2020 10:56:16 +0200 Subject: [PATCH 1/4] api_domain added to recaptcha.php config file --- config/recaptcha.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/config/recaptcha.php b/config/recaptcha.php index 31d1d1f..7516d5e 100644 --- a/config/recaptcha.php +++ b/config/recaptcha.php @@ -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) From bd4f9bd0313600377d30332f7ae866c4bf21d4ea Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Sun, 13 Sep 2020 11:11:13 +0200 Subject: [PATCH 2/4] 'api_url' and 'api_js_url' attributes supported in ReCaptchaBuilder ad ReCaptchaBuilderV3 for any ReCaptcha version --- src/ReCaptchaBuilder.php | 62 +++++++++++++++++++++++++++++++++++--- src/ReCaptchaBuilderV3.php | 16 ++++++---- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/ReCaptchaBuilder.php b/src/ReCaptchaBuilder.php index 4ac9b34..1d0c709 100755 --- a/src/ReCaptchaBuilder.php +++ b/src/ReCaptchaBuilder.php @@ -1,4 +1,5 @@ setApiSecretKey($api_secret_key); $this->setVersion($version); $this->setSkipByIp($this->skipByIp()); + $this->setApiDomain(); + $this->setApiUrls(); } /** @@ -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 */ @@ -233,7 +288,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string // Create query string $query = ($query) ? '?' . http_build_query($query) : ""; - $html .= ""; + $html .= ""; return $html; } @@ -300,7 +355,6 @@ public function validate($response) } return $response['success']; - } /** @@ -329,4 +383,4 @@ protected function returnArray(): bool return ($this->version == 'v3'); } -} \ No newline at end of file +} diff --git a/src/ReCaptchaBuilderV3.php b/src/ReCaptchaBuilderV3.php index e0b4a17..66674ed 100644 --- a/src/ReCaptchaBuilderV3.php +++ b/src/ReCaptchaBuilderV3.php @@ -1,4 +1,5 @@ api_site_key}\">"; + $html = ""; $action = Arr::get($configuration, 'action', 'homepage'); @@ -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 @@ -92,5 +97,4 @@ public function htmlScriptTagJsApi(?array $configuration = []): string return $html; } - -} \ No newline at end of file +} From eb1680fa6ff46104bcc74a5e9ed3ab5e09682a80 Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Sun, 13 Sep 2020 11:11:29 +0200 Subject: [PATCH 3/4] Tests added --- tests/ReCaptchaCustomApiDomainTest.php | 81 ++++++++++++++++++++++++++ tests/ReCaptchaTest.php | 52 +++++++++++------ 2 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 tests/ReCaptchaCustomApiDomainTest.php diff --git a/tests/ReCaptchaCustomApiDomainTest.php b/tests/ReCaptchaCustomApiDomainTest.php new file mode 100644 index 0000000..08ab8fb --- /dev/null +++ b/tests/ReCaptchaCustomApiDomainTest.php @@ -0,0 +1,81 @@ +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'); + } +} diff --git a/tests/ReCaptchaTest.php b/tests/ReCaptchaTest.php index 3f6d326..2b55b86 100644 --- a/tests/ReCaptchaTest.php +++ b/tests/ReCaptchaTest.php @@ -1,4 +1,5 @@ recaptcha_invisible; $html_button = $recaptcha->htmlFormButton(); - $this->assertEquals('', - $html_button); + $this->assertEquals( + '', + $html_button + ); } /** @@ -68,8 +71,10 @@ public function testReCaptchaInvisibleHtmlFormButtonCustom() $recaptcha = $this->recaptcha_invisible; $html_button = $recaptcha->htmlFormButton('Custom Text'); - $this->assertEquals('', - $html_button); + $this->assertEquals( + '', + $html_button + ); } /** @@ -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) { @@ -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'); - } -} \ No newline at end of file +} From c861eadaf1d1c4b5f01859abeb23adcbf8739f86 Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Mon, 14 Sep 2020 23:53:49 +0200 Subject: [PATCH 4/4] README.md changed --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5bf75d6..c96cf22 100644 --- a/README.md +++ b/README.md @@ -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