| | |
| | | register_rest_route($this->namespace, '/auth/login', [ |
| | | 'methods' => 'POST', |
| | | 'callback' => [$this, 'handleLogin'], |
| | | 'permission_callback' => [$this, 'checkRateLimit'], |
| | | 'args' => [ |
| | | 'user_email' => [ |
| | | 'required' => true, |
| | | 'type' => 'string', |
| | | 'sanitize_callback' => 'sanitize_email' |
| | | ], |
| | | 'user_password' => [ |
| | | 'required' => true, |
| | | 'type' => 'string' |
| | | ], |
| | | 'remember_me' => [ |
| | | 'required' => false, |
| | | 'type' => 'boolean', |
| | | 'default' => false |
| | | ] |
| | | ] |
| | | 'permission_callback' => [$this, 'checkRateLimit'] |
| | | ]); |
| | | |
| | | // Logout endpoint |
| | |
| | | |
| | | public function handleLogin(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $data = $request->get_json_params(); |
| | | $data = $request->get_params(); |
| | | error_log('Data: '.print_r($data, true)); |
| | | // Verify Turnstile |
| | | if (!$this->verifyTurnstile($data['cf-turnstile-response'] ?? '')) { |
| | | return $this->error('Security verification failed', 'turnstile_failed', 403); |
| | |
| | | // Attempt login |
| | | $user = wp_signon([ |
| | | 'user_login' => $username, |
| | | 'user_email' => $username, |
| | | 'user_password' => $password, |
| | | 'remember' => $remember |
| | | ], false); |
| | | ], is_ssl()); |
| | | |
| | | |
| | | if (is_wp_error($user)) { |
| | | // Track failed attempt |
| | |
| | | 401 |
| | | ) : false; |
| | | } |
| | | |
| | | // Clear failed attempts on success |
| | | $this->clearFailedAttempts($username); |
| | | |
| | | // Set auth cookie with remember me flag |
| | | wp_set_current_user($user->ID); |
| | | wp_set_auth_cookie($user->ID, $remember); |
| | | wp_set_auth_cookie($user->ID, $remember, is_ssl()); |
| | | |
| | | |
| | | |
| | | // Store session fingerprint for hijacking protection |
| | | if ($request) { |
| | |
| | | */ |
| | | protected function getSessionId(int $user_id): string |
| | | { |
| | | // Use WordPress session tokens |
| | | $sessions = WP_Session_Tokens::get_instance($user_id); |
| | | $token = wp_get_session_token(); // Current session token |
| | | |
| | | if (!$token) { |
| | | // Fallback to user-specific hash that changes on password reset |
| | | return md5($user_id . get_user_meta($user_id, 'session_tokens', true)); |
| | | // Fallback to a hash based on user ID and current timestamp |
| | | // This will be replaced once the session token is available |
| | | return md5($user_id . time()); |
| | | } |
| | | |
| | | return md5($token); |
| | |
| | | wp_set_current_user($user->ID); |
| | | wp_set_auth_cookie($user->ID, true); |
| | | |
| | | if (session_status() === PHP_SESSION_ACTIVE) { |
| | | session_regenerate_id(true); |
| | | } |
| | | // Store session fingerprint |
| | | $this->storeSessionFingerprint($user->ID, $request); |
| | | |
| | |
| | | update_user_meta($user_id, BASE . $key, sanitize_text_field($value)); |
| | | } |
| | | |
| | | $redirect = $this->getRedirect($user, $request->get_param('redirect_to'), 'register'); |
| | | $redirect = $this->getRedirect($user, $request->get_param('redirect_to')??get_home_url(null,'/dash'), 'register'); |
| | | |
| | | // Handle token handlers |
| | | do_action('jvbUserRegistered', $user_id, $email, $data); |
| | |
| | | ]; |
| | | } |
| | | |
| | | protected function getRedirect(WP_User $user, string $url, string $context = 'login'):string |
| | | protected function getRedirect(WP_User $user, ?string $url=null, string $context = 'login'):string |
| | | { |
| | | if (!empty($url)) { |
| | | $url = sanitize_url($url); |