improve handel requests
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user