From 01fef431b164f985e12c94a5ef906253f291e9d5 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Fri, 25 Jun 2021 17:06:47 +0530 Subject: [PATCH] Captcha Backend Completed --- config/app.php | 30 +++-- packages/Webkul/Customer/src/Captcha.php | 127 ++++++++++++++++++ .../Webkul/Customer/src/Contracts/Captcha.php | 10 ++ .../Webkul/Customer/src/Facades/Captcha.php | 18 +++ .../Http/Controllers/SessionController.php | 9 +- .../src/Providers/CustomerServiceProvider.php | 19 ++- 6 files changed, 197 insertions(+), 16 deletions(-) create mode 100644 packages/Webkul/Customer/src/Captcha.php create mode 100644 packages/Webkul/Customer/src/Contracts/Captcha.php create mode 100644 packages/Webkul/Customer/src/Facades/Captcha.php diff --git a/config/app.php b/config/app.php index 1aff706f6..ea88f140c 100755 --- a/config/app.php +++ b/config/app.php @@ -297,6 +297,11 @@ return [ 'aliases' => [ + /** + * Laravel + * + * Place your aliases in alphabetical order. + */ 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, @@ -330,18 +335,25 @@ return [ 'URL' => Illuminate\Support\Facades\URL::class, 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, - 'Datagrid' => Webkul\Ui\DataGrid\Facades\DataGrid::class, - 'ProductGrid' => Webkul\Ui\DataGrid\Facades\ProductGrid::class, - 'Image' => Intervention\Image\Facades\Image::class, + + /** + * Bagisto + * + * Place your aliases in alphabetical order. + */ + 'Captcha' => Webkul\Customer\Facades\Captcha::class, 'Cart' => Webkul\Checkout\Facades\Cart::class, - 'Core' => Webkul\Core\Facades\Core::class, - 'DbView' => Flynsarmy\DbBladeCompiler\Facades\DbView::class, - 'PDF' => Barryvdh\DomPDF\Facade::class, - 'Excel' => Maatwebsite\Excel\Facades\Excel::class, 'Concord' => Konekt\Concord\Facades\Concord::class, - 'Helper' => Konekt\Concord\Facades\Helper::class, + 'Core' => Webkul\Core\Facades\Core::class, + 'Datagrid' => Webkul\Ui\DataGrid\Facades\DataGrid::class, + 'DbView' => Flynsarmy\DbBladeCompiler\Facades\DbView::class, 'Debugbar' => Barryvdh\Debugbar\Facade::class, + 'Excel' => Maatwebsite\Excel\Facades\Excel::class, + 'Helper' => Konekt\Concord\Facades\Helper::class, + 'Image' => Intervention\Image\Facades\Image::class, + 'PDF' => Barryvdh\DomPDF\Facade::class, 'ProductImage' => Webkul\Product\Facades\ProductImage::class, - 'ProductVideo' => Webkul\Product\Facades\ProductVideo::class + 'ProductGrid' => Webkul\Ui\DataGrid\Facades\ProductGrid::class, + 'ProductVideo' => Webkul\Product\Facades\ProductVideo::class, ], ]; diff --git a/packages/Webkul/Customer/src/Captcha.php b/packages/Webkul/Customer/src/Captcha.php new file mode 100644 index 000000000..f382366b7 --- /dev/null +++ b/packages/Webkul/Customer/src/Captcha.php @@ -0,0 +1,127 @@ +siteKey = '6LcUYlYbAAAAALyF7D5IrwZgufgXBwBrjXlcndAt'; + + $this->secretKey = '6LcUYlYbAAAAAKi3vUJ62a9QEk4JvPfbLoizTajz'; + } + + /** + * Get client endpoint. + * + * @return string + */ + public function getClientEndpoint(): string + { + return static::CLIENT_ENDPOINT; + } + + /** + * Get site verify endpoint. + * + * @return string + */ + public function getSiteVerifyEndpoint(): string + { + return static::SITE_VERIFY_ENDPOINT; + } + + /** + * Render JS. + * + * @return string + */ + public function renderJS(): string + { + return ''; + } + + /** + * Render Captcha. + * + * @return string + */ + public function render(): string + { + $htmlAttributes = $this->buildHTMLAttributes($this->getAttributes()); + + return "
"; + } + + /** + * Validate response. + * + * @return bool + */ + public function validateResponse($response): bool + { + $client = new \GuzzleHttp\Client(); + + $response = $client->post(static::SITE_VERIFY_ENDPOINT, [ + 'query' => [ + 'secret' => '6LcUYlYbAAAAAKi3vUJ62a9QEk4JvPfbLoizTajz', + 'response' => $response + ] + ]); + + return json_decode($response->getBody())->success; + } + + /** + * Get attributes. + * + * @return array + */ + protected function getAttributes(): array + { + return [ + 'class' => 'g-recaptcha', + 'data-sitekey' => $this->siteKey, + ]; + } + + /** + * Build attributes. + * + * @param array $attributes + * @return string + */ + protected function buildHTMLAttributes(array $attributes): string + { + $htmlAttributes = []; + + foreach ($attributes as $key => $value) { + $htmlAttributes[] = "{$key}=\"{$value}\""; + } + + return count($htmlAttributes) + ? implode(' ', $htmlAttributes) + : ''; + } +} diff --git a/packages/Webkul/Customer/src/Contracts/Captcha.php b/packages/Webkul/Customer/src/Contracts/Captcha.php new file mode 100644 index 000000000..e38f8243d --- /dev/null +++ b/packages/Webkul/Customer/src/Contracts/Captcha.php @@ -0,0 +1,10 @@ +middleware('customer')->except(['show','create']); + $this->middleware('customer')->except(['show', 'create']); $this->_config = request('_config'); } @@ -50,6 +50,7 @@ class SessionController extends Controller $this->validate(request(), [ 'email' => 'required|email', 'password' => 'required', + 'g-recaptcha-response' => 'required|captcha', ]); if (! auth()->guard('customer')->attempt(request(['email', 'password']))) { @@ -98,4 +99,4 @@ class SessionController extends Controller return redirect()->route($this->_config['redirect']); } -} \ No newline at end of file +} diff --git a/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php b/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php index f12f69d6e..8bea3c8cf 100755 --- a/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php +++ b/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php @@ -3,12 +3,18 @@ namespace Webkul\Customer\Providers; use Illuminate\Database\Eloquent\Factory as EloquentFactory; -use Illuminate\Support\ServiceProvider; use Illuminate\Routing\Router; +use Illuminate\Support\ServiceProvider; +use Webkul\Customer\Captcha; use Webkul\Customer\Http\Middleware\RedirectIfNotCustomer; class CustomerServiceProvider extends ServiceProvider { + /** + * Bootstrap application services. + * + * @return void + */ public function boot(Router $router) { $router->aliasMiddleware('customer', RedirectIfNotCustomer::class); @@ -16,6 +22,10 @@ class CustomerServiceProvider extends ServiceProvider $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'customer'); $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + + $this->app['validator']->extend('captcha', function ($attribute, $value, $parameters) { + return $this->app['captcha']->validateResponse($value); + }); } /** @@ -27,13 +37,16 @@ class CustomerServiceProvider extends ServiceProvider public function register() { $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); + + $this->app->singleton('captcha', function ($app) { + return new Captcha(); + }); } /** * Register factories. * - * @param string $path - * + * @param string $path * @return void * @throws \Illuminate\Contracts\Container\BindingResolutionException */