diff --git a/dabestaniha-authenticate-bridge.php b/dabestaniha-authenticate-bridge.php index 6da28b0..f36d263 100644 --- a/dabestaniha-authenticate-bridge.php +++ b/dabestaniha-authenticate-bridge.php @@ -1,4 +1,5 @@ AuthenticateController::resolve()); - if (!$request->shouldContinue()) { - return; - } - - $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; -} \ No newline at end of file +add_action('admin_menu', fn() => SettingsPageMenuController::resolve()); +add_action('admin_init', fn() => SettingsPageFormController::resolve()); diff --git a/settings-page.php b/settings-page.php deleted file mode 100644 index 92a28a3..0000000 --- a/settings-page.php +++ /dev/null @@ -1,117 +0,0 @@ - $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 = ""; - 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 ""; - }, - 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 '
'; - echo ''.home_url('/').''; - echo ""; - echo '
'; - }, - 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 '
'; - echo ''.home_url('/').''; - echo ""; - echo '
'; - }, - 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 -{ - ?> -
-

-
- -
-
- 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; + } +} \ No newline at end of file diff --git a/src/app/Http/Controllers/InvocableController.php b/src/app/Http/Controllers/InvocableController.php new file mode 100644 index 0000000..20ab4af --- /dev/null +++ b/src/app/Http/Controllers/InvocableController.php @@ -0,0 +1,33 @@ +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); + } +} \ No newline at end of file diff --git a/src/app/Http/Controllers/SettingsPageFormController.php b/src/app/Http/Controllers/SettingsPageFormController.php new file mode 100644 index 0000000..ad8acc7 --- /dev/null +++ b/src/app/Http/Controllers/SettingsPageFormController.php @@ -0,0 +1,106 @@ + $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 = ""; + echo $html; + } + + public function jwt_secret(): void + { + $name = config('da.options.jwt-secret'); + $value = esc_attr(get_option($name, '')); + echo ""; + } + + public function login_route(): void + { + $name = config('da.options.login-route'); + $value = esc_attr(get_option($name, config('da.login_route_default'))); + echo '
'; + echo ''.home_url('/').''; + echo ""; + echo '
'; + } + + 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 '
'; + echo ''.home_url('/').''; + echo ""; + echo '
'; + } +} \ No newline at end of file diff --git a/src/app/Http/Controllers/SettingsPageMenuController.php b/src/app/Http/Controllers/SettingsPageMenuController.php new file mode 100644 index 0000000..589a1d6 --- /dev/null +++ b/src/app/Http/Controllers/SettingsPageMenuController.php @@ -0,0 +1,33 @@ + $this->dabestaniha_authenticate_settings_page(), + ); + } + + function dabestaniha_authenticate_settings_page(): void + { + ?> +
+

+
+ +
+
+ getCurrentRoutePath(), '/') === $this->getRequestRoutePath(); + } + + abstract public function getRequestRoutePath(): string; + + public function getCurrentRoutePath(): string { return $this->url['path']; } diff --git a/src/app/Http/Requests/DabestanihaAuthenticateRequest.php b/src/app/Http/Requests/DabestanihaAuthenticateRequest.php index 5b58b8a..c008773 100644 --- a/src/app/Http/Requests/DabestanihaAuthenticateRequest.php +++ b/src/app/Http/Requests/DabestanihaAuthenticateRequest.php @@ -5,10 +5,9 @@ namespace Dabestaniha\AuthenticateBridge\App\Http\Requests; class DabestanihaAuthenticateRequest extends BaseFormRequest { - public function shouldContinue(): bool + public function getRequestRoutePath(): string { - $path = $this->getRoutePath(); - return trim($path, '/') === get_configured_option('login-route'); + return get_configured_option('login-route'); } public function rules(): array