From 1d0ae52c7b5d70eab424a32f2bf56ba1d6f5e621 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Thu, 5 Sep 2019 12:23:46 +0800 Subject: [PATCH] Switch to another captcha library --- app/Http/Controllers/AuthController.php | 10 ++ app/Rules/Captcha.php | 4 +- composer.json | 2 +- composer.lock | 192 +++++++----------------- routes/web.php | 2 +- tests/RulesTest/CaptchaTest.php | 15 +- 6 files changed, 77 insertions(+), 148 deletions(-) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 42dfff04..0f665dfe 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -229,6 +229,16 @@ class AuthController extends Controller return json(trans('auth.reset.success'), 0); } + public function captcha(\Gregwar\Captcha\CaptchaBuilder $builder) + { + $builder->build(100, 34); + session(['captcha' => $builder->getPhrase()]); + return response($builder->output(), 200, [ + 'Content-Type' => 'image/jpeg', + 'Cache-Control' => 'no-store', + ]); + } + public function fillEmail(Request $request) { $email = $this->validate($request, ['email' => 'required|email|unique:users'])['email']; diff --git a/app/Rules/Captcha.php b/app/Rules/Captcha.php index d9bc3876..df2130eb 100644 --- a/app/Rules/Captcha.php +++ b/app/Rules/Captcha.php @@ -2,6 +2,7 @@ namespace App\Rules; +use Gregwar\Captcha\CaptchaBuilder; use Illuminate\Contracts\Validation\Rule; class Captcha implements Rule @@ -35,7 +36,8 @@ class Captcha implements Rule } } - return captcha_check($value); + $builder = new CaptchaBuilder(session()->pull('captcha')); + return $builder->testPhrase($value); } public function message() diff --git a/composer.json b/composer.json index 6833932f..e33c7f00 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "nesbot/carbon": "^2.0", "devitek/yaml-translation": "^4.1.0", "composer/semver": "^1.4", - "mews/captcha": "^2.2", + "gregwar/captcha": "1.*", "guzzlehttp/guzzle": "^6.3", "doctrine/dbal": "^2.9", "tymon/jwt-auth": "dev-develop", diff --git a/composer.lock b/composer.lock index 44669ec7..33cf3055 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a34a838d8b34983c46fde89c68ebab5a", + "content-hash": "6a5e7188b9f242f23c36e54da0dada9b", "packages": [ { "name": "composer/ca-bundle", @@ -797,6 +797,59 @@ "homepage": "https://github.com/firebase/php-jwt", "time": "2017-06-27T22:17:23+00:00" }, + { + "name": "gregwar/captcha", + "version": "v1.1.7", + "source": { + "type": "git", + "url": "https://github.com/Gregwar/Captcha.git", + "reference": "cf953dd79748406e0292cea8c565399681e4d345" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Gregwar/Captcha/zipball/cf953dd79748406e0292cea8c565399681e4d345", + "reference": "cf953dd79748406e0292cea8c565399681e4d345", + "shasum": "" + }, + "require": { + "ext-gd": "*", + "ext-mbstring": "*", + "php": ">=5.3.0", + "symfony/finder": "*" + }, + "require-dev": { + "phpunit/phpunit": "^6.4" + }, + "type": "captcha", + "autoload": { + "psr-4": { + "Gregwar\\": "src/Gregwar" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Grégoire Passault", + "email": "g.passault@gmail.com", + "homepage": "http://www.gregwar.com/" + }, + { + "name": "Jeremy Livingston", + "email": "jeremy.j.livingston@gmail.com" + } + ], + "description": "Captcha generator", + "homepage": "https://github.com/Gregwar/Captcha", + "keywords": [ + "bot", + "captcha", + "spam" + ], + "time": "2018-08-17T22:57:28+00:00" + }, { "name": "guzzlehttp/guzzle", "version": "6.3.3", @@ -984,76 +1037,6 @@ ], "time": "2019-07-01T23:21:34+00:00" }, - { - "name": "intervention/image", - "version": "2.5.0", - "source": { - "type": "git", - "url": "https://github.com/Intervention/image.git", - "reference": "39eaef720d082ecc54c64bf54541c55f10db546d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Intervention/image/zipball/39eaef720d082ecc54c64bf54541c55f10db546d", - "reference": "39eaef720d082ecc54c64bf54541c55f10db546d", - "shasum": "" - }, - "require": { - "ext-fileinfo": "*", - "guzzlehttp/psr7": "~1.1", - "php": ">=5.4.0" - }, - "require-dev": { - "mockery/mockery": "~0.9.2", - "phpunit/phpunit": "^4.8 || ^5.7" - }, - "suggest": { - "ext-gd": "to use GD library based image processing.", - "ext-imagick": "to use Imagick based image processing.", - "intervention/imagecache": "Caching extension for the Intervention Image library" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.4-dev" - }, - "laravel": { - "providers": [ - "Intervention\\Image\\ImageServiceProvider" - ], - "aliases": { - "Image": "Intervention\\Image\\Facades\\Image" - } - } - }, - "autoload": { - "psr-4": { - "Intervention\\Image\\": "src/Intervention/Image" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Oliver Vogel", - "email": "oliver@olivervogel.com", - "homepage": "http://olivervogel.com/" - } - ], - "description": "Image handling and manipulation library with support for Laravel integration", - "homepage": "http://image.intervention.io/", - "keywords": [ - "gd", - "image", - "imagick", - "laravel", - "thumbnail", - "watermark" - ], - "time": "2019-06-24T14:06:31+00:00" - }, { "name": "laravel/framework", "version": "v5.8.33", @@ -1538,73 +1521,6 @@ ], "time": "2019-05-05T09:22:01+00:00" }, - { - "name": "mews/captcha", - "version": "2.3.0", - "source": { - "type": "git", - "url": "https://github.com/mewebstudio/captcha.git", - "reference": "2c9efa19d7d7ae56bed5f23e1c4c42ea5d01fc1a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/mewebstudio/captcha/zipball/2c9efa19d7d7ae56bed5f23e1c4c42ea5d01fc1a", - "reference": "2c9efa19d7d7ae56bed5f23e1c4c42ea5d01fc1a", - "shasum": "" - }, - "require": { - "ext-gd": "*", - "illuminate/config": "~5.0", - "illuminate/filesystem": "~5.0", - "illuminate/hashing": "~5.0", - "illuminate/support": "~5.0", - "intervention/image": "~2.2", - "php": ">=5.4" - }, - "require-dev": { - "mockery/mockery": "0.9.*", - "phpunit/phpunit": "~4.1" - }, - "type": "package", - "extra": { - "laravel": { - "providers": [ - "Mews\\Captcha\\CaptchaServiceProvider" - ], - "aliases": { - "Captcha": "Mews\\Captcha\\Facades\\Captcha" - } - } - }, - "autoload": { - "psr-4": { - "Mews\\Captcha\\": "src/" - }, - "files": [ - "src/helpers.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Muharrem ERİN", - "role": "Developer", - "email": "me@mewebstudio.com", - "homepage": "https://github.com/mewebstudio" - } - ], - "description": "Laravel 5 Captcha Package", - "homepage": "https://github.com/mewebstudio/captcha", - "keywords": [ - "captcha", - "laravel5 Captcha", - "laravel5 Security" - ], - "time": "2019-08-16T21:57:24+00:00" - }, { "name": "monolog/monolog", "version": "1.24.0", diff --git a/routes/web.php b/routes/web.php index dea11c03..16852a85 100644 --- a/routes/web.php +++ b/routes/web.php @@ -30,7 +30,7 @@ Route::group(['prefix' => 'auth'], function () { }); Route::any('/logout', 'AuthController@logout'); - Route::any('/captcha', '\Mews\Captcha\CaptchaController@getCaptcha'); + Route::any('/captcha', 'AuthController@captcha'); Route::post('/login', 'AuthController@handleLogin'); Route::post('/register', 'AuthController@handleRegister'); diff --git a/tests/RulesTest/CaptchaTest.php b/tests/RulesTest/CaptchaTest.php index 5b6648ba..8377ff00 100644 --- a/tests/RulesTest/CaptchaTest.php +++ b/tests/RulesTest/CaptchaTest.php @@ -12,15 +12,16 @@ class CaptchaTest extends TestCase { public function testCharactersCaptcha() { - app()->instance('captcha', new class { - public function check() - { - return true; - } - }); + session(['captcha' => 'abc']); $rule = resolve(Captcha::class); - $this->assertTrue($rule->passes('captcha', 'any')); + $this->assertFalse($rule->passes('captcha', 'abcd')); $this->assertEquals(trans('validation.captcha'), $rule->message()); + $this->assertNull(session('captcha')); + + session(['captcha' => 'abc']); + $rule = resolve(Captcha::class); + $this->assertTrue($rule->passes('captcha', 'abc')); + $this->assertNull(session('captcha')); } public function testRecaptcha()