From d0fb30a5b1966f539ddde96a78fc42ae6a344889 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sun, 15 Feb 2026 14:55:14 -0600 Subject: [PATCH] fix(auth): enforce ToS gate and session activation severity parity Fixes login consistency issues: 1. ToS gate enforcement: - LegalSettingsState now initialized GLOBALLY at app startup (not per-session) - runtime_bootstrap checks ToS BEFORE attempting auto-login - If ToS not accepted, skip auto-login and return to login screen 2. Session activation severity parity: - runtime_bootstrap now treats activate_session failure as FATAL - On failure: rollback auth state, set degraded, return error - Matches behavior of v2_auto_login/v2_manual_login 3. LegalSettings is now truly global: - Removed from session_lifecycle activate/deactivate - ToS acceptance persists across sessions - Can be checked before user is logged in Acceptance criteria: - ToS false + credentials -> no auto-login - ToS true + credentials + activation OK -> session restored - ToS true + credentials + activation fails -> error, no session --- src-tauri/src/commands_v2.rs | 44 +++++++++++++++++++++++++----- src-tauri/src/lib.rs | 8 +++++- src-tauri/src/session_lifecycle.rs | 13 ++++----- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index 5d200ae23..a85be918c 100644 --- a/src-tauri/src/commands_v2.rs +++ b/src-tauri/src/commands_v2.rs @@ -6,7 +6,7 @@ //! Playback flows through CoreBridge -> QbzCore -> Player (qbz-player crate). use std::sync::Arc; -use tauri::{Emitter, State}; +use tauri::{Emitter, Manager, State}; use tokio::sync::RwLock; use qbz_models::{ @@ -161,7 +161,32 @@ pub async fn runtime_bootstrap( } } - // Step 2: Check for saved credentials and attempt auto-login + // Step 2: Check ToS acceptance BEFORE attempting auto-login + // ToS gate is enforced: if not accepted, skip auto-login entirely + let tos_accepted: bool = { + let legal_state = app.state::(); + let guard = legal_state.lock(); + match guard { + Ok(ref g) => { + if let Some(store) = g.as_ref() { + store.get_settings().map(|s| s.qobuz_tos_accepted).unwrap_or(false) + } else { + false + } + } + Err(_) => false, + } + }; + + if !tos_accepted { + log::info!("[Runtime] ToS not accepted, skipping auto-login. User must accept ToS first."); + manager.set_bootstrap_in_progress(false).await; + let status = manager.get_status().await; + log::info!("[Runtime] Bootstrap complete (ToS gate): {:?}", status.state); + return Ok(status); + } + + // Step 3: Check for saved credentials and attempt auto-login let creds = crate::credentials::load_qobuz_credentials(); let last_user_id = crate::user_data::UserDataPaths::load_last_user_id(); @@ -179,7 +204,7 @@ pub async fn runtime_bootstrap( user_id: Some(session.user_id), }); - // Step 3: Authenticate CoreBridge/V2 - REQUIRED per ADR + // Step 4: Authenticate CoreBridge/V2 - REQUIRED per ADR if let Some(bridge) = core_bridge.try_get().await { match bridge.login(&creds.email, &creds.password).await { Ok(_) => { @@ -201,14 +226,19 @@ pub async fn runtime_bootstrap( return Err(RuntimeError::V2NotInitialized); } - // Step 4: Activate per-user session - NOW autocontained in bootstrap + // Step 5: Activate per-user session - REQUIRED (FATAL if fails) // This initializes all per-user stores and sets runtime state + // Session activation failure is FATAL per parity with v2_auto_login/v2_manual_login if let Err(e) = crate::session_lifecycle::activate_session(&app, session.user_id).await { log::error!("[Runtime] Session activation failed: {}", e); - // Non-fatal for bootstrap - user can still use app but some features may not work - // Emit degraded event so frontend knows + // Rollback auth state since session is not usable + manager.set_legacy_auth(false, None).await; + manager.set_corebridge_auth(false).await; let reason = DegradedReason::SessionActivationFailed(e.clone()); - let _ = app.emit("runtime:event", RuntimeEvent::RuntimeDegraded { reason }); + manager.set_degraded(reason.clone()).await; + manager.set_bootstrap_in_progress(false).await; + let _ = app.emit("runtime:event", RuntimeEvent::RuntimeDegraded { reason: reason.clone() }); + return Err(RuntimeError::RuntimeDegraded(reason)); } } Err(e) => { diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 5aa7317a8..37b0a1984 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -253,7 +253,13 @@ pub fn run() { let tray_settings_state = config::tray_settings::TraySettingsState::new_empty(); let remote_control_settings_state = config::remote_control_settings::RemoteControlSettingsState::new_empty(); let allowed_origins_state = config::remote_control_settings::AllowedOriginsState::new_empty(); - let legal_settings_state = config::legal_settings::create_empty_legal_settings_state(); + // LegalSettings is GLOBAL (not per-user) - must be initialized at startup + // so ToS acceptance can be checked BEFORE attempting auto-login + let legal_settings_state = config::legal_settings::create_legal_settings_state() + .unwrap_or_else(|e| { + log::warn!("Failed to initialize legal settings: {}. Using empty state.", e); + config::legal_settings::create_empty_legal_settings_state() + }); let updates_state = updates::UpdatesState::new_empty() .expect("Failed to initialize empty updates state"); let subscription_state = config::create_empty_subscription_state(); diff --git a/src-tauri/src/session_lifecycle.rs b/src-tauri/src/session_lifecycle.rs index e0de70ce0..4589bc73d 100644 --- a/src-tauri/src/session_lifecycle.rs +++ b/src-tauri/src/session_lifecycle.rs @@ -38,7 +38,7 @@ pub async fn activate_session(app: &tauri::AppHandle, user_id: u64) -> Result<() let tray_settings = app.state::(); let remote_control_settings = app.state::(); let allowed_origins = app.state::(); - let legal_settings = app.state::(); + // NOTE: legal_settings is GLOBAL - not per-user, not initialized/torn down here let updates = app.state::(); let library = app.state::(); let reco = app.state::(); @@ -93,15 +93,14 @@ pub async fn activate_session(app: &tauri::AppHandle, user_id: u64) -> Result<() musicbrainz.init_at(&data_dir).await?; listenbrainz.init_at(&data_dir).await?; - // Type-alias states + // Type-alias states (per-user settings) + // NOTE: LegalSettingsState is GLOBAL (not per-user) - initialized at app startup use crate::config::{ subscription_state::SubscriptionStateStore, download_settings::DownloadSettingsStore, - legal_settings::LegalSettingsStore, }; crate::commands::user_session::init_type_alias_state(&*subscription_state, &data_dir, SubscriptionStateStore::new_at)?; crate::commands::user_session::init_type_alias_state(&*download_settings, &data_dir, DownloadSettingsStore::new_at)?; - crate::commands::user_session::init_type_alias_state(&*legal_settings, &data_dir, LegalSettingsStore::new_at)?; // Cache-dir stores offline_cache.init_at(&cache_dir).await?; @@ -181,7 +180,7 @@ pub async fn deactivate_session(app: &tauri::AppHandle) -> Result<(), String> { let tray_settings = app.state::(); let remote_control_settings = app.state::(); let allowed_origins = app.state::(); - let legal_settings = app.state::(); + // NOTE: legal_settings is GLOBAL - not per-user, not initialized/torn down here let updates = app.state::(); let library = app.state::(); let reco = app.state::(); @@ -216,10 +215,10 @@ pub async fn deactivate_session(app: &tauri::AppHandle) -> Result<(), String> { musicbrainz.teardown().await; listenbrainz.teardown().await; - // Type-alias states + // Type-alias states (per-user settings) + // NOTE: LegalSettingsState is GLOBAL (not per-user) - NOT torn down here crate::commands::user_session::teardown_type_alias_state(&*subscription_state); crate::commands::user_session::teardown_type_alias_state(&*download_settings); - crate::commands::user_session::teardown_type_alias_state(&*legal_settings); // Clear the active user and persisted last_user_id user_paths.clear_user();