improve handel requests
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin Name: Dabestaniha Authenticate Bridge
|
* Plugin Name: Dabestaniha Authenticate Bridge
|
||||||
* Description: Authenticate WordPress user via json web token from Mahakiha Application.
|
* Description: Authenticate WordPress user via json web token from Mahakiha Application.
|
||||||
@@ -10,81 +11,11 @@
|
|||||||
|
|
||||||
require_once __DIR__.'/vendor/autoload.php';
|
require_once __DIR__.'/vendor/autoload.php';
|
||||||
|
|
||||||
use Firebase\JWT;
|
use Dabestaniha\AuthenticateBridge\App\Http\Controllers\AuthenticateController;
|
||||||
use Dabestaniha\AuthenticateBridge\App\Http\Requests\DabestanihaAuthenticateRequest;
|
use Dabestaniha\AuthenticateBridge\App\Http\Controllers\SettingsPageMenuController;
|
||||||
|
use Dabestaniha\AuthenticateBridge\App\Http\Controllers\SettingsPageFormController;
|
||||||
|
|
||||||
add_action('init', function () {
|
add_action('init', fn() => AuthenticateController::resolve());
|
||||||
$request = DabestanihaAuthenticateRequest::make();
|
|
||||||
|
|
||||||
if (!$request->shouldContinue()) {
|
add_action('admin_menu', fn() => SettingsPageMenuController::resolve());
|
||||||
return;
|
add_action('admin_init', fn() => SettingsPageFormController::resolve());
|
||||||
}
|
|
||||||
|
|
||||||
$request->validate();
|
|
||||||
$token = $request->string('token');
|
|
||||||
|
|
||||||
$payload = decrypt_jwt_token($token);
|
|
||||||
|
|
||||||
$identifier_name = get_configured_option('user-identifier');
|
|
||||||
$identifier_value = $payload[$identifier_name] ?? null;
|
|
||||||
|
|
||||||
$name = $payload['name'] ?? config('da.translates.new-user');
|
|
||||||
$user_id = find_or_create_user($identifier_name, $identifier_value, $name);
|
|
||||||
|
|
||||||
login_user($user_id);
|
|
||||||
|
|
||||||
$redirect_path = get_configured_option('after-login-route');
|
|
||||||
wp_redirect(home_url($redirect_path));
|
|
||||||
|
|
||||||
exit();
|
|
||||||
});
|
|
||||||
|
|
||||||
require_once 'settings-page.php';
|
|
||||||
|
|
||||||
function decrypt_jwt_token(string $token): array
|
|
||||||
{
|
|
||||||
$jwt_secret = get_configured_option('jwt-secret');
|
|
||||||
|
|
||||||
try {
|
|
||||||
$decoded = (array) JWT\JWT::decode($token, new JWT\Key($jwt_secret, 'HS256'));
|
|
||||||
return (array) $decoded['data'];
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function find_or_create_user(string $identifier_name, string $identifier_value, string $name): int
|
|
||||||
{
|
|
||||||
$user = get_user_by($identifier_name, $identifier_value);
|
|
||||||
|
|
||||||
if ($user) {
|
|
||||||
return $user->ID;
|
|
||||||
}
|
|
||||||
$random_password = wp_generate_password(12, true);
|
|
||||||
$username = $identifier_value;
|
|
||||||
|
|
||||||
if (username_exists($username)) {
|
|
||||||
$username .= '_'.wp_generate_password(4, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
$user_id = wp_create_user($username, $random_password);
|
|
||||||
|
|
||||||
if (is_wp_error($user_id)) {
|
|
||||||
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
|
|
||||||
}
|
|
||||||
|
|
||||||
wp_update_user([
|
|
||||||
'ID' => $user_id,
|
|
||||||
'display_name' => $name,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return $user_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
function login_user(int $user_id): bool
|
|
||||||
{
|
|
||||||
wp_set_auth_cookie($user_id);
|
|
||||||
wp_set_current_user($user_id);
|
|
||||||
do_action('wp_login', get_userdata($user_id)->user_login, get_userdata($user_id));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,117 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
add_action('admin_menu', function () {
|
|
||||||
add_options_page(
|
|
||||||
config('da.translations.settings-page-title'),
|
|
||||||
config('da.translations.settings-page-menu'),
|
|
||||||
'manage_options',
|
|
||||||
config('da.settings-page'),
|
|
||||||
'dabestaniha_authenticate_settings_page'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
add_action('admin_init', function () {
|
|
||||||
|
|
||||||
foreach (config('da.options') as $configured => $options_key) {
|
|
||||||
register_setting(config('da.settings-group'), $options_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
add_settings_section(
|
|
||||||
config('da.section-id'),
|
|
||||||
config('da.translations.section-id'),
|
|
||||||
null,
|
|
||||||
config('da.settings-page')
|
|
||||||
);
|
|
||||||
|
|
||||||
add_settings_field(
|
|
||||||
config('da.options.user-identifier'),
|
|
||||||
config('da.translations.user-identifier'),
|
|
||||||
function () {
|
|
||||||
$name = config('da.options.user-identifier');
|
|
||||||
$value = get_option($name, 'mobile');
|
|
||||||
|
|
||||||
$html = "<select name='$name'>";
|
|
||||||
|
|
||||||
foreach (config('da.user-identifiers') as $identifier) {
|
|
||||||
$selected = $value === $identifier ? "selected='selected'" : null;
|
|
||||||
$translate = config("da.translations.$identifier");
|
|
||||||
$html .= "<option value='$identifier' $selected>$translate</option>";
|
|
||||||
}
|
|
||||||
|
|
||||||
$html .= "</select>";
|
|
||||||
echo $html;
|
|
||||||
},
|
|
||||||
config('da.settings-page'),
|
|
||||||
config('da.section-id')
|
|
||||||
);
|
|
||||||
|
|
||||||
add_settings_field(
|
|
||||||
config('da.options.jwt-secret'),
|
|
||||||
config('da.translations.jwt-secret'),
|
|
||||||
function () {
|
|
||||||
$name = config('da.options.jwt-secret');
|
|
||||||
$value = esc_attr(get_option($name, ''));
|
|
||||||
echo "<input type='text' name='$name' value='$value' class='regular-text ltr' />";
|
|
||||||
},
|
|
||||||
config('da.settings-page'),
|
|
||||||
config('da.section-id')
|
|
||||||
);
|
|
||||||
|
|
||||||
add_settings_field(
|
|
||||||
config('da.options.login-route'),
|
|
||||||
config('da.translations.login-route'),
|
|
||||||
function () {
|
|
||||||
$name = config('da.options.login-route');
|
|
||||||
$value = esc_attr(get_option($name, config('da.login_route_default')));
|
|
||||||
echo '<div class="ltr" style="float: right; display: flex; align-items: center; gap: 5px;">';
|
|
||||||
echo '<span style="color: #666;">'.home_url('/').'</span>';
|
|
||||||
echo "<input type='text' name='$name' value='$value' class='regular-text' />";
|
|
||||||
echo '</div>';
|
|
||||||
},
|
|
||||||
config('da.settings-page'),
|
|
||||||
config('da.section-id')
|
|
||||||
);
|
|
||||||
|
|
||||||
add_settings_field(
|
|
||||||
config('da.options.after-login-route'),
|
|
||||||
config('da.translations.after-login-route'),
|
|
||||||
function () {
|
|
||||||
$name = config('da.options.after-login-route');
|
|
||||||
$value = esc_attr(get_option($name, config('da.after_login_route_default')));
|
|
||||||
echo '<div class="ltr" style="float: right; display: flex; align-items: center; gap: 5px;">';
|
|
||||||
echo '<span style="color: #666;">'.home_url('/').'</span>';
|
|
||||||
echo "<input type='text' name='$name' value='$value' class='regular-text' />";
|
|
||||||
echo '</div>';
|
|
||||||
},
|
|
||||||
config('da.settings-page'),
|
|
||||||
config('da.section-id')
|
|
||||||
);
|
|
||||||
|
|
||||||
$loginRouteOptionName = config('da.options.login-route');
|
|
||||||
add_filter("pre_update_option_{$loginRouteOptionName}", function ($value) {
|
|
||||||
return trim($value, '/');
|
|
||||||
});
|
|
||||||
|
|
||||||
$afterLoginRouteOptionName = config('da.options.after-login-route');
|
|
||||||
add_filter("pre_update_option_{$afterLoginRouteOptionName}", function ($value) {
|
|
||||||
return trim($value, '/');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
function dabestaniha_authenticate_settings_page(): void
|
|
||||||
{
|
|
||||||
?>
|
|
||||||
<div class="wrap">
|
|
||||||
<h1><?= config('da.translations.settings-page-title') ?></h1>
|
|
||||||
<form method="post" action="options.php">
|
|
||||||
<?php
|
|
||||||
settings_fields(config('da.settings-group'));
|
|
||||||
do_settings_sections(config('da.settings-page'));
|
|
||||||
submit_button();
|
|
||||||
?>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
<?php
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Dabestaniha\AuthenticateBridge\App\Http\Controllers;
|
||||||
|
|
||||||
|
use Dabestaniha\AuthenticateBridge\App\Http\Requests\DabestanihaAuthenticateRequest;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use Firebase\JWT;
|
||||||
|
|
||||||
|
class AuthenticateController extends InvocableController
|
||||||
|
{
|
||||||
|
public function __invoke(DabestanihaAuthenticateRequest $request)
|
||||||
|
{
|
||||||
|
$token = $request->string('token');
|
||||||
|
|
||||||
|
$payload = $this->decrypt_jwt_token($token);
|
||||||
|
|
||||||
|
$identifier_name = get_configured_option('user-identifier');
|
||||||
|
$identifier_value = $payload[$identifier_name] ?? null;
|
||||||
|
|
||||||
|
$name = $payload['name'] ?? config('da.translates.new-user');
|
||||||
|
$user_id = $this->find_or_create_user($identifier_name, $identifier_value, $name);
|
||||||
|
|
||||||
|
$this->login_user($user_id);
|
||||||
|
|
||||||
|
$redirect_path = get_configured_option('after-login-route');
|
||||||
|
wp_redirect(home_url($redirect_path));
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function decrypt_jwt_token(string $token): array
|
||||||
|
{
|
||||||
|
$jwt_secret = get_configured_option('jwt-secret');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$decoded = (array) JWT\JWT::decode($token, new JWT\Key($jwt_secret, 'HS256'));
|
||||||
|
return (array) $decoded['data'];
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find_or_create_user(string $identifier_name, string $identifier_value, string $name): int
|
||||||
|
{
|
||||||
|
$user = get_user_by($identifier_name, $identifier_value);
|
||||||
|
|
||||||
|
if ($user) {
|
||||||
|
return $user->ID;
|
||||||
|
}
|
||||||
|
$random_password = wp_generate_password(12, true);
|
||||||
|
$username = $identifier_value;
|
||||||
|
|
||||||
|
if (username_exists($username)) {
|
||||||
|
$username .= '_'.wp_generate_password(4, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user_id = wp_create_user($username, $random_password);
|
||||||
|
|
||||||
|
if (is_wp_error($user_id)) {
|
||||||
|
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_update_user([
|
||||||
|
'ID' => $user_id,
|
||||||
|
'display_name' => $name,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $user_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function login_user(int $user_id): bool
|
||||||
|
{
|
||||||
|
wp_set_auth_cookie($user_id);
|
||||||
|
wp_set_current_user($user_id);
|
||||||
|
do_action('wp_login', get_userdata($user_id)->user_login, get_userdata($user_id));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Dabestaniha\AuthenticateBridge\App\Http\Controllers;
|
||||||
|
|
||||||
|
use Dabestaniha\AuthenticateBridge\App\Http\Requests\BaseFormRequest;
|
||||||
|
use ReflectionMethod;
|
||||||
|
|
||||||
|
abstract class InvocableController
|
||||||
|
{
|
||||||
|
public static function resolve(): void
|
||||||
|
{
|
||||||
|
$r = new ReflectionMethod(static::class, '__invoke');
|
||||||
|
|
||||||
|
$args = [];
|
||||||
|
foreach ($r->getParameters() as $param) {
|
||||||
|
$class = $param->getType()->getName();
|
||||||
|
|
||||||
|
if (is_subclass_of($class, BaseFormRequest::class)) {
|
||||||
|
$request = new $class();
|
||||||
|
|
||||||
|
if (!$request->isRequestForThisRoute()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$request->validate();
|
||||||
|
$args[] = $request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$controller = new static();
|
||||||
|
$controller(...$args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Dabestaniha\AuthenticateBridge\App\Http\Controllers;
|
||||||
|
|
||||||
|
class SettingsPageFormController extends InvocableController
|
||||||
|
{
|
||||||
|
public function __invoke()
|
||||||
|
{
|
||||||
|
foreach (config('da.options') as $configured => $options_key) {
|
||||||
|
register_setting(config('da.settings-group'), $options_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
add_settings_section(
|
||||||
|
config('da.section-id'),
|
||||||
|
config('da.translations.section-id'),
|
||||||
|
null,
|
||||||
|
config('da.settings-page')
|
||||||
|
);
|
||||||
|
|
||||||
|
add_settings_field(
|
||||||
|
config('da.options.user-identifier'),
|
||||||
|
config('da.translations.user-identifier'),
|
||||||
|
fn () => $this->user_identifier(),
|
||||||
|
config('da.settings-page'),
|
||||||
|
config('da.section-id')
|
||||||
|
);
|
||||||
|
|
||||||
|
add_settings_field(
|
||||||
|
config('da.options.jwt-secret'),
|
||||||
|
config('da.translations.jwt-secret'),
|
||||||
|
fn () => $this->jwt_secret(),
|
||||||
|
config('da.settings-page'),
|
||||||
|
config('da.section-id')
|
||||||
|
);
|
||||||
|
|
||||||
|
add_settings_field(
|
||||||
|
config('da.options.login-route'),
|
||||||
|
config('da.translations.login-route'),
|
||||||
|
fn () => $this->login_route(),
|
||||||
|
config('da.settings-page'),
|
||||||
|
config('da.section-id')
|
||||||
|
);
|
||||||
|
|
||||||
|
add_settings_field(
|
||||||
|
config('da.options.after-login-route'),
|
||||||
|
config('da.translations.after-login-route'),
|
||||||
|
fn () => $this->after_login_route(),
|
||||||
|
config('da.settings-page'),
|
||||||
|
config('da.section-id')
|
||||||
|
);
|
||||||
|
|
||||||
|
$loginRouteOptionName = config('da.options.login-route');
|
||||||
|
add_filter("pre_update_option_{$loginRouteOptionName}", function ($value) {
|
||||||
|
return trim($value, '/');
|
||||||
|
});
|
||||||
|
|
||||||
|
$afterLoginRouteOptionName = config('da.options.after-login-route');
|
||||||
|
add_filter("pre_update_option_{$afterLoginRouteOptionName}", function ($value) {
|
||||||
|
return trim($value, '/');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user_identifier(): void
|
||||||
|
{
|
||||||
|
$name = config('da.options.user-identifier');
|
||||||
|
$value = get_option($name, 'mobile');
|
||||||
|
|
||||||
|
$html = "<select name='$name'>";
|
||||||
|
|
||||||
|
foreach (config('da.user-identifiers') as $identifier) {
|
||||||
|
$selected = $value === $identifier ? "selected='selected'" : null;
|
||||||
|
$translate = config("da.translations.$identifier");
|
||||||
|
$html .= "<option value='$identifier' $selected>$translate</option>";
|
||||||
|
}
|
||||||
|
|
||||||
|
$html .= "</select>";
|
||||||
|
echo $html;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jwt_secret(): void
|
||||||
|
{
|
||||||
|
$name = config('da.options.jwt-secret');
|
||||||
|
$value = esc_attr(get_option($name, ''));
|
||||||
|
echo "<input type='text' name='$name' value='$value' class='regular-text ltr' />";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function login_route(): void
|
||||||
|
{
|
||||||
|
$name = config('da.options.login-route');
|
||||||
|
$value = esc_attr(get_option($name, config('da.login_route_default')));
|
||||||
|
echo '<div class="ltr" style="float: right; display: flex; align-items: center; gap: 5px;">';
|
||||||
|
echo '<span style="color: #666;">'.home_url('/').'</span>';
|
||||||
|
echo "<input type='text' name='$name' value='$value' class='regular-text' />";
|
||||||
|
echo '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function after_login_route(): void
|
||||||
|
{
|
||||||
|
$name = config('da.options.after-login-route');
|
||||||
|
$value = esc_attr(get_option($name, config('da.after_login_route_default')));
|
||||||
|
echo '<div class="ltr" style="float: right; display: flex; align-items: center; gap: 5px;">';
|
||||||
|
echo '<span style="color: #666;">'.home_url('/').'</span>';
|
||||||
|
echo "<input type='text' name='$name' value='$value' class='regular-text' />";
|
||||||
|
echo '</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Dabestaniha\AuthenticateBridge\App\Http\Controllers;
|
||||||
|
|
||||||
|
class SettingsPageMenuController extends InvocableController
|
||||||
|
{
|
||||||
|
public function __invoke()
|
||||||
|
{
|
||||||
|
add_options_page(
|
||||||
|
config('da.translations.settings-page-title'),
|
||||||
|
config('da.translations.settings-page-menu'),
|
||||||
|
'manage_options',
|
||||||
|
config('da.settings-page'),
|
||||||
|
fn() => $this->dabestaniha_authenticate_settings_page(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function dabestaniha_authenticate_settings_page(): void
|
||||||
|
{
|
||||||
|
?>
|
||||||
|
<div class="wrap">
|
||||||
|
<h1><?= config('da.translations.settings-page-title') ?></h1>
|
||||||
|
<form method="post" action="options.php">
|
||||||
|
<?php
|
||||||
|
settings_fields(config('da.settings-group'));
|
||||||
|
do_settings_sections(config('da.settings-page'));
|
||||||
|
submit_button();
|
||||||
|
?>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,14 @@ abstract class BaseFormRequest
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRoutePath(): string
|
public function isRequestForThisRoute(): bool
|
||||||
|
{
|
||||||
|
return trim($this->getCurrentRoutePath(), '/') === $this->getRequestRoutePath();
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract public function getRequestRoutePath(): string;
|
||||||
|
|
||||||
|
public function getCurrentRoutePath(): string
|
||||||
{
|
{
|
||||||
return $this->url['path'];
|
return $this->url['path'];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,9 @@ namespace Dabestaniha\AuthenticateBridge\App\Http\Requests;
|
|||||||
class DabestanihaAuthenticateRequest extends BaseFormRequest
|
class DabestanihaAuthenticateRequest extends BaseFormRequest
|
||||||
{
|
{
|
||||||
|
|
||||||
public function shouldContinue(): bool
|
public function getRequestRoutePath(): string
|
||||||
{
|
{
|
||||||
$path = $this->getRoutePath();
|
return get_configured_option('login-route');
|
||||||
return trim($path, '/') === get_configured_option('login-route');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rules(): array
|
public function rules(): array
|
||||||
|
|||||||
Reference in New Issue
Block a user