Validation is ready.
This commit is contained in:
		
							
								
								
									
										60
									
								
								src/CaptchaProvider.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/CaptchaProvider.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace korElf\CaptchaRuleForLaravel;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Support\Facades\Validator;
 | 
			
		||||
use Illuminate\Support\ServiceProvider;
 | 
			
		||||
use Illuminate\Support\Facades\Blade;
 | 
			
		||||
use Illuminate\Contracts\Foundation\Application;
 | 
			
		||||
 | 
			
		||||
final class CaptchaProvider extends ServiceProvider
 | 
			
		||||
{
 | 
			
		||||
    public function boot(): Void
 | 
			
		||||
    {
 | 
			
		||||
        $this->addValidationRule();
 | 
			
		||||
        $this->publishes([
 | 
			
		||||
            __DIR__ . '/../config/captcha.php' => config_path('captcha.php'),
 | 
			
		||||
        ], 'config');
 | 
			
		||||
 | 
			
		||||
        if (config('captcha.enable_blade_captcha')) {
 | 
			
		||||
            Blade::directive('captcha', function () {
 | 
			
		||||
                return "<?php echo \korElf\CaptchaRuleForLaravel\htmlCaptcha(); ?>";
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Extends Validator to include a captcha type
 | 
			
		||||
     */
 | 
			
		||||
    public function addValidationRule(): Void
 | 
			
		||||
    {
 | 
			
		||||
        $message = trans(config('captcha.error_message_key'));
 | 
			
		||||
        Validator::extendImplicit(config('captcha.rule_name'), function ($attribute, $value) {
 | 
			
		||||
            if (!is_string($value)) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            return app(CaptchaService::class)->validate($value, request()->userAgent());
 | 
			
		||||
        }, $message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Register the service provider.
 | 
			
		||||
     *
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function register(): Void
 | 
			
		||||
    {
 | 
			
		||||
        $this->mergeConfigFrom(
 | 
			
		||||
            __DIR__ . '/../config/captcha.php',
 | 
			
		||||
            'captcha'
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $this->app->singleton(CaptchaService::class, function (Application $app) {
 | 
			
		||||
            return new CaptchaService(
 | 
			
		||||
                privateToken: config('captcha.api_private_token', ''),
 | 
			
		||||
                domainApi: config('captcha.api_domain', ''),
 | 
			
		||||
                curlTimeout: config('captcha.curl_timeout', ''),
 | 
			
		||||
            );
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								src/CaptchaService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/CaptchaService.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace korElf\CaptchaRuleForLaravel;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Support\Facades\Http;
 | 
			
		||||
 | 
			
		||||
final readonly class CaptchaService
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        private string $privateToken,
 | 
			
		||||
        private string $domainApi,
 | 
			
		||||
        private int $curlTimeout = 10,
 | 
			
		||||
    ) { }
 | 
			
		||||
 | 
			
		||||
    public function validate(string $token, ?string $userAgent = null): bool
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $post = [];
 | 
			
		||||
            if (!is_null($userAgent)) {
 | 
			
		||||
                $post['user_ageng'] = $userAgent;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $response = Http::withHeaders([
 | 
			
		||||
                'private-token' => $this->privateToken,
 | 
			
		||||
            ])
 | 
			
		||||
                ->timeout($this->curlTimeout)
 | 
			
		||||
                ->post($this->domainApi . '/api/v1/captcha/' . $token, $post);
 | 
			
		||||
 | 
			
		||||
            return ($response->json('status') === true);
 | 
			
		||||
        } catch (\Throwable $e) {
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								src/helpers.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/helpers.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
<?php declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace korElf\CaptchaRuleForLaravel;
 | 
			
		||||
 | 
			
		||||
function htmlCaptcha(): string
 | 
			
		||||
{
 | 
			
		||||
    return '<div class="captcha-service-kor-elf" data-domain="' . config('captcha.api_domain') . '" data-static-path="' . config('captcha.static_path') . '" data-token="' . config('captcha.api_public_token') . '" data-captcha-verified-name="' . config('captcha.captcha_verified_name') . '"></div><script src="' . config('captcha.static_path') . '/script.js" async></script>';
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user