Skip to content

Commit f140bf5

Browse files
committed
log + translations
1 parent 3dfc03b commit f140bf5

10 files changed

Lines changed: 83 additions & 3 deletions

File tree

config/captcha.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
'secret_key' => env('CAPTCHA_TURNSTILE_SECRET_KEY', ''),
1111
],
1212
],
13+
'log_channel' => env('CAPTCHA_LOG_CHANNEL'),
14+
'log_verification_errors' => env('CAPTCHA_LOG_VERIFICATION_ERRORS', true),
1315
];

resources/lang/de/errors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'required' => 'Das Captcha muss vor dem Absenden des Formulars validiert werden.',
5+
'verification_failed' => 'Bei der Überprüfung des Captchas ist ein Fehler aufgetreten. Bitte versuchen Sie es später noch einmal.',
6+
];

resources/lang/en/errors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'required' => 'The captcha must be validated before submitting the form.',
5+
'verification_failed' => 'An error occurred during captcha verification. Please try again later.',
6+
];

resources/lang/es/errors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'required' => 'El captcha debe ser validado antes de enviar el formulario.',
5+
'verification_failed' => 'Se ha producido un error durante la verificación del captcha. Por favor, inténtelo de nuevo más tarde.',
6+
];

resources/lang/fr/errors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'required' => 'Le captcha doit être validé avant d’envoyer le formulaire.',
5+
'verification_failed' => 'Une erreur est survenue lors de la vérification du captcha. Veuillez réessayer plus tard.',
6+
];

resources/lang/it/errors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'required' => 'Il captcha deve essere convalidato prima di inviare il modulo.',
5+
'verification_failed' => 'Si è verificato un errore durante la verifica del captcha. Riprova più tardi.',
6+
];

src/CaptchaServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function configurePackage(Package $package): void
1717
$package
1818
->name('laravel-captcha')
1919
->hasConfigFile()
20+
->hasTranslations()
2021
->hasViews('captcha');
2122
}
2223

src/Providers/Turnstile/TurnstileClient.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Code16\Captcha\Providers\Turnstile;
44

55
use Code16\Captcha\Contracts\ClientInterface;
6+
use Illuminate\Http\Client\RequestException;
7+
use Illuminate\Support\Facades\Context;
68
use RyanChandler\LaravelCloudflareTurnstile\Client;
79
use RyanChandler\LaravelCloudflareTurnstile\Responses\SiteverifyResponse;
810

@@ -14,6 +16,12 @@ public function __construct(protected string $secret)
1416

1517
public function verify(string $token): SiteverifyResponse
1618
{
17-
return new Client($this->secret)->siteverify($token);
19+
try {
20+
return new Client($this->secret)->siteverify($token);
21+
} catch (RequestException $exception) {
22+
Context::add('response', $exception->response);
23+
24+
return SiteverifyResponse::failure($exception->response->json('error-codes', ['unknown']));
25+
}
1826
}
1927
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Code16\Captcha\Providers\Turnstile;
4+
5+
use Closure;
6+
use Code16\Captcha\Facades\Captcha;
7+
use Illuminate\Contracts\Validation\ValidationRule;
8+
use Illuminate\Support\Facades\Log;
9+
use RyanChandler\LaravelCloudflareTurnstile\Responses\SiteverifyResponse;
10+
11+
class TurnstileRule implements ValidationRule
12+
{
13+
protected array $messages = [];
14+
15+
public function validate(string $attribute, mixed $value, Closure $fail): void
16+
{
17+
/**
18+
* @var SiteverifyResponse $response
19+
*/
20+
$response = Captcha::verify($value);
21+
22+
if ($response->success) {
23+
return;
24+
}
25+
26+
if (count($response->errorCodes)) {
27+
if (config('captcha.log_verification_errors')) {
28+
Log::channel(config('captcha.log_channel'))->error('Captcha verification API failed', [
29+
'provider' => 'turnstile',
30+
'errorCodes' => $response->errorCodes,
31+
]);
32+
}
33+
$fail(__('captcha::errors.verification_failed'));
34+
}
35+
}
36+
}

src/Rules/Captcha.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Code16\Captcha\Rules;
44

55
use Closure;
6+
use Code16\Captcha\Providers\Turnstile\TurnstileRule;
67
use Illuminate\Contracts\Validation\ValidationRule;
78
use Illuminate\Support\Facades\Validator;
8-
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
99

1010
class Captcha implements ValidationRule
1111
{
@@ -23,9 +23,12 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2323
$attribute => [
2424
'required',
2525
match (config('captcha.provider')) {
26-
'turnstile' => new Turnstile(),
26+
'turnstile' => new TurnstileRule(),
2727
},
2828
],
29+
],
30+
[
31+
"$attribute.required" => __('captcha::errors.required'),
2932
]
3033
);
3134

0 commit comments

Comments
 (0)