├── .github └── workflows │ └── main.yml ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── lang ├── de │ └── validation.php └── en │ └── validation.php └── src ├── Facades └── FriendlyCaptcha.php ├── FriendlyCaptcha.php ├── FriendlyCaptchaServiceProvider.php ├── Rules └── FriendlyCaptcha.php └── config └── friendlycaptcha.php /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | test: 11 | runs-on: ${{ matrix.os }} 12 | strategy: 13 | fail-fast: true 14 | matrix: 15 | os: [ubuntu-latest, windows-latest] 16 | php: [7.4, 8.0, 8.1] 17 | laravel: [8.*, 9.*, 10.*] 18 | stability: [prefer-stable] 19 | include: 20 | - laravel: 10.* 21 | testbench: 8.* 22 | - laravel: 9.* 23 | testbench: 7.* 24 | - laravel: 8.* 25 | testbench: 6.* 26 | exclude: 27 | - laravel: 10.* 28 | php: 8.0 29 | - laravel: 10.* 30 | php: 7.4 31 | - laravel: 9.* 32 | php: 7.4 33 | 34 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} 35 | 36 | steps: 37 | - name: Checkout code 38 | uses: actions/checkout@v2 39 | 40 | - name: Setup PHP 41 | uses: shivammathur/setup-php@v2 42 | with: 43 | php-version: ${{ matrix.php }} 44 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo 45 | coverage: none 46 | 47 | - name: Setup problem matchers 48 | run: | 49 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 50 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 51 | - name: Install dependencies 52 | run: | 53 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 54 | composer update --${{ matrix.stability }} --prefer-dist --no-interaction 55 | - name: Execute tests 56 | run: vendor/bin/phpunit 57 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `ossycodes/friendlycaptcha` will be documented in this file 4 | 5 | ## 1.0.0 - 2021-10-21 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ossycodes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A simple package to help integrate FriendlyCaptcha. 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ossycodes/friendlycaptcha.svg?style=flat-square)](https://packagist.org/packages/ossycodes/friendlycaptcha) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/ossycodes/friendlycaptcha.svg?style=flat-square)](https://packagist.org/packages/ossycodes/friendlycaptcha) 5 | ![GitHub Actions](https://github.com/ossycodes/friendlycaptcha/actions/workflows/main.yml/badge.svg) 6 | 7 | This package helps in setting up and validating FriendlyCaptcha widget and response in your Laravel applications 8 | 9 | ## Installation 10 | 11 | You can install the package via composer: 12 | 13 | ```bash 14 | composer require ossycodes/friendlycaptcha 15 | ``` 16 | 17 | ### Configuration 18 | 19 | Add `FRIENDLY_CAPTCHA_SECRET`, `FRIENDLY_CAPTCHA_SITEKEY` and optional `FRIENDLY_CAPTCHA_PUZZLE_ENDPOINT`, `FRIENDLY_CAPTCHA_VERIFY_ENDPOINT` in **.env** file : 20 | 21 | ``` 22 | FRIENDLY_CAPTCHA_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 23 | FRIENDLY_CAPTCHA_SITEKEY=XXXXXXXXXXXXXXXX 24 | FRIENDLY_CAPTCHA_PUZZLE_ENDPOINT=https://api.friendlycaptcha.com/api/v1/puzzle #optional 25 | FRIENDLY_CAPTCHA_VERIFY_ENDPOINT=https://api.friendlycaptcha.com/api/v1/siteverify #optional 26 | ``` 27 | 28 | You can obtain your site-key from [here](https://docs.friendlycaptcha.com/#/installation?id=_1-generating-a-sitekey) and secret from [here](https://apiserver-prod.friendlycaptcha.eu/dashboard/accounts/1118678876/apikeys) 29 | 30 | ## Usage 31 | 32 | For FriendlyCaptcha widget scripts from a CDN, add the Blade directive `@friendlyCaptchaRenderWidgetScripts` in your layout file. This should be added to the `` of your document. 33 | 34 | ```blade 35 | 36 | 37 | @friendlyCaptchaRenderWidgetScripts() 38 | 39 | 40 | {{ $slot }} 41 | 42 | 43 | ``` 44 | 45 | or if you don't want to use the Blade directive you can do this instead 46 | 47 | ```php 48 | {!! FriendlyCaptcha::renderWidgetScripts() !!} 49 | ``` 50 | 51 | You have two options on how to add the script tag either from unpkg (default) or from jsdelivr 52 | 53 | `@friendlyCaptchaRenderWidgetScripts()` 54 | or 55 | `@friendlyCaptchaRenderWidgetScripts('jsdelivr')` 56 | 57 | `{!! FriendlyCaptcha::renderWidgetScripts() !!}` 58 | or 59 | `{!! FriendlyCaptcha::renderWidgetScripts('jsdelivr') !!}` 60 | 61 | You can also host the FriendlyCaptcha widget scripts yourself: 62 | 63 | ``` 64 | npm install --save friendly-challenge@0.9.9 65 | ``` 66 | 67 | And import it in your app: 68 | 69 | ```js 70 | import "friendly-challenge/widget"; 71 | ``` 72 | 73 | 74 | Once that's done, you can call the `renderWidget()` method in `
` to output the appropriate markup (friendlycaptcha widget) with your site key configured. 75 | 76 | ```blade 77 | 78 | 79 | {!! FriendlyCaptcha::renderWidget() !!} 80 | 81 | or with custom theme 82 | 83 | {!! FriendlyCaptcha::renderWidget(['dark-theme' => true]) !!} 84 | 85 | or with custom language 86 | 87 | {!! FriendlyCaptcha::renderWidget(['data-lang' => 'en']) !!} 88 | 89 | 92 |
93 | ``` 94 | 95 | Finally On the server, use the provided validation rule to validate the CAPTCHA response. 96 | 97 | ```php 98 | use Illuminate\Validation\Rule; 99 | 100 | public function submit(Request $request) 101 | { 102 | $request->validate([ 103 | 'frc-captcha-solution' => ['required', Rule::friendlycaptcha()], 104 | ]); 105 | } 106 | ``` 107 | 108 | If you prefer to not use a macro, you can resolve an instance of the rule from the container via dependency injection or the `app()` helper. 109 | 110 | ```php 111 | use Ossycodes\FriendlyCaptcha\Rules\FriendlyCaptcha; 112 | 113 | public function submit(Request $request, FriendlyCaptcha $friendlyCaptcha) 114 | { 115 | $request->validate([ 116 | 'frc-captcha-solution' => ['required', $friendlyCaptcha], 117 | ]); 118 | } 119 | ``` 120 | 121 | ```php 122 | use Ossycodes\FriendlyCaptcha\Rules\FriendlyCaptcha; 123 | 124 | public function submit(Request $request) 125 | { 126 | $request->validate([ 127 | 'frc-captcha-solution' => ['required', app(FriendlyCaptcha::class)], 128 | ]); 129 | } 130 | ``` 131 | 132 | ### Testing 133 | 134 | ```bash 135 | composer test 136 | ``` 137 | 138 | ### Security 139 | 140 | If you discover any security related issues, please email osaigbovoemmanuel1@gmail.com instead of using the issue tracker. 141 | 142 | ## Credits 143 | 144 | - [Osaigbovo Emmanuel](https://github.com/ossycodes) 145 | - [Julian Dorn](https://github.com/wi-wissen) 146 | - [All Contributors](../../contributors) 147 | 148 | ## License 149 | 150 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 151 | 152 | ## How do I say Thank you? 153 | 154 | Please buy me a cup of coffee https://www.paypal.com/paypalme/osaigbovoemmanuel , Leave a star and follow me on [Twitter](https://twitter.com/ossycodes) . 155 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ossycodes/friendlycaptcha", 3 | "description": "A simple package to help integrate FriendlyCaptcha in your Laravel applications.", 4 | "keywords": [ 5 | "friendlycaptcha", 6 | "captcha", 7 | "laravel" 8 | ], 9 | "homepage": "https://github.com/ossycodes/friendlycaptcha", 10 | "license": "MIT", 11 | "type": "library", 12 | "authors": [ 13 | { 14 | "name": "ossycodes", 15 | "email": "osaigbovoemmanuel1@gmail.com", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php": "^7.4|^8.0|^8.1|^8.2|^8.3", 21 | "illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0", 22 | "guzzlehttp/guzzle": "^7.0" 23 | }, 24 | "require-dev": { 25 | "orchestra/testbench": "^6.0|^7.0|^8.0|^9.0", 26 | "phpunit/phpunit": "^8.0 || ^9.5 || ^10.5 || ^11.0 || ^12.0" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Ossycodes\\FriendlyCaptcha\\": "src/" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "Ossycodes\\FriendlyCaptcha\\Tests\\": "tests" 36 | } 37 | }, 38 | "scripts": { 39 | "test": "vendor/bin/phpunit", 40 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 41 | }, 42 | "config": { 43 | "sort-packages": true, 44 | "platform-check": false, 45 | "allow-plugins": { 46 | "php-http/discovery": true 47 | } 48 | }, 49 | "extra": { 50 | "laravel": { 51 | "providers": [ 52 | "Ossycodes\\FriendlyCaptcha\\FriendlyCaptchaServiceProvider" 53 | ], 54 | "aliases": { 55 | "FriendlyCaptcha": "Ossycodes\\FriendlyCaptcha\\Facades\\FriendlyCaptcha" 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /lang/de/validation.php: -------------------------------------------------------------------------------- 1 | 'Sie haben vergessen, den Parameter secret (=API-Schlüssel) hinzuzufügen.', 11 | 'secret_invalid' => 'Der von Ihnen angegebene API-Schlüssel war ungültig.', 12 | 'solution_missing' => 'Sie haben vergessen, den Parameter secret (=API-Schlüssel) hinzuzufügen.', 13 | 'secret_missing' => 'Sie haben vergessen, den Lösungsparameter hinzuzufügen.', 14 | 'bad_request' => 'Mit Ihrer Anfrage ist etwas anderes nicht in Ordnung, z. B. ist Ihr Anfragekörper leer.', 15 | 'solution_invalid' => 'Die von Ihnen angegebene Lösung war ungültig (vielleicht wurde versucht, das Rätsel zu manipulieren).', 16 | 'solution_timeout_or_duplicate' => 'Das Rätsel, für das Sie die Lösung angegeben haben, ist abgelaufen oder wurde bereits verwendet.', 17 | 'unexpected' => 'Ein unerwarteter Fehler ist aufgetreten.' 18 | ]; -------------------------------------------------------------------------------- /lang/en/validation.php: -------------------------------------------------------------------------------- 1 | 'You forgot to add the secret (=API key) parameter.', 11 | 'secret_invalid' => 'The API key you provided was invalid.', 12 | 'solution_missing' => 'You forgot to add the secret (=API key) parameter.', 13 | 'secret_missing' => 'You forgot to add the solution parameter.', 14 | 'bad_request' => 'Something else is wrong with your request, e.g. your request body is empty.', 15 | 'solution_invalid' => 'The solution you provided was invalid (perhaps the user tried to tamper with the puzzle).', 16 | 'solution_timeout_or_duplicate' => 'The puzzle that the solution was for has expired or has already been used.', 17 | 'unexpected' => 'An unexpected error occurred.' 18 | ]; -------------------------------------------------------------------------------- /src/Facades/FriendlyCaptcha.php: -------------------------------------------------------------------------------- 1 | secret = $secret; 50 | $this->sitekey = $sitekey; 51 | $this->puzzle = $puzzle; 52 | $this->verify = $verify; 53 | $this->http = new Client($options); 54 | } 55 | 56 | public function renderWidgetScripts($option = 'unpkg') 57 | { 58 | if ($option == 'unpkg') { 59 | return << 61 | 62 | EOF; 63 | } 64 | 65 | return << 67 | 68 | EOF; 69 | } 70 | 71 | public function renderWidget($attributes = []) 72 | { 73 | $attributes = $this->prepareAttributes($attributes); 74 | return 'buildAttributes($attributes) . '>'; 75 | } 76 | 77 | /** 78 | * Prepare HTML attributes and ensure that the correct classes and attributes for captcha are inserted. 79 | * 80 | * @param array $attributes 81 | * 82 | * @return array 83 | */ 84 | protected function prepareAttributes(array $attributes) 85 | { 86 | $attributes['data-puzzle-endpoint'] = $this->puzzle; 87 | 88 | $attributes['data-sitekey'] = $this->sitekey; 89 | 90 | if (isset($attributes['dark-theme'])) { 91 | $attributes['class'] = 'frc-captcha dark'; 92 | unset($attributes['dark-theme']); 93 | return $attributes; 94 | } 95 | 96 | $attributes['class'] = trim('frc-captcha'); 97 | 98 | $locale = app()->getLocale(); 99 | 100 | if (in_array($locale, ["en", "fr", "de", "it", "nl", "pt", "es", "ca", "da", "ja", "ru", "sv", "el", "uk", "bg", "cs", "sk", "no", "fi", "lt", "lt", "pl", "et", "hr", "sr", "sl", "hu", "ro", "zh", "zh_TW", "vi"])) { 101 | //use supported locale - https://docs.friendlycaptcha.com/#/widget_api?id=data-lang-attribute 102 | $attributes['data-lang'] = $locale; 103 | } 104 | 105 | return $attributes; 106 | } 107 | 108 | /** 109 | * Build HTML attributes. 110 | * 111 | * @param array $attributes 112 | * 113 | * @return string 114 | */ 115 | protected function buildAttributes(array $attributes) 116 | { 117 | $html = []; 118 | 119 | foreach ($attributes as $key => $value) { 120 | $html[] = $key . '="' . $value . '"'; 121 | } 122 | 123 | return count($html) ? ' ' . implode(' ', $html) : ''; 124 | } 125 | 126 | /** 127 | * Verify FriendlyCaptcha response. 128 | * 129 | * @param string $solution 130 | * 131 | * @return bool 132 | */ 133 | public function verifyRequest($solution) 134 | { 135 | return $this->verifyResponse( 136 | $solution, 137 | ); 138 | } 139 | 140 | /** 141 | * Verify FriendlyCaptcha response. 142 | * 143 | * @param string $solution 144 | * 145 | * @return self 146 | */ 147 | public function verifyResponse($solution) 148 | { 149 | if (empty($solution)) { 150 | return false; 151 | } 152 | 153 | $verifyResponse = $this->sendRequestVerify([ 154 | 'solution' => $solution, 155 | 'secret' => $this->secret, 156 | 'sitekey' => $this->sitekey, 157 | ]); 158 | 159 | if (isset($verifyResponse['success']) && $verifyResponse['success'] === true) { 160 | $this->isSuccess = true; 161 | return $this; 162 | } 163 | 164 | if (isset($verifyResponse['errors'])) { 165 | $this->errors = $verifyResponse['errors']; 166 | } 167 | 168 | if (isset($verifyResponse['error'])) { 169 | $this->errors = [$verifyResponse['error']]; 170 | } 171 | 172 | $this->isSuccess = false; 173 | 174 | return $this; 175 | 176 | } 177 | 178 | /** 179 | * Send verify request. 180 | * 181 | * @param array $data 182 | * 183 | * @return array 184 | */ 185 | protected function sendRequestVerify(array $data = []) 186 | { 187 | $response = $this->http->request('POST', $this->verify, [ 188 | 'form_params' => $data, 189 | ]); 190 | 191 | return json_decode($response->getBody(), true); 192 | } 193 | 194 | public function isSuccess() 195 | { 196 | return $this->isSuccess; 197 | } 198 | 199 | public function getErrors() 200 | { 201 | return $this->errors; 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /src/FriendlyCaptchaServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 25 | $this->bootConfig(); 26 | } 27 | 28 | $this->bootBladeDirectives(); 29 | 30 | $this->bootMacro(); 31 | 32 | $this->bootLang(); 33 | } 34 | 35 | /** 36 | * Boot config. 37 | */ 38 | protected function bootConfig() 39 | { 40 | $path = __DIR__ . '/config/friendlycaptcha.php'; 41 | 42 | if (function_exists('config_path')) { 43 | $this->publishes([$path => config_path('friendlycaptcha.php')]); 44 | } 45 | } 46 | 47 | /** 48 | * Boot blade directives 49 | */ 50 | public function bootBladeDirectives() 51 | { 52 | Blade::directive('friendlyCaptchaRenderWidgetScripts', function ($option) { 53 | $option = trim($option, "'"); 54 | 55 | if (empty($option) || $option == 'unpkg') { 56 | return << 58 | 59 | EOF; 60 | } 61 | 62 | return << 64 | 65 | EOF; 66 | }); 67 | } 68 | 69 | /** 70 | * boot macro 71 | */ 72 | public function bootMacro() 73 | { 74 | Rule::macro('friendlycaptcha', function () { 75 | return app(\Ossycodes\FriendlyCaptcha\Rules\FriendlyCaptcha::class); 76 | }); 77 | } 78 | 79 | /** 80 | * boot lang 81 | */ 82 | public function bootLang() 83 | { 84 | Rule::macro('friendlycaptcha', function () { 85 | $this->loadTranslationsFrom(__DIR__.'/../lang', 'friendlycaptcha'); 86 | }); 87 | } 88 | 89 | /** 90 | * Register the application services. 91 | */ 92 | public function register() 93 | { 94 | $path = __DIR__ . '/config/friendlycaptcha.php'; 95 | 96 | $this->mergeConfigFrom($path, 'friendlycaptcha'); 97 | 98 | $this->app->singleton('FriendlyCaptcha', function ($app) { 99 | return new FriendlyCaptcha( 100 | $app['config']['friendlycaptcha.secret'], 101 | $app['config']['friendlycaptcha.sitekey'], 102 | $app['config']['friendlycaptcha.puzzle_endpoint'], 103 | $app['config']['friendlycaptcha.verify_endpoint'], 104 | $app['config']['friendlycaptcha.options'] 105 | ); 106 | }); 107 | 108 | $this->app->alias('FriendlyCaptcha', FriendlyCaptcha::class); 109 | } 110 | 111 | /** 112 | * Get the services provided by the provider. 113 | * 114 | * @return array 115 | */ 116 | public function provides() 117 | { 118 | return ['FriendlyCaptcha']; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/Rules/FriendlyCaptcha.php: -------------------------------------------------------------------------------- 1 | friendlyCaptchaClient = $friendlyCaptcha; 18 | } 19 | 20 | public function passes($attribute, $value) 21 | { 22 | $response = $this->friendlyCaptchaClient->verifyResponse($value); 23 | 24 | if ($response->isSuccess()) { 25 | return true; 26 | } 27 | 28 | foreach ($response->getErrors() as $errorCode) { 29 | $this->messages[] = $this->mapErrorCodeToMessage($errorCode); 30 | } 31 | 32 | return false; 33 | } 34 | 35 | public function message() 36 | { 37 | return $this->messages; 38 | } 39 | 40 | /** 41 | * map FriendlyCaptcha error code to human readable validation message 42 | * 43 | * @var string $code 44 | */ 45 | protected function mapErrorCodeToMessage(string $code): string 46 | { 47 | switch ($code) { 48 | case "secret_missing": 49 | return __('validation.secret_missing'); 50 | break; 51 | case "secret_invalid": 52 | return __('validation.secret_invalid'); 53 | break; 54 | case "solution_missing": 55 | return __('validation.solution_missing'); 56 | break; 57 | case "bad_request": 58 | return __('validation.bad_request'); 59 | break; 60 | case "solution_invalid": 61 | return __('validation.solution_invalid'); 62 | break; 63 | case "solution_timeout_or_duplicate": 64 | return __('validation.solution_timeout_or_duplicate'); 65 | break; 66 | default: 67 | return __('validation.unexpected'); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/config/friendlycaptcha.php: -------------------------------------------------------------------------------- 1 | env('FRIENDLY_CAPTCHA_SECRET'), 5 | 'sitekey' => env('FRIENDLY_CAPTCHA_SITEKEY'), 6 | 'puzzle_endpoint' => env('FRIENDLY_CAPTCHA_PUZZLE_ENDPOINT', 'https://api.friendlycaptcha.com/api/v1/puzzle'), 7 | 'verify_endpoint' => env('FRIENDLY_CAPTCHA_VERIFY_ENDPOINT', 'https://api.friendlycaptcha.com/api/v1/siteverify'), 8 | 'options' => [ 9 | 'timeout' => 30, 10 | 'http_errors' => false, 11 | ], 12 | ]; 13 | --------------------------------------------------------------------------------