|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laragear\Turnstile\Livewire; |
| 4 | + |
| 5 | +use Filament\Notifications\Notification; |
| 6 | +use Illuminate\Contracts\Validation\Validator as ValidatorContract; |
| 7 | +use Illuminate\Support\Arr; |
| 8 | +use Illuminate\Validation\ValidationException; |
| 9 | +use Laragear\Turnstile\Challenge; |
| 10 | +use Laragear\Turnstile\Turnstile; |
| 11 | +use Throwable; |
| 12 | +use function __; |
| 13 | +use function app; |
| 14 | +use function request; |
| 15 | + |
| 16 | +trait InteractsWithTurnstile |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Boots the trait. |
| 20 | + */ |
| 21 | + public function bootInteractsWithTurnstile(): void |
| 22 | + { |
| 23 | + $this->withValidator(function (ValidatorContract $validator): void { |
| 24 | + $validator->after($this->validateTurnstile(...)); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Check if the Turnstile challenge validation should be skipped. |
| 30 | + */ |
| 31 | + protected function skipTurnstileValidation(): bool |
| 32 | + { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Validates the Turnstile captcha token provided in the request data. |
| 38 | + */ |
| 39 | + protected function validateTurnstile(): void |
| 40 | + { |
| 41 | + if ($this->skipTurnstileValidation() || request()->isPrecognitive()) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + $turnstile = app(Turnstile::class); |
| 46 | + |
| 47 | + // If Turnstile is disabled, we don't need to use it at all. |
| 48 | + if ($turnstile->isDisabled()) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (!$token = $this->turnstileToken($turnstile->key())) { |
| 53 | + $this->handleFailedTurnstileChallenge(); |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + $challenge = $this->retrieveTurnstileChallenge($turnstile, $token); |
| 58 | + } catch (Throwable $exception) { |
| 59 | + $this->handleTurnstileException($exception); |
| 60 | + } |
| 61 | + |
| 62 | + $challenge && $this->handleTurnstileChallengeStatus($challenge) |
| 63 | + ? $this->handleSuccessfulTurnstileChallenge($challenge) |
| 64 | + : $this->handleFailedTurnstileChallenge($challenge); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Return token for the Turnstile Challenge present in the form. |
| 69 | + * |
| 70 | + * @return string|null|void |
| 71 | + */ |
| 72 | + protected function turnstileToken(string $key) |
| 73 | + { |
| 74 | + return Arr::get($this->data ?? $this->form?->getFormSnapshot(), $key); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Retrieves the token challenge. |
| 79 | + */ |
| 80 | + protected function retrieveTurnstileChallenge(Turnstile $turnstile, string $token): ?Challenge |
| 81 | + { |
| 82 | + return $turnstile->getChallenge($token); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Handles the Turnstile challenge and returns true or false if it has succeeded or failed. |
| 87 | + */ |
| 88 | + protected function handleTurnstileChallengeStatus(Challenge $challenge): bool |
| 89 | + { |
| 90 | + return $challenge->successful; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Handle a successful Turnstile challenge. |
| 95 | + */ |
| 96 | + protected function handleSuccessfulTurnstileChallenge(Challenge $challenge): void |
| 97 | + { |
| 98 | + // |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Handle a failed Turnstile challenge. |
| 103 | + */ |
| 104 | + protected function handleFailedTurnstileChallenge(?Challenge $challenge = null): void |
| 105 | + { |
| 106 | + $this->notifyFailedTurnstileChallenge($challenge); |
| 107 | + $this->throwTurnstileValidationError($challenge); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Send a notification to the user if the challenge has failed. |
| 112 | + */ |
| 113 | + protected function notifyFailedTurnstileChallenge(?Challenge $challenge = null): void |
| 114 | + { |
| 115 | + Notification::make('turnstile-challenge') |
| 116 | + ->title(__('turnstile::notification.failed.title')) |
| 117 | + ->body(__('turnstile::notification.failed.body')) |
| 118 | + ->danger() |
| 119 | + ->send(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Throw a Validation exception for the failed Turnstile challenge. |
| 124 | + */ |
| 125 | + protected function throwTurnstileValidationError(?Challenge $challenge = null): never |
| 126 | + { |
| 127 | + throw ValidationException::withMessages([ |
| 128 | + Turnstile::KEY => __('turnstile::validation.invalid'), |
| 129 | + ]); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Handle a Turnstile challenge exception. |
| 134 | + * |
| 135 | + * @return void|never |
| 136 | + */ |
| 137 | + protected function handleTurnstileException(Throwable $exception) |
| 138 | + { |
| 139 | + throw $exception; |
| 140 | + } |
| 141 | +} |
0 commit comments