user_login, $secret ); // Store pending secret server-side (encrypted) for 15 minutes — never trust secret from POST on verify set_transient( 'aio_2fa_pending_' . $user->ID, AIO_Crypto::encrypt( $secret ), 15 * MINUTE_IN_SECONDS ); wp_send_json_success( [ 'secret' => $secret, 'otpauth_uri' => $otpauth_uri, ] ); } public function aio_verify_2fa_ajax() { check_ajax_referer( 'aio-settings-nonce', 'security' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Unauthorized', 'aio-tools' ) ); } $user_id = get_current_user_id(); $code = isset( $_POST['code'] ) ? sanitize_text_field( wp_unslash( $_POST['code'] ) ) : ''; $encrypted_tmp = get_transient( 'aio_2fa_pending_' . $user_id ); $secret = ! empty( $encrypted_tmp ) ? AIO_Crypto::decrypt( $encrypted_tmp ) : ''; if ( empty( $code ) || empty( $secret ) ) { wp_send_json_error( __( '2FA setup session expired or missing code. Please click Setup 2FA again.', 'aio-tools' ) ); } $matched_slice = null; $is_valid = AIO_TOTP::verify_code( $secret, $code, 1, $matched_slice ); if ( $is_valid ) { $backup = AIO_TOTP::generate_backup_codes( 8 ); update_user_meta( $user_id, '_aio_2fa_secret', AIO_Crypto::encrypt( $secret ) ); update_user_meta( $user_id, '_aio_2fa_enabled', 1 ); update_user_meta( $user_id, '_aio_2fa_last_slice', (int) $matched_slice ); update_user_meta( $user_id, '_aio_2fa_backup_codes', $backup['hashes'] ); delete_transient( 'aio_2fa_pending_' . $user_id ); wp_send_json_success( [ 'message' => __( '2FA successfully activated! Please save your recovery codes below.', 'aio-tools' ), 'backup_codes' => $backup['plain'], ] ); } else { wp_send_json_error( __( 'Invalid 6-digit code! Please check your authenticator app and try again.', 'aio-tools' ) ); } } public function aio_regenerate_2fa_backup_ajax() { check_ajax_referer( 'aio-settings-nonce', 'security' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Unauthorized', 'aio-tools' ) ); } $user_id = get_current_user_id(); if ( ! get_user_meta( $user_id, '_aio_2fa_enabled', true ) ) { wp_send_json_error( __( '2FA is not enabled on your account.', 'aio-tools' ) ); } $backup = AIO_TOTP::generate_backup_codes( 8 ); update_user_meta( $user_id, '_aio_2fa_backup_codes', $backup['hashes'] ); wp_send_json_success( [ 'message' => __( 'New recovery codes generated! Previous recovery codes have been invalidated.', 'aio-tools' ), 'backup_codes' => $backup['plain'], ] ); } public function aio_disable_2fa_ajax() { check_ajax_referer( 'aio-settings-nonce', 'security' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Unauthorized', 'aio-tools' ) ); } $user_id = get_current_user_id(); delete_user_meta( $user_id, '_aio_2fa_enabled' ); delete_user_meta( $user_id, '_aio_2fa_secret' ); delete_user_meta( $user_id, '_aio_2fa_backup_codes' ); delete_user_meta( $user_id, '_aio_2fa_last_slice' ); delete_transient( 'aio_2fa_pending_' . $user_id ); wp_send_json_success( __( 'Successfully disabled!', 'aio-tools' ) ); } public function aio_unblock_ip_ajax() { check_ajax_referer( 'aio-settings-nonce', 'security' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Unauthorized', 'aio-tools' ) ); } $ip = isset( $_POST['ip'] ) ? sanitize_text_field( wp_unslash( $_POST['ip'] ) ) : ''; if ( empty( $ip ) ) { wp_send_json_error( __( 'Invalid IP', 'aio-tools' ) ); } global $wpdb; $lockouts_table = $wpdb->prefix . 'aio_lockouts'; $wpdb->delete( $lockouts_table, [ 'ip_address' => $ip ], [ '%s' ] ); delete_transient( 'aio_lockout_' . md5( $ip ) ); delete_transient( 'aio_login_attempts_' . md5( $ip ) ); wp_send_json_success( __( 'IP has been unblocked successfully!', 'aio-tools' ) ); } }