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

Commit 4991b28

Browse files
committed
Merge branch 'feature/recaptcha_dot_net_support' into develop
2 parents af5a7d7 + c861ead commit 4991b28

6 files changed

+201
-35
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Available reCAPTCHA versions:
1414

1515
## System requirements
1616

17-
| Package | reCAPTCHA | PHP | Laravel | Docs |
18-
| ------- | ----------------------------- | --------------------- | ---------------------------- | ---------------------------------------------------------------------- |
19-
| 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) |
20-
| 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) |
21-
| 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) |
22-
| 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) |
23-
| 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) |
17+
| Package | reCAPTCHA | PHP | Laravel | Docs |
18+
| ---------------- | ----------------------------- | --------------------- | ---------------------------- | ---------------------------------------------------------------------- |
19+
| 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) |
20+
| 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) |
21+
| 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) |
22+
| 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) |
23+
| 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) |
2424

2525
> (\*) Latest version (3.6.1) is Laravel 6 ready
2626

config/recaptcha.php

+11
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@
9999
*/
100100
'explicit' => false,
101101

102+
/**
103+
*
104+
* Set API domain. You can use "www.recaptcha.net" in case "www.google.com" is not accessible.
105+
* (no check will be made on the entered value)
106+
* @see https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally
107+
* @since v4.3.0
108+
* Default 'www.google.com' (ReCaptchaBuilder::DEFAULT_RECAPTCHA_API_DOMAIN)
109+
*
110+
*/
111+
'api_domain' => 'www.google.com',
112+
102113
/**
103114
*
104115
* g-recaptcha tag attributes and grecaptcha.render parameters (v2 only)

src/ReCaptchaBuilder.php

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaBuilder.php
@@ -44,6 +45,11 @@ class ReCaptchaBuilder
4445
*/
4546
const DEFAULT_RECAPTCHA_FIELD_NAME = 'g-recaptcha-response';
4647

48+
/**
49+
* @var string
50+
*/
51+
const DEFAULT_RECAPTCHA_API_DOMAIN = 'www.google.com';
52+
4753
/**
4854
* The Site key
4955
* please visit https://developers.google.com/recaptcha/docs/start
@@ -71,10 +77,23 @@ class ReCaptchaBuilder
7177
*/
7278
protected $skip_by_ip = false;
7379

80+
/**
81+
* The API domain (default: retrieved from config file)
82+
* @var string
83+
*/
84+
protected $api_domain = '';
85+
7486
/**
7587
* The API request URI
88+
* @var string
7689
*/
77-
protected $api_url = 'https://www.google.com/recaptcha/api/siteverify';
90+
protected $api_url = '';
91+
92+
/**
93+
* The URI of the API Javascript file to embed in you pages
94+
* @var string
95+
*/
96+
protected $api_js_url = '';
7897

7998
/**
8099
* ReCaptchaBuilder constructor.
@@ -93,6 +112,8 @@ public function __construct(
93112
$this->setApiSecretKey($api_secret_key);
94113
$this->setVersion($version);
95114
$this->setSkipByIp($this->skipByIp());
115+
$this->setApiDomain();
116+
$this->setApiUrls();
96117
}
97118

98119
/**
@@ -165,6 +186,40 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder
165186
return $this;
166187
}
167188

189+
/**
190+
* @param null|string $api_domain
191+
*
192+
* @return ReCaptchaBuilder
193+
*/
194+
public function setApiDomain(?string $api_domain = null): ReCaptchaBuilder
195+
{
196+
197+
$this->api_domain = $api_domain ?? config('recaptcha.api_domain', self::DEFAULT_RECAPTCHA_API_DOMAIN);
198+
199+
return $this;
200+
}
201+
202+
/**
203+
* @return string
204+
*/
205+
public function getApiDomain(): string
206+
{
207+
208+
return $this->api_domain;
209+
}
210+
211+
/**
212+
* @return ReCaptchaBuilder
213+
*/
214+
public function setApiUrls(): ReCaptchaBuilder
215+
{
216+
217+
$this->api_url = 'https://' . $this->api_domain . '/recaptcha/api/siteverify';
218+
$this->api_js_url = 'https://' . $this->api_domain . '/recaptcha/api.js';
219+
220+
return $this;
221+
}
222+
168223
/**
169224
* @return array|mixed
170225
*/
@@ -233,7 +288,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
233288

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

238293
return $html;
239294
}
@@ -300,7 +355,6 @@ public function validate($response)
300355
}
301356

302357
return $response['success'];
303-
304358
}
305359

