Skip to content

Commit 25b7be9

Browse files
wip
1 parent 3415cd5 commit 25b7be9

File tree

11 files changed

+179
-70
lines changed

11 files changed

+179
-70
lines changed

config/google-recaptcha-v3.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3-
// config for Maize/GoogleRecaptchaV3
43
return [
4+
'enabled' => env('GOOGLE_RECAPTCHA_V3_ENABLED'),
55

6+
'site_key' => env('GOOGLE_RECAPTCHA_V3_SITE_KEY'),
7+
8+
'secret_key' => env('GOOGLE_RECAPTCHA_V3_SECRET_KEY'),
69
];

database/factories/ModelFactory.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

database/migrations/create_google_recaptcha_v3_table.php.stub

Lines changed: 0 additions & 19 deletions
This file was deleted.

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ parameters:
66
paths:
77
- src
88
- config
9-
- database
109
tmpDir: build/phpstan
1110
checkOctaneCompatibility: true
1211
checkModelProperties: true

resources/views/.gitkeep

Whitespace-only changes.

src/Commands/GoogleRecaptchaV3Command.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Enums/Badge.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Maize\GoogleRecaptchaV3\Enums;
4+
5+
enum Badge: string
6+
{
7+
case INLINE = 'inline';
8+
case BOTTOMLEFT = 'bottomleft';
9+
case BOTTOMRIGHT = 'bottomright';
10+
11+
// TODO
12+
// case HIDDEN = 'hidden';
13+
}

src/GoogleRecaptchaV3.php

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,79 @@
22

33
namespace Maize\GoogleRecaptchaV3;
44

5-
class GoogleRecaptchaV3 {}
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Validation\Rule;
7+
use Maize\GoogleRecaptchaV3\Enums\Badge;
8+
use Maize\GoogleRecaptchaV3\Support\Config;
9+
10+
class GoogleRecaptchaV3
11+
{
12+
public function boot(): void
13+
{
14+
Blade::directive('recaptcha', fn (Badge $badge) => (
15+
$this->toHtml($badge)
16+
));
17+
18+
Rule::macro('googleRecaptchaV3', fn () => null);
19+
}
20+
21+
protected function getJsScriptUrl(Badge $badge): string
22+
{
23+
return Config::getBaseJsScriptUrl()
24+
->withQuery(['badge' => $badge])
25+
->value();
26+
}
27+
28+
protected function getJsTokenScript(): string
29+
{
30+
$key = Config::getSiteKey();
31+
32+
return <<<JS
33+
window.recaptcha = function (action = 'submit') {
34+
return new Promise((resolve, reject) => {
35+
if (typeof grecaptcha.ready === 'undefined') {
36+
return resolve(null);
37+
}
38+
39+
if (typeof grecaptcha.ready === 'undefined') {
40+
return resolve(null);
41+
}
42+
43+
grecaptcha.ready(function() {
44+
try {
45+
grecaptcha.execute('{$key}', { action: action })
46+
.then(token => resolve(token))
47+
.catch(error => resolve(null));
48+
} catch (error) {
49+
resolve(null);
50+
}
51+
});
52+
});
53+
};
54+
JS;
55+
}
56+
57+
protected function toHtml(Badge $badge): string
58+
{
59+
if (! Config::isEnabled()) {
60+
return '';
61+
}
62+
63+
return implode("\n\n", [
64+
str($this->getJsScriptUrl($badge))->wrap('<script>', '</script>'),
65+
str($this->getJsTokenScript())->wrap('<script>', '</script>'),
66+
// TODO: add style
67+
]);
68+
}
69+
70+
public function FunctionName($value = '')
71+
{
72+
// Rule::macro('recaptcha', );
73+
}
74+
75+
// <style>
76+
// .grecaptcha-badge {
77+
// bottom: 90px !important;
78+
// }
79+
// </style>
80+
}

src/GoogleRecaptchaV3ServiceProvider.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22

33
namespace Maize\GoogleRecaptchaV3;
44

5-
use Maize\GoogleRecaptchaV3\Commands\GoogleRecaptchaV3Command;
5+
use Maize\GoogleRecaptchaV3\Facades\GoogleRecaptchaV3;
66
use Spatie\LaravelPackageTools\Package;
77
use Spatie\LaravelPackageTools\PackageServiceProvider;
88

99
class GoogleRecaptchaV3ServiceProvider extends PackageServiceProvider
1010
{
1111
public function configurePackage(Package $package): void
1212
{
13-
/*
14-
* This class is a Package Service Provider
15-
*
16-
* More info: https://github.com/spatie/laravel-package-tools
17-
*/
1813
$package
1914
->name('laravel-google-recaptcha-v3')
20-
->hasConfigFile()
21-
->hasViews()
22-
->hasMigration('create_laravel_google_recaptcha_v3_table')
23-
->hasCommand(GoogleRecaptchaV3Command::class);
15+
->hasConfigFile();
16+
}
17+
18+
public function packageBooted(): void
19+
{
20+
GoogleRecaptchaV3::boot();
2421
}
2522
}

src/Rules/Recaptcha.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Support\ReCaptcha\Rules;
4+
5+
use Illuminate\Contracts\Validation\ValidationRule;
6+
use Illuminate\Support\Facades\Http;
7+
use Maize\GoogleRecaptchaV3\Support\Config;
8+
9+
class Recaptcha implements ValidationRule
10+
{
11+
public function __construct(
12+
private float $scoreThreshold
13+
) {}
14+
15+
/**
16+
* Esegue la logica di validazione.
17+
*
18+
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
19+
*/
20+
public function validate(string $attribute, mixed $value, \Closure $fail): void
21+
{
22+
$response = Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [
23+
'secret' => Config::getSecretKey(),
24+
'response' => $value,
25+
])->json();
26+
27+
if (! ($response['success'] ?? false)) {
28+
$fail(__('ReCAPTCHA verification failed.'));
29+
}
30+
31+
if (($response['score'] ?? 0.0) < $this->scoreThreshold) {
32+
$fail(__('ReCAPTCHA verification failed.'));
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)