├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Facades │ └── InvisibleReCaptcha.php ├── InvisibleReCaptcha.php ├── InvisibleReCaptchaServiceProvider.php └── config │ ├── .gitkeep │ └── captcha.php └── tests ├── CaptchaTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock 3 | .DS_Store 4 | .idea 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - '7.2' 5 | - '7.3' 6 | - '7.4' 7 | 8 | install: 9 | - travis_retry composer install --no-interaction --prefer-dist --no-suggest 10 | 11 | before_script: 12 | - travis_retry composer self-update 13 | - travis_retry composer install --prefer-source --no-interaction --dev 14 | 15 | script: 16 | - vendor/bin/phpunit 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Albert Chen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Invisible reCAPTCHA 2 | ========== 3 | ![php-badge](https://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg) 4 | [![packagist-badge](https://img.shields.io/packagist/v/albertcht/invisible-recaptcha.svg)](https://packagist.org/packages/albertcht/invisible-recaptcha) 5 | [![Total Downloads](https://poser.pugx.org/albertcht/invisible-recaptcha/downloads)](https://packagist.org/packages/albertcht/invisible-recaptcha) 6 | [![travis-badge](https://api.travis-ci.org/albertcht/invisible-recaptcha.svg?branch=master)](https://travis-ci.org/albertcht/invisible-recaptcha) 7 | 8 | ![invisible_recaptcha_demo](http://i.imgur.com/1dZ9XKn.png) 9 | 10 | ## Why Invisible reCAPTCHA? 11 | 12 | Invisible reCAPTCHA is an improved version of reCAPTCHA v2(no captcha). 13 | In reCAPTCHA v2, users need to click the button: "I'm not a robot" to prove they are human. In invisible reCAPTCHA, there will be not embed a captcha box for users to click. It's totally invisible! Only the badge will show on the buttom of the page to hint users that your website is using this technology. (The badge could be hidden, but not suggested.) 14 | 15 | ## Notice 16 | 17 | * The master branch doesn't support multi captchas feature, please use `multi-forms` branch if you need it. (**Most of the time you are misusing recaptcha when you try to put multiple captchas in one page.**) 18 | 19 | ## Installation 20 | 21 | ``` 22 | composer require albertcht/invisible-recaptcha 23 | ``` 24 | 25 | ## Laravel 5 26 | 27 | ### Setup 28 | 29 | Add ServiceProvider to the providers array in `app/config/app.php`. 30 | 31 | ``` 32 | AlbertCht\InvisibleReCaptcha\InvisibleReCaptchaServiceProvider::class, 33 | ``` 34 | 35 | > It also supports package discovery for Laravel 5.5. 36 | 37 | ### Configuration 38 | Before you set your config, remember to choose `invisible reCAPTCHA` while applying for keys. 39 | ![invisible_recaptcha_setting](http://i.imgur.com/zIAlKbY.jpg) 40 | 41 | Add `INVISIBLE_RECAPTCHA_SITEKEY`, `INVISIBLE_RECAPTCHA_SECRETKEY` to **.env** file. 42 | 43 | ``` 44 | // required 45 | INVISIBLE_RECAPTCHA_SITEKEY={siteKey} 46 | INVISIBLE_RECAPTCHA_SECRETKEY={secretKey} 47 | 48 | // optional 49 | INVISIBLE_RECAPTCHA_BADGEHIDE=false 50 | INVISIBLE_RECAPTCHA_DATABADGE='bottomright' 51 | INVISIBLE_RECAPTCHA_TIMEOUT=5 52 | INVISIBLE_RECAPTCHA_DEBUG=false 53 | ``` 54 | 55 | > There are three different captcha styles you can set: `bottomright`, `bottomleft`, `inline` 56 | 57 | > If you set `INVISIBLE_RECAPTCHA_BADGEHIDE` to true, you can hide the badge logo. 58 | 59 | > You can see the binding status of those catcha elements on browser console by setting `INVISIBLE_RECAPTCHA_DEBUG` as true. 60 | 61 | ### Usage 62 | 63 | Before you render the captcha, please keep those notices in mind: 64 | 65 | * `render()` or `renderHTML()` function needs to be called within a form element. 66 | * You have to ensure the `type` attribute of your submit button has to be `submit`. 67 | * There can only be one submit button in your form. 68 | 69 | ##### Display reCAPTCHA in Your View 70 | 71 | ```php 72 | {!! app('captcha')->render() !!} 73 | 74 | // or you can use this in blade 75 | @captcha 76 | ``` 77 | 78 | With custom language support: 79 | 80 | ```php 81 | {!! app('captcha')->render('en') !!} 82 | 83 | // or you can use this in blade 84 | @captcha('en') 85 | ``` 86 | 87 | ##### Usage with Javascript frameworks like VueJS: 88 | 89 | The `render()` process includes three distinct sections that can be rendered separately incase you're using the package with a framework like VueJS which throws console errors when `' . PHP_EOL; 118 | } 119 | 120 | /** 121 | * Render the captcha HTML. 122 | * 123 | * @return string 124 | */ 125 | public function renderCaptchaHTML() 126 | { 127 | $html = '
' . PHP_EOL; 128 | if ($this->getOption('hideBadge', false)) { 129 | $html .= '' . PHP_EOL; 130 | } 131 | 132 | $html .= '
getOption('dataBadge', 'bottomright') . '">
'; 134 | return $html; 135 | } 136 | 137 | /** 138 | * Render the footer JS necessary for the recaptcha integration. 139 | * 140 | * @return string 141 | */ 142 | public function renderFooterJS(...$arguments) 143 | { 144 | $lang = Arr::get($arguments, 0); 145 | $nonce = Arr::get($arguments, 1); 146 | 147 | $html = '" . PHP_EOL; 170 | return $html; 171 | } 172 | 173 | /** 174 | * Get debug javascript code. 175 | * 176 | * @return string 177 | */ 178 | public function renderDebug() 179 | { 180 | $html = ''; 181 | foreach (static::DEBUG_ELEMENTS as $element) { 182 | $html .= $this->consoleLog('"Checking element binding of ' . $element . '..."'); 183 | $html .= $this->consoleLog($element . '!==undefined'); 184 | } 185 | 186 | return $html; 187 | } 188 | 189 | /** 190 | * Get console.log function for javascript code. 191 | * 192 | * @return string 193 | */ 194 | public function consoleLog($string) 195 | { 196 | return "console.log({$string});"; 197 | } 198 | 199 | /** 200 | * Verify invisible reCaptcha response. 201 | * 202 | * @param string $response 203 | * @param string $clientIp 204 | * 205 | * @return bool 206 | */ 207 | public function verifyResponse($response, $clientIp) 208 | { 209 | if (empty($response)) { 210 | return false; 211 | } 212 | 213 | $response = $this->sendVerifyRequest([ 214 | 'secret' => $this->secretKey, 215 | 'remoteip' => $clientIp, 216 | 'response' => $response 217 | ]); 218 | 219 | return isset($response['success']) && $response['success'] === true; 220 | } 221 | 222 | /** 223 | * Verify invisible reCaptcha response by Symfony Request. 224 | * 225 | * @param Request $request 226 | * 227 | * @return bool 228 | */ 229 | public function verifyRequest(Request $request) 230 | { 231 | return $this->verifyResponse( 232 | $request->get('g-recaptcha-response'), 233 | $request->getClientIp() 234 | ); 235 | } 236 | 237 | /** 238 | * Send verify request. 239 | * 240 | * @param array $query 241 | * 242 | * @return array 243 | */ 244 | protected function sendVerifyRequest(array $query = []) 245 | { 246 | $response = $this->client->post(static::VERIFY_URI, [ 247 | 'form_params' => $query, 248 | ]); 249 | 250 | return json_decode($response->getBody(), true); 251 | } 252 | 253 | /** 254 | * Getter function of site key 255 | * 256 | * @return string 257 | */ 258 | public function getSiteKey() 259 | { 260 | return $this->siteKey; 261 | } 262 | 263 | /** 264 | * Getter function of secret key 265 | * 266 | * @return string 267 | */ 268 | public function getSecretKey() 269 | { 270 | return $this->secretKey; 271 | } 272 | 273 | /** 274 | * Set options 275 | * 276 | * @param array $options 277 | */ 278 | public function setOptions($options) 279 | { 280 | $this->options = $options; 281 | } 282 | 283 | /** 284 | * Set option 285 | * 286 | * @param string $key 287 | * @param string $value 288 | */ 289 | public function setOption($key, $value) 290 | { 291 | $this->options[$key] = $value; 292 | } 293 | 294 | /** 295 | * Getter function of options 296 | * 297 | * @return string 298 | */ 299 | public function getOptions() 300 | { 301 | return $this->options; 302 | } 303 | 304 | /** 305 | * Get default option value for options. (for support under PHP 7.0) 306 | * 307 | * @param string $key 308 | * @param string $value 309 | * 310 | * @return string 311 | */ 312 | public function getOption($key, $value = null) 313 | { 314 | return array_key_exists($key, $this->options) ? $this->options[$key] : $value; 315 | } 316 | 317 | /** 318 | * Set guzzle client 319 | * 320 | * @param \GuzzleHttp\Client $client 321 | */ 322 | public function setClient(Client $client) 323 | { 324 | $this->client = $client; 325 | } 326 | 327 | /** 328 | * Getter function of guzzle client 329 | * 330 | * @return string 331 | */ 332 | public function getClient() 333 | { 334 | return $this->client; 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /src/InvisibleReCaptchaServiceProvider.php: -------------------------------------------------------------------------------- 1 | bootConfig(); 18 | $this->app['validator']->extend('captcha', function ($attribute, $value) { 19 | return $this->app['captcha']->verifyResponse($value, $this->app['request']->getClientIp()); 20 | }); 21 | } 22 | 23 | /** 24 | * Register any application services. 25 | * 26 | * @return void 27 | */ 28 | public function register() 29 | { 30 | $this->app->singleton('captcha', function ($app) { 31 | return new InvisibleReCaptcha( 32 | $app['config']['captcha.siteKey'], 33 | $app['config']['captcha.secretKey'], 34 | $app['config']['captcha.options'] 35 | ); 36 | }); 37 | 38 | $this->app->afterResolving('blade.compiler', function () { 39 | $this->addBladeDirective($this->app['blade.compiler']); 40 | }); 41 | } 42 | 43 | /** 44 | * Boot configure. 45 | * 46 | * @return void 47 | */ 48 | protected function bootConfig() 49 | { 50 | $path = __DIR__.'/config/captcha.php'; 51 | 52 | $this->mergeConfigFrom($path, 'captcha'); 53 | 54 | if (function_exists('config_path')) { 55 | $this->publishes([$path => config_path('captcha.php')]); 56 | } 57 | } 58 | 59 | /** 60 | * Get the services provided by the provider. 61 | * 62 | * @return array 63 | */ 64 | public function provides() 65 | { 66 | return ['captcha']; 67 | } 68 | 69 | /** 70 | * @param BladeCompiler $blade 71 | * @return void 72 | */ 73 | public function addBladeDirective(BladeCompiler $blade) 74 | { 75 | $blade->directive('captcha', function ($arguments) { 76 | return "renderCaptcha({$arguments}); ?>"; 77 | }); 78 | $blade->directive('captchaPolyfill', function () { 79 | return "renderPolyfill(); ?>"; 80 | }); 81 | $blade->directive('captchaHTML', function () { 82 | return "renderCaptchaHTML(); ?>"; 83 | }); 84 | $blade->directive('captchaScripts', function ($arguments) { 85 | return "renderFooterJS({$arguments}); ?>"; 86 | }); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/config/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/albertcht/invisible-recaptcha/12a4aec36c32e4c4b97884ff6cae97859eb69098/src/config/.gitkeep -------------------------------------------------------------------------------- /src/config/captcha.php: -------------------------------------------------------------------------------- 1 | env('INVISIBLE_RECAPTCHA_SITEKEY'), 6 | // your recapthca secret key. 7 | 'secretKey' => env('INVISIBLE_RECAPTCHA_SECRETKEY'), 8 | // other options to customize your configs 9 | 'options' => [ 10 | // set true if you want to hide your recaptcha badge 11 | 'hideBadge' => env('INVISIBLE_RECAPTCHA_BADGEHIDE', false), 12 | // optional, reposition the reCAPTCHA badge. 'inline' allows you to control the CSS. 13 | // available values: bottomright, bottomleft, inline 14 | 'dataBadge' => env('INVISIBLE_RECAPTCHA_DATABADGE', 'bottomright'), 15 | // timeout value for guzzle client 16 | 'timeout' => env('INVISIBLE_RECAPTCHA_TIMEOUT', 5), 17 | // set true to show binding status on your javascript console 18 | 'debug' => env('INVISIBLE_RECAPTCHA_DEBUG', false) 19 | ] 20 | ]; 21 | -------------------------------------------------------------------------------- /tests/CaptchaTest.php: -------------------------------------------------------------------------------- 1 | false, 18 | 'dataBadge' => 'bottomright', 19 | 'timeout' => 5, 20 | 'debug' => false 21 | ]; 22 | 23 | protected $captcha; 24 | 25 | protected function setUp(): void 26 | { 27 | $this->captcha = new InvisibleReCaptcha( 28 | static::SITE_KEY, 29 | static::SECRET_KEY, 30 | static::OPTIONS 31 | ); 32 | } 33 | 34 | public function testConstructor() 35 | { 36 | $this->assertEquals(static::SITE_KEY, $this->captcha->getSiteKey()); 37 | $this->assertEquals(static::SECRET_KEY, $this->captcha->getSecretKey()); 38 | $this->assertTrue($this->captcha->getClient() instanceof \GuzzleHttp\Client); 39 | } 40 | 41 | public function testGetOptions() 42 | { 43 | $this->assertEquals(static::OPTIONS, $this->captcha->getOptions()); 44 | } 45 | 46 | public function testSetOption() 47 | { 48 | $this->captcha->setOption('debug', true); 49 | $this->captcha->setOption('timeout', 10); 50 | $this->assertEquals(10, $this->captcha->getOption('timeout')); 51 | $this->assertTrue($this->captcha->getOption('debug')); 52 | } 53 | 54 | public function testGetCaptchaJs() 55 | { 56 | $js = 'https://www.google.com/recaptcha/api.js'; 57 | 58 | $this->assertEquals($js, $this->captcha->getCaptchaJs()); 59 | $this->assertEquals($js . '?hl=us', $this->captcha->getCaptchaJs('us')); 60 | } 61 | 62 | public function testJavascriptHasNonce() 63 | { 64 | $this->assertStringContainsString('nonce="nonce-ASDFGHJKL"', $this->captcha->renderFooterJS('us', 'nonce-ASDFGHJKL')); 65 | } 66 | 67 | public function testGetPolyfillJs() 68 | { 69 | $js = 'https://cdn.polyfill.io/v2/polyfill.min.js'; 70 | 71 | $this->assertEquals($js, $this->captcha->getPolyfillJs()); 72 | } 73 | 74 | public function testBladeDirective() 75 | { 76 | $app = Container::getInstance(); 77 | $app->instance('captcha', $this->captcha); 78 | 79 | $blade = new BladeCompiler( 80 | $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock(), 81 | __DIR__ 82 | ); 83 | 84 | $serviceProvider = new InvisibleReCaptchaServiceProvider($app); 85 | $serviceProvider->addBladeDirective($blade); 86 | 87 | $this->assertEquals( 88 | "renderCaptcha(); ?>", 89 | $blade->compileString('@captcha()') 90 | ); 91 | 92 | $this->assertEquals( 93 | "renderCaptcha('us'); ?>", 94 | $blade->compileString("@captcha('us')") 95 | ); 96 | 97 | $this->assertEquals( 98 | "renderCaptcha('us', 'nonce-ASDFGHJKL'); ?>", 99 | $blade->compileString("@captcha('us', 'nonce-ASDFGHJKL')") 100 | ); 101 | 102 | $this->assertEquals( 103 | "renderPolyfill(); ?>", 104 | $blade->compileString('@captchaPolyfill()') 105 | ); 106 | 107 | $this->assertEquals( 108 | "renderCaptchaHTML(); ?>", 109 | $blade->compileString('@captchaHTML()') 110 | ); 111 | 112 | $this->assertEquals( 113 | "renderFooterJS(); ?>", 114 | $blade->compileString('@captchaScripts()') 115 | ); 116 | 117 | $this->assertEquals( 118 | "renderFooterJS('us'); ?>", 119 | $blade->compileString("@captchaScripts('us')") 120 | ); 121 | 122 | $this->assertEquals( 123 | "renderFooterJS('us', 'nonce-ASDFGHJKL'); ?>", 124 | $blade->compileString("@captchaScripts('us', 'nonce-ASDFGHJKL')") 125 | ); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |