Captcha Backend Completed

This commit is contained in:
devansh bawari 2021-06-25 17:06:47 +05:30
parent 122031a159
commit 01fef431b1
6 changed files with 197 additions and 16 deletions

View File

@ -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,
],
];

View File

@ -0,0 +1,127 @@
<?php
namespace Webkul\Customer;
use Webkul\Customer\Contracts\Captcha as CaptchaContract;
class Captcha implements CaptchaContract
{
/**
* Site key.
*
* @var string
*/
protected $siteKey;
/**
* Secret key.
*
* @var string
*/
protected $secretKey;
/**
* Create a new instance.
*
* @return void
*/
public function __construct()
{
$this->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 '<script src="' . $this->getClientEndpoint() . '" async defer></script>';
}
/**
* Render Captcha.
*
* @return string
*/
public function render(): string
{
$htmlAttributes = $this->buildHTMLAttributes($this->getAttributes());
return "<div {$htmlAttributes}></div>";
}
/**
* 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)
: '';
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Webkul\Customer\Contracts;
interface Captcha
{
const CLIENT_ENDPOINT = 'https://www.google.com/recaptcha/api.js';
const SITE_VERIFY_ENDPOINT = 'https://google.com/recaptcha/api/siteverify';
}

View File

@ -0,0 +1,18 @@
<?php
namespace Webkul\Customer\Facades;
use Illuminate\Support\Facades\Facade;
class Captcha extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'captcha';
}
}

View File

@ -2,8 +2,8 @@
namespace Webkul\Customer\Http\Controllers;
use Illuminate\Support\Facades\Event;
use Cookie;
use Illuminate\Support\Facades\Event;
class SessionController extends Controller
{
@ -18,10 +18,10 @@ class SessionController extends Controller
* Create a new Repository instance.
*
* @return void
*/
*/
public function __construct()
{
$this->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']);
}
}
}

View File

@ -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
*/