Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recaptcha Added #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Hooks/Handlers/AdminMenuHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FluentAuth\App\Hooks\Handlers;

use FluentAuth\App\Helpers\Helper;
use FluentAuth\App\Services\ReCaptcha\Recaptcha;

class AdminMenuHandler
{
Expand Down Expand Up @@ -47,6 +48,8 @@ public function addMenu()
array($this, 'render')
);

Recaptcha::renderMenuPage($permission, 'fluent-auth', array($this, 'render'));

add_submenu_page(
'fluent-auth',
__('Social Login', 'fluent-support'),
Expand Down
1 change: 1 addition & 0 deletions app/Hooks/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
(new \FluentAuth\App\Hooks\Handlers\TwoFaHandler())->register();
(new \FluentAuth\App\Hooks\Handlers\BasicTasksHandler())->register();

\FluentAuth\App\Services\ReCaptcha\Recaptcha::register();
34 changes: 34 additions & 0 deletions app/Http/Controllers/ReCaptchaController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace FluentAuth\App\Http\Controllers;

use FluentAuth\App\Services\ReCaptcha\Recaptcha;

class ReCaptchaController
{
public static function getSettings()
{
return [
'data' => Recaptcha::settings()->get()
];
}

public static function updateSettings(\WP_REST_Request $request)
{
// :Todo validate request before save

$data = [
'site_key' => sanitize_text_field($request['site_key']),
'secret_key' => sanitize_text_field($request['secret_key']),
'enable_recaptcha' => sanitize_text_field($request['enable_recaptcha']),
'enable_on_shortcode_login' => sanitize_text_field($request['enable_on_shortcode_login']),
];

$response = Recaptcha::settings()->update($data);

return [
'message' => __('Settings has been updated', 'fluent-security'),
'data' => $response
];
}
}
4 changes: 3 additions & 1 deletion app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
->get('social-auth-settings', ['\FluentAuth\App\Http\Controllers\SocialAuthApiController', 'getSettings'], $permissions)
->post('social-auth-settings', ['\FluentAuth\App\Http\Controllers\SocialAuthApiController', 'saveSettings'], $permissions)
->get('auth-forms-settings', ['\FluentAuth\App\Http\Controllers\SettingsController', 'getAuthFormSettings'], $permissions)
->post('auth-forms-settings', ['\FluentAuth\App\Http\Controllers\SettingsController', 'saveAuthFormSettings'], $permissions);
->post('auth-forms-settings', ['\FluentAuth\App\Http\Controllers\SettingsController', 'saveAuthFormSettings'], $permissions)
->get('recaptcha-settings', ['\FluentAuth\App\Http\Controllers\ReCaptchaController', 'getSettings'], $permissions)
->post('recaptcha-settings', ['\FluentAuth\App\Http\Controllers\ReCaptchaController', 'updateSettings'], $permissions);
40 changes: 40 additions & 0 deletions app/Services/ReCaptcha/MenuPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace FluentAuth\App\Services\ReCaptcha;

class MenuPage
{
private $permission;
private $menuSlug;
private $cb;
public function __construct($permission, $menuSlug, $cb)
{
$this->permission = $permission;
$this->menuSlug = $menuSlug;
$this->cb = $cb;
}

public function registerSubmenuPage($pageTitle, $menuTitle, $menuSlug, $cb = null)
{
add_submenu_page(
$this->menuSlug,
$pageTitle,
$menuTitle,
$this->permission,
$menuSlug,
$cb ?: $this->cb
);
}

public function registerMenuPage($pageTitle, $menuTitle, $menuSlug, $cb = null)
{
add_menu_page(
$pageTitle,
$menuTitle,
$this->permission,
$menuSlug,
$cb ?: $this->cb
);
}

}
59 changes: 59 additions & 0 deletions app/Services/ReCaptcha/Recaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace FluentAuth\App\Services\ReCaptcha;


/**
* Class Recaptcha
* @package FluentAuth\App\Services\ReCaptcha
* @method static void register()
* @method static void settings()
* @method static void renderMenuPage($permission, $menuSlug, $cb)
*/
class Recaptcha
{
public static $service = null;

public function __construct()
{
if (is_null(self::$service)) {
self::$service = new RecaptchaService();
}

return self::$service;
}

public function __call($name, $arguments)
{
return call_user_func_array([self::$service, $name], $arguments);
}

public static function __callStatic($name, $arguments)
{
if (is_null(self::$service)) {
self::$service = new RecaptchaService();
}

return call_user_func_array([self::$service, $name], $arguments);
}

public function __get($name)
{
return self::$service->{$name};
}

public function __set($name, $value)
{
self::$service->{$name} = $value;
}

public function __isset($name)
{
return isset(self::$service->{$name});
}

public function __unset($name)
{
unset(self::$service->{$name});
}
}
172 changes: 172 additions & 0 deletions app/Services/ReCaptcha/RecaptchaService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace FluentAuth\App\Services\ReCaptcha;

class RecaptchaService
{
public static $settings = null;

public function __construct()
{
if (is_null(self::$settings)) {
self::$settings = new Settings();
}
}

public function renderMenuPage($permission, $menuSlug, $cb)
{
$menuPage = new MenuPage($permission, $menuSlug, $cb);
$menuPage->registerSubmenuPage('ReCaptcha', 'ReCaptcha', 'fluent-auth#/recaptcha');
}

public function settings()
{
if (is_null(self::$settings)) {
self::$settings = new Settings();
}

return self::$settings;
}


public function register()
{
add_action('login_form', array($this, 'renderCaptcha'));
add_action('register_form', array($this, 'renderCaptcha'));
add_action('signup_extra_fields', array($this, 'renderCaptcha'));
add_action('lostpassword_form', array($this, 'renderCaptcha'));
add_action('login_enqueue_scripts', array($this, 'enqueueScripts'));
add_action('wp_ajax_nopriv_fluent_auth_recaptcha_verify', array($this, 'verifyRecaptcha'));

}

public function enqueueScripts()
{
$settings = self::settings();

if (!$settings->enabled()) {
return;
}

wp_enqueue_script('fluent-auth-recaptcha', 'https://www.google.com/recaptcha/api.js', [], '1.0', true);
wp_enqueue_script('fluent-auth-recaptcha-script', FLUENT_AUTH_PLUGIN_URL . '/src/public/recaptcha.js', ['fluent-auth-recaptcha', 'jquery'], '1.0', true);

wp_localize_script('fluent-auth-recaptcha-script', 'fluent_auth_recaptcha', [
'fls_ajax_url' => admin_url('admin-ajax.php'),
'fls_action' => 'fluent_auth_recaptcha_verify'
]);
}

public function renderCaptcha()
{

$this->renderCaptchaStyle();

$settings = self::settings();

if ($settings->enabled()) {
$siteKey = $settings->site_key;
return printf('<div class="g-recaptcha" data-sitekey="%s"></div>', $siteKey);
}

return $this;
}

public function verifyRecaptcha()
{
$settings = self::settings();

$secretKey = $settings->secret_key;
$token = $_POST['token'];
$remoteIp = $_SERVER['REMOTE_ADDR'];


if(!$secretKey) {
wp_send_json([
'success' => false,
'message' => 'Please add the secret key'
], 422);

return;
}


if(!$token) {
wp_send_json([
'success' => false,
'message' => 'Please Check the Captcha'
], 422);

return;
}

$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$token&remoteip=$remoteIp";

$response = wp_remote_get($url);

if (is_wp_error($response)) {

wp_send_json([
'success' => false,
'message' => 'Captcha verification failed'
], 500);

return;
}

$response = json_decode(wp_remote_retrieve_body($response), true);

if (!$response['success']) {

wp_send_json([
'success' => false,
'message' => 'Invalid captcha'
], 422);

return;
}

wp_send_json($response);
}

public function registerShortcodeCaptcha()
{
$settings = self::settings();

if(!$settings->enabled()) {
return;
}

if($settings->enable_on_shortcode_login !== 'yes') {
return;
}

add_action('wp_loaded', function() use ($settings){

add_filter('login_form_middle', function($data) use ($settings){
$data .= '<div class="g-recaptcha" data-sitekey="'. $settings->site_key .'"></div>';
return $data;
});

wp_enqueue_script('fluent-auth-recaptcha', 'https://www.google.com/recaptcha/api.js', [], '1.0', true);
wp_enqueue_script('fluent-auth-recaptcha-script', FLUENT_AUTH_PLUGIN_URL . '/src/public/recaptcha.js', ['fluent-auth-recaptcha', 'jquery'], '1.0', true);

wp_localize_script('fluent-auth-recaptcha-script', 'fluent_auth_recaptcha', [
'fls_ajax_url' => admin_url('admin-ajax.php'),
'fls_action' => 'fluent_auth_recaptcha_verify'
]);
});
}

public function renderCaptchaStyle()
{
echo '<style>
.g-recaptcha {
margin-bottom: 15px;
}
#login {
width: 352px;
}
</style>';
}
}
Loading