306360
/**
@@ -329,4 +383,4 @@ protected function returnArray(): bool
329383

330384
return ($this->version == 'v3');
331385
}
332-
}
386+
}

src/ReCaptchaBuilderV3.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaBuilderV3.php
@@ -46,7 +47,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
4647
return '';
4748
}
4849

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

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

@@ -65,9 +66,13 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
6566
$js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';
6667

6768
$validate_function = "
68-
fetch('/" . config('recaptcha.default_validation_route',
69-
'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name',
70-
'token') . "=' + token, {
69+
fetch('/" . config(
70+
'recaptcha.default_validation_route',
71+
'biscolab-recaptcha/validate'
72+
) . "?" . config(
73+
'recaptcha.default_token_parameter_name',
74+
'token'
75+
) . "=' + token, {
7176
headers: {
7277
\"X-Requested-With\": \"XMLHttpRequest\",
7378
\"X-CSRF-TOKEN\": csrfToken.content
@@ -92,5 +97,4 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
9297

9398
return $html;
9499
}
95-
96-
}
100+
}
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* Copyright (c) 2017 - present
5+
* LaravelGoogleRecaptcha - ReCaptchaCustomApiDomainTest.php
6+
* author: Roberto Belotti - [email protected]
7+
* web : robertobelotti.com, github.com/biscolab
8+
* Initial version created on: 13/9/2020
9+
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
10+
*/
11+
12+
namespace Biscolab\ReCaptcha\Tests;
13+
14+
use Biscolab\ReCaptcha\ReCaptchaBuilderInvisible;
15+
use Biscolab\ReCaptcha\ReCaptchaBuilderV2;
16+
use Biscolab\ReCaptcha\ReCaptchaBuilderV3;
17+
18+
class ReCaptchaCustomApiDomainTest extends TestCase
19+
{
20+
21+
/**
22+
* @var ReCaptchaBuilderInvisible
23+
*/
24+
protected $recaptcha_invisible;
25+
26+
/**
27+
* @var ReCaptchaBuilderV2
28+
*/
29+
protected $recaptcha_v2;
30+
31+
/**
32+
* @var ReCaptchaBuilderV3
33+
*/
34+
protected $recaptcha_v3;
35+
36+
/**
37+
* @test
38+
*/
39+
public function testRecaptchaApiDomainChangesByConfig()
40+
{
41+
$this->app['config']->set('recaptcha.api_domain', 'www.recaptcha.net');
42+
$this->assertEquals("www.recaptcha.net", $this->recaptcha_v2->getApiDomain());
43+
$this->assertEquals("www.recaptcha.net", $this->recaptcha_invisible->getApiDomain());
44+
$this->assertEquals("www.recaptcha.net", $this->recaptcha_v3->getApiDomain());
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function testRecaptchaApiDomainChangesByConfigInHtmlScriptTagJsApi()
51+
{
52+
$this->assertContains("https://www.recaptcha.net/recaptcha/api.js", $this->recaptcha_v2->htmlScriptTagJsApi());
53+
$this->assertContains("https://www.recaptcha.net/recaptcha/api.js", $this->recaptcha_invisible->htmlScriptTagJsApi());
54+
$this->assertContains("https://www.recaptcha.net/recaptcha/api.js", $this->recaptcha_v3->htmlScriptTagJsApi());
55+
}
56+
57+
/**
58+
* Define environment setup.
59+
*
60+
* @param \Illuminate\Foundation\Application $app
61+
*
62+
* @return void
63+
*/
64+
protected function getEnvironmentSetUp($app)
65+
{
66+
67+
$app['config']->set('recaptcha.api_domain', 'www.recaptcha.net');
68+
}
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
protected function setUp(): void
74+
{
75+
76+
parent::setUp(); // TODO: Change the autogenerated stub
77+
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key');
78+
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
79+
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
80+
}
81+
}

tests/ReCaptchaTest.php

+34-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaTest.php
@@ -56,8 +57,10 @@ public function testReCaptchaInvisibleHtmlFormButtonDefault()
5657

5758
$recaptcha = $this->recaptcha_invisible;
5859
$html_button = $recaptcha->htmlFormButton();
59-
$this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Submit</button>',
60-
$html_button);
60+
$this->assertEquals(
61+
'<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Submit</button>',
62+
$html_button
63+
);
6164
}
6265

6366
/**
@@ -68,8 +71,10 @@ public function testReCaptchaInvisibleHtmlFormButtonCustom()
6871

6972
$recaptcha = $this->recaptcha_invisible;
7073
$html_button = $recaptcha->htmlFormButton('Custom Text');
71-
$this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Custom Text</button>',
72-
$html_button);
74+
$this->assertEquals(
75+
'<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Custom Text</button>',
76+
$html_button
77+
);
7378
}
7479

7580
/**
@@ -156,19 +161,31 @@ public function testReCaptchaV2htmlFormButtonShouldThrowError()
156161
$this->recaptcha_v2->htmlFormButton();
157162
}
158163

159-
/**
160-
* @test
161-
*/
162-
public function testRecaptchaFieldNameHelperReturnsReCaptchaBuilderDefaultFieldName() {
163-
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME, recaptchaFieldName());
164-
}
164+
/**
165+
* @test
166+
*/
167+
public function testRecaptchaFieldNameHelperReturnsReCaptchaBuilderDefaultFieldName()
168+
{
169+
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME, recaptchaFieldName());
170+
}
171+
172+
/**
173+
* @test
174+
*/
175+
public function testRecaptchaRuleNameHelperReturnsReCaptchaBuilderDefaultRuleName()
176+
{
177+
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME, recaptchaRuleName());
178+
}
165179

166-
/**
167-
* @test
168-
*/
169-
public function testRecaptchaRuleNameHelperReturnsReCaptchaBuilderDefaultRuleName() {
170-
$this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME, recaptchaRuleName());
171-
}
180+
/**
181+
* @test
182+
*/
183+
public function testDefaultRecaptchaApiDomainIsGoogleDotCom()
184+
{
185+
$this->assertEquals("www.google.com", $this->recaptcha_v2->getApiDomain());
186+
$this->assertEquals("www.google.com", $this->recaptcha_invisible->getApiDomain());
187+
$this->assertEquals("www.google.com", $this->recaptcha_v3->getApiDomain());
188+
}
172189

173190
protected function setSkipByIp(ReCaptchaBuilder $builder, bool $value)
174191
{
@@ -190,6 +207,5 @@ protected function setUp(): void
190207
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key');
191208
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
192209
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key');
193-
194210
}
195-
}
211+
}

0 commit comments

Comments
 (0)