From 328f420bf18c5f23bb62180b13da126631f3ce92 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 26 Nov 2023 23:32:41 +0600 Subject: [PATCH 1/5] Update copyright information in LICENSE. The author name in the copyright section of the LICENSE has been updated for accuracy. Previously, it displayed a nickname 'kor-elf' but now it includes the full name 'Leonid Nikitin' as well as the nickname. Ensuring correct, complete, and clear author attributions contributes to the maintainability and transparency of the project. --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index eca5772..3f3a98e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 kor-elf +Copyright (c) 2023 Leonid Nikitin (kor-elf) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From 250c1a9f8d62274c073d3d031d39a8a96f31170e Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sat, 2 Dec 2023 19:59:51 +0600 Subject: [PATCH 2/5] The LICENSE file was renamed to LICENSE.md. --- LICENSE => LICENSE.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename LICENSE => LICENSE.md (100%) diff --git a/LICENSE b/LICENSE.md similarity index 100% rename from LICENSE rename to LICENSE.md From c39f6ba4d79dab3b90ddee5932cef30e5b67c3f2 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 3 Dec 2023 00:46:06 +0600 Subject: [PATCH 3/5] Validation is ready. --- composer.json | 40 +++++++++++++++++++++++++++ config/captcha.php | 58 +++++++++++++++++++++++++++++++++++++++ src/CaptchaProvider.php | 60 +++++++++++++++++++++++++++++++++++++++++ src/CaptchaService.php | 35 ++++++++++++++++++++++++ src/helpers.php | 8 ++++++ 5 files changed, 201 insertions(+) create mode 100644 composer.json create mode 100644 config/captcha.php create mode 100644 src/CaptchaProvider.php create mode 100644 src/CaptchaService.php create mode 100644 src/helpers.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d18902d --- /dev/null +++ b/composer.json @@ -0,0 +1,40 @@ +{ + "name": "kor-elf/captcha-rule-for-laravel", + "description": "Validation Rule Captcha for Laravel", + "license": "MIT", + "type": "library", + "keywords": [ + "captcha", + "laravel", + "validation" + ], + "homepage": "https://git.kor-elf.net/kor-elf/captcha-rule-for-laravel", + "authors": [ + { + "name": "Leonid Nikitin", + "email": "i@kor-elf.net", + "homepage": "https://git.kor-elf.net/kor-elf", + "role": "Developer" + } + ], + "require": { + "php": "^8.2", + "illuminate/support": "^10.0", + "guzzlehttp/guzzle": "^7.0.1" + }, + "extra": { + "laravel": { + "providers": [ + "korElf\\CaptchaRuleForLaravel\\CaptchaProvider" + ] + } + }, + "autoload": { + "psr-4": { + "korElf\\CaptchaRuleForLaravel\\": "src/" + }, + "files": [ + "src/helpers.php" + ] + } +} diff --git a/config/captcha.php b/config/captcha.php new file mode 100644 index 0000000..4ecd1ba --- /dev/null +++ b/config/captcha.php @@ -0,0 +1,58 @@ + env('CAPTCHA_API_DOMAIN'), + + /* + * Приватный токен для проверки получаемого ключа после успешной проверки от бота. + */ + 'api_private_token' => env('CAPTCHA_PRIVATE_TOKEN'), + + /* + * Curl timeout в секундах. + */ + 'curl_timeout' => (int) env('CAPTCHA_CURL_TIMEOUT', 10), + + /* + * Включает Blade::directive "captcha". + */ + 'enable_blade_captcha' => (bool) env('CAPTCHA_ENABLE_BLADE_CAPTCHA', true), + + /* + * Публичный токен для начало проверки я не робот. + */ + 'api_public_token' => env('CAPTCHA_PUBLIC_TOKEN'), + + /** + * Указываем путь к статике, на данный момент это к стилям. + * Примеры: /captcha, https://captcha.localhost/captcha + */ + 'static_path' => env('CAPTCHA_STATIC_PATH', env('CAPTCHA_API_DOMAIN') . '/captcha'), + + /* + * Используется в переводах. + */ + 'error_message_key' => 'validation.captcha', + + /* + * Имя Validator::extendImplicit. + */ + 'rule_name' => 'captcha', + + /* + * Name в input после успешной проверки. + */ + 'captcha_verified_name' => 'captcha-verified', +]; diff --git a/src/CaptchaProvider.php b/src/CaptchaProvider.php new file mode 100644 index 0000000..ea326d6 --- /dev/null +++ b/src/CaptchaProvider.php @@ -0,0 +1,60 @@ +addValidationRule(); + $this->publishes([ + __DIR__ . '/../config/captcha.php' => config_path('captcha.php'), + ], 'config'); + + if (config('captcha.enable_blade_captcha')) { + Blade::directive('captcha', function () { + return ""; + }); + } + } + + /** + * 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', ''), + ); + }); + } +} diff --git a/src/CaptchaService.php b/src/CaptchaService.php new file mode 100644 index 0000000..1c5a608 --- /dev/null +++ b/src/CaptchaService.php @@ -0,0 +1,35 @@ + $this->privateToken, + ]) + ->timeout($this->curlTimeout) + ->post($this->domainApi . '/api/v1/captcha/' . $token, $post); + + return ($response->json('status') === true); + } catch (\Throwable $e) { + + } + return false; + } +} \ No newline at end of file diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..cd647f6 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,8 @@ +'; +} From d9e9564ea8d43daa3a9db2e5df9fd44c49795291 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 3 Dec 2023 00:58:47 +0600 Subject: [PATCH 4/5] Fix typo in 'user_agent' key assignment. A typo in the key assignment for 'user_agent' in the CaptchaService has been fixed. The key was previously incorrectly typed as 'user_ageng'. This issue was causing problems with HTTP posts where user agent data was not being properly passed due to the misspelt key. --- src/CaptchaService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CaptchaService.php b/src/CaptchaService.php index 1c5a608..7680148 100644 --- a/src/CaptchaService.php +++ b/src/CaptchaService.php @@ -17,7 +17,7 @@ final readonly class CaptchaService try { $post = []; if (!is_null($userAgent)) { - $post['user_ageng'] = $userAgent; + $post['user_agent'] = $userAgent; } $response = Http::withHeaders([ From 032185b52ca0be00d417f9be50c55bbcea3daac0 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 3 Dec 2023 01:06:09 +0600 Subject: [PATCH 5/5] Add instructions to README.md. --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 40fd0f1..240e780 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,33 @@ # captcha-rule-for-laravel +Laravel 10. Rule Validation для сервиса https://git.kor-elf.net/kor-elf/service-captcha. + +Параметры в .env. + +# CAPTCHA_API_DOMAIN +Указываем адрес к сервису для проверки от робота. + +Примеры: http://captcha.localhost:9008, https://captcha.localhost, http://captcha.localhost + +# CAPTCHA_PRIVATE_TOKEN +Указываем приватный токен, для проверки со стороны сервера. + +# CAPTCHA_CURL_TIMEOUT +Curl timeout в секундах. +По умолчанию: 10 + + +# CAPTCHA_ENABLE_BLADE_CAPTCHA +Включает Blade::directive "captcha" (@captcha). + +По умолчанию: true + +# CAPTCHA_PUBLIC_TOKEN +Указываем публичный токен, который мы получаем от сервиса для проверки от робота. + +# CAPTCHA_STATIC_PATH +Указываем путь к статике. + +Примеры: /captcha, https://captcha.localhost/captcha + +По умолчанию: env('CAPTCHA_API_DOMAIN') . '/captcha' \ No newline at end of file