enqueue_styles(); } } public function render_buttons() { $google_enabled = aio_get_option( 'optEnableGoogleLogin' ) == 1 && ! empty( aio_get_option( 'optGoogleClientId' ) ); $facebook_enabled = aio_get_option( 'optEnableFacebookLogin' ) == 1 && ! empty( aio_get_option( 'optFacebookAppId' ) ); if ( ! $google_enabled && ! $facebook_enabled ) { return; } echo '
'; } /** * Initiate OAuth 2.0 flow with cryptographic anti-CSRF state cookie + transient. */ public function handle_oauth_init() { if ( ! isset( $_GET['aio_social_auth'] ) ) { return; } $provider = sanitize_key( wp_unslash( $_GET['aio_social_auth'] ) ); if ( ! in_array( $provider, [ 'google', 'facebook' ], true ) ) { return; } $state = bin2hex( random_bytes( 16 ) ); $redirect_uri = site_url( '/?aio_social_login=' . $provider ); set_transient( 'aio_oauth_state_' . hash( 'sha256', $state ), $provider, 15 * MINUTE_IN_SECONDS ); $this->set_secure_cookie( 'aio_oauth_state', $state, time() + 15 * MINUTE_IN_SECONDS ); if ( $provider === 'google' && aio_get_option( 'optEnableGoogleLogin' ) == 1 ) { $client_id = aio_get_option( 'optGoogleClientId' ); if ( empty( $client_id ) ) { wp_safe_redirect( wp_login_url() ); exit; } $auth_url = 'https://accounts.google.com/o/oauth2/v2/auth?' . http_build_query( [ 'response_type' => 'code', 'client_id' => $client_id, 'redirect_uri' => $redirect_uri, 'scope' => 'openid email profile', 'state' => $state, ] ); nocache_headers(); wp_redirect( $auth_url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } if ( $provider === 'facebook' && aio_get_option( 'optEnableFacebookLogin' ) == 1 ) { $app_id = aio_get_option( 'optFacebookAppId' ); if ( empty( $app_id ) ) { wp_safe_redirect( wp_login_url() ); exit; } $auth_url = 'https://www.facebook.com/v19.0/dialog/oauth?' . http_build_query( [ 'client_id' => $app_id, 'redirect_uri' => $redirect_uri, 'scope' => 'email,public_profile', 'response_type' => 'code', 'state' => $state, ] ); nocache_headers(); wp_redirect( $auth_url ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } } public function handle_oauth_callback() { if ( ! isset( $_GET['aio_social_login'] ) ) { return; } $provider = sanitize_key( wp_unslash( $_GET['aio_social_login'] ) ); if ( ! in_array( $provider, [ 'google', 'facebook' ], true ) ) { return; } if ( isset( $_GET['error'] ) ) { wp_safe_redirect( wp_login_url() ); exit; } if ( ! isset( $_GET['code'] ) || ! isset( $_GET['state'] ) ) { wp_die( esc_html__( 'Missing OAuth state or authorization code.', 'aio-tools' ), esc_html__( 'Authentication Error', 'aio-tools' ), [ 'response' => 403 ] ); } $state = sanitize_text_field( wp_unslash( $_GET['state'] ) ); $cookie_state = isset( $_COOKIE['aio_oauth_state'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['aio_oauth_state'] ) ) : ''; $state_key = 'aio_oauth_state_' . hash( 'sha256', $state ); $saved_prov = get_transient( $state_key ); delete_transient( $state_key ); $this->set_secure_cookie( 'aio_oauth_state', '', time() - 3600 ); if ( empty( $cookie_state ) || ! hash_equals( $cookie_state, $state ) || $saved_prov !== $provider ) { wp_die( esc_html__( 'Invalid or expired OAuth state. Please try logging in again.', 'aio-tools' ), esc_html__( 'Security Error', 'aio-tools' ), [ 'response' => 403 ] ); } $code = sanitize_text_field( wp_unslash( $_GET['code'] ) ); $redirect_uri = site_url( '/?aio_social_login=' . $provider ); $profile = null; if ( $provider === 'google' ) { $client_id = aio_get_option( 'optGoogleClientId' ); $client_secret = AIO_Crypto::decrypt( (string) aio_get_option( 'optGoogleClientSecret', '' ) ); $token_response = wp_safe_remote_post( 'https://oauth2.googleapis.com/token', [ 'timeout' => 15, 'body' => [ 'code' => $code, 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'grant_type' => 'authorization_code', ], ] ); if ( ! is_wp_error( $token_response ) ) { $body = json_decode( wp_remote_retrieve_body( $token_response ), true ); if ( ! empty( $body['access_token'] ) ) { $user_response = wp_safe_remote_get( 'https://www.googleapis.com/oauth2/v2/userinfo', [ 'timeout' => 15, 'headers' => [ 'Authorization' => 'Bearer ' . $body['access_token'] ], ] ); if ( ! is_wp_error( $user_response ) ) { $raw_profile = json_decode( wp_remote_retrieve_body( $user_response ), true ); // Require verified_email === true from Google if ( is_array( $raw_profile ) && ! empty( $raw_profile['verified_email'] ) ) { $profile = $raw_profile; } } } } } elseif ( $provider === 'facebook' ) { $app_id = aio_get_option( 'optFacebookAppId' ); $app_secret = AIO_Crypto::decrypt( (string) aio_get_option( 'optFacebookAppSecret', '' ) ); $token_response = wp_safe_remote_post( 'https://graph.facebook.com/v19.0/oauth/access_token', [ 'timeout' => 15, 'body' => [ 'client_id' => $app_id, 'redirect_uri' => $redirect_uri, 'client_secret' => $app_secret, 'code' => $code, ], ] ); if ( ! is_wp_error( $token_response ) ) { $body = json_decode( wp_remote_retrieve_body( $token_response ), true ); if ( ! empty( $body['access_token'] ) ) { $appsecret_proof = hash_hmac( 'sha256', $body['access_token'], $app_secret ); $user_url = 'https://graph.facebook.com/v19.0/me?' . http_build_query( [ 'fields' => 'id,name,email', 'access_token' => $body['access_token'], 'appsecret_proof' => $appsecret_proof, ] ); $user_response = wp_safe_remote_get( $user_url, [ 'timeout' => 15 ] ); if ( ! is_wp_error( $user_response ) ) { $profile = json_decode( wp_remote_retrieve_body( $user_response ), true ); } } } } if ( is_array( $profile ) && ! empty( $profile['email'] ) && is_email( $profile['email'] ) && ! empty( $profile['id'] ) ) { $this->process_user( sanitize_email( $profile['email'] ), sanitize_text_field( $profile['name'] ?? '' ), $provider, sanitize_text_field( (string) $profile['id'] ) ); } else { wp_die( esc_html__( 'Authentication failed or a verified email address was not provided.', 'aio-tools' ), esc_html__( 'Authentication Error', 'aio-tools' ), [ 'response' => 403 ] ); } } private function process_user( $email, $name, $provider, $provider_id ) { $meta_key = 'aio_social_' . $provider . '_id'; // 1. Check explicitly linked account first $linked_users = get_users( [ 'meta_key' => $meta_key, 'meta_value' => $provider_id, 'number' => 1, ] ); $user = ! empty( $linked_users ) ? $linked_users[0] : get_user_by( 'email', $email ); if ( $user ) { $linked_id = get_user_meta( $user->ID, $meta_key, true ); $legacy_linked = ( get_user_meta( $user->ID, 'aio_social_provider', true ) === $provider && (string) get_user_meta( $user->ID, 'aio_social_id', true ) === $provider_id ); // Prevent unlinked administrator takeover via email match alone if ( user_can( $user, 'manage_options' ) && $linked_id !== $provider_id && ! $legacy_linked ) { wp_die( esc_html__( 'Administrator accounts cannot be logged into via unlinked Social Login. Please sign in using your username and password.', 'aio-tools' ), esc_html__( 'Security Policy', 'aio-tools' ), [ 'response' => 403 ] ); } if ( empty( $linked_id ) ) { update_user_meta( $user->ID, $meta_key, $provider_id ); } // Enforce 2FA if enabled on this user account if ( get_user_meta( $user->ID, '_aio_2fa_enabled', true ) ) { if ( ! function_exists( 'aio_start_2fa_challenge_for_user' ) && file_exists( AIO_MODULES_PATH . 'security/public-security.php' ) ) { require_once AIO_MODULES_PATH . 'security/public-security.php'; } if ( function_exists( 'aio_start_2fa_challenge_for_user' ) ) { aio_start_2fa_challenge_for_user( $user, home_url(), true ); } } wp_clear_auth_cookie(); wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID, true ); do_action( 'wp_login', $user->user_login, $user ); wp_safe_redirect( home_url() ); exit; } // 2. New user registration check $reg_opt = aio_get_option( 'optSocialAllowRegister', null ); $allow_reg = ( $reg_opt !== null ) ? ! empty( $reg_opt ) : (bool) get_option( 'users_can_register' ); if ( ! $allow_reg ) { wp_die( esc_html__( 'New user registration via Social Login is currently disabled.', 'aio-tools' ), esc_html__( 'Registration Disabled', 'aio-tools' ), [ 'response' => 403 ] ); } $session_token = wp_generate_password( 40, false, false ); set_transient( 'aio_social_reg_' . hash( 'sha256', $session_token ), [ 'email' => $email, 'name' => $name, 'provider' => $provider, 'provider_id' => $provider_id, ], 30 * MINUTE_IN_SECONDS ); $this->set_secure_cookie( 'aio_social_reg_token', $session_token, time() + 30 * MINUTE_IN_SECONDS ); wp_safe_redirect( site_url( '/?aio_social_login=set_password' ) ); exit; } public function handle_password_setup() { if ( ! isset( $_GET['aio_social_login'] ) || $_GET['aio_social_login'] !== 'set_password' ) { return; } $token = isset( $_COOKIE['aio_social_reg_token'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['aio_social_reg_token'] ) ) : ''; if ( empty( $token ) ) { wp_die( esc_html__( 'Invalid registration session.', 'aio-tools' ), esc_html__( 'Error', 'aio-tools' ), [ 'response' => 403 ] ); } $transient_key = 'aio_social_reg_' . hash( 'sha256', $token ); $data = get_transient( $transient_key ); if ( ! is_array( $data ) || empty( $data['email'] ) ) { wp_die( esc_html__( 'Registration session expired. Please try logging in again.', 'aio-tools' ), esc_html__( 'Error', 'aio-tools' ), [ 'response' => 403 ] ); } if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['aio_social_password'] ) ) { if ( ! isset( $_POST['aio_social_pw_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['aio_social_pw_nonce'] ) ), 'aio_social_set_password' ) ) { $error = __( 'Security check failed. Please refresh and try again.', 'aio-tools' ); } else { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $password = wp_unslash( $_POST['aio_social_password'] ); if ( strlen( $password ) < 8 ) { $error = __( 'Password must be at least 8 characters.', 'aio-tools' ); } else { $requested_role = sanitize_key( (string) aio_get_option( 'optSocialLoginRole', 'customer' ) ); $allowed_roles = [ 'subscriber' ]; if ( wp_roles()->is_role( 'customer' ) ) { $allowed_roles[] = 'customer'; } $role = in_array( $requested_role, $allowed_roles, true ) ? $requested_role : 'subscriber'; $base_username = sanitize_user( explode( '@', $data['email'] )[0], true ); if ( empty( $base_username ) ) { $base_username = 'user'; } $username = $base_username; $suffix = 1; while ( username_exists( $username ) ) { $username = $base_username . $suffix; $suffix++; } $user_id = wp_create_user( $username, $password, $data['email'] ); if ( is_wp_error( $user_id ) ) { $error = $user_id->get_error_message(); } else { $user = new WP_User( $user_id ); $user->set_role( $role ); if ( ! empty( $data['name'] ) ) { wp_update_user( [ 'ID' => $user_id, 'display_name' => $data['name'], 'first_name' => $data['name'], ] ); } update_user_meta( $user_id, 'aio_social_provider', $data['provider'] ); update_user_meta( $user_id, 'aio_social_id', $data['provider_id'] ); update_user_meta( $user_id, 'aio_social_' . $data['provider'] . '_id', $data['provider_id'] ); delete_transient( $transient_key ); $this->set_secure_cookie( 'aio_social_reg_token', '', time() - 3600 ); wp_clear_auth_cookie(); wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id, true ); do_action( 'wp_login', $user->user_login, $user ); wp_safe_redirect( home_url() ); exit; } } } } nocache_headers(); echo '' . sprintf( esc_html__( 'Welcome %s, please set a password to complete your registration.', 'aio-tools' ), esc_html( $data['name'] ?: $data['email'] ) ) . '
'; if ( isset( $error ) ) { echo '