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
This commit is contained in:
vicrodh committed 2026-02-15 14:55:14 -06:00
1 parent 63994c0a20
commit d0fb30a5b1
3 files changed
+50 -15

No files matched your search

+37 -7
View File
@@ -6,7 +6,7 @@
//! Playback flows through CoreBridge -> QbzCore -> Player (qbz-player crate). //! Playback flows through CoreBridge -> QbzCore -> Player (qbz-player crate).
use std::sync::Arc; use std::sync::Arc;
use tauri::{Emitter, State}; use tauri::{Emitter, Manager, State};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use qbz_models::{ 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::<crate::config::legal_settings::LegalSettingsState>();
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 creds = crate::credentials::load_qobuz_credentials();
let last_user_id = crate::user_data::UserDataPaths::load_last_user_id(); 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), 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 { if let Some(bridge) = core_bridge.try_get().await {
match bridge.login(&creds.email, &creds.password).await { match bridge.login(&creds.email, &creds.password).await {
Ok(_) => { Ok(_) => {
@@ -201,14 +226,19 @@ pub async fn runtime_bootstrap(
return Err(RuntimeError::V2NotInitialized); 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 // 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 { if let Err(e) = crate::session_lifecycle::activate_session(&app, session.user_id).await {
log::error!("[Runtime] Session activation failed: {}", e); log::error!("[Runtime] Session activation failed: {}", e);
// Non-fatal for bootstrap - user can still use app but some features may not work // Rollback auth state since session is not usable
// Emit degraded event so frontend knows manager.set_legacy_auth(false, None).await;
manager.set_corebridge_auth(false).await;
let reason = DegradedReason::SessionActivationFailed(e.clone()); 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) => { Err(e) => {
+7 -1
View File
@@ -253,7 +253,13 @@ pub fn run() {
let tray_settings_state = config::tray_settings::TraySettingsState::new_empty(); let tray_settings_state = config::tray_settings::TraySettingsState::new_empty();
let remote_control_settings_state = config::remote_control_settings::RemoteControlSettingsState::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 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() let updates_state = updates::UpdatesState::new_empty()
.expect("Failed to initialize empty updates state"); .expect("Failed to initialize empty updates state");
let subscription_state = config::create_empty_subscription_state(); let subscription_state = config::create_empty_subscription_state();
+6 -7
View File
@@ -38,7 +38,7 @@ pub async fn activate_session(app: &tauri::AppHandle, user_id: u64) -> Result<()
let tray_settings = app.state::<crate::config::tray_settings::TraySettingsState>(); let tray_settings = app.state::<crate::config::tray_settings::TraySettingsState>();
let remote_control_settings = app.state::<crate::config::remote_control_settings::RemoteControlSettingsState>(); let remote_control_settings = app.state::<crate::config::remote_control_settings::RemoteControlSettingsState>();
let allowed_origins = app.state::<crate::config::remote_control_settings::AllowedOriginsState>(); let allowed_origins = app.state::<crate::config::remote_control_settings::AllowedOriginsState>();
let legal_settings = app.state::<crate::config::legal_settings::LegalSettingsState>(); // NOTE: legal_settings is GLOBAL - not per-user, not initialized/torn down here
let updates = app.state::<crate::updates::UpdatesState>(); let updates = app.state::<crate::updates::UpdatesState>();
let library = app.state::<crate::library::commands::LibraryState>(); let library = app.state::<crate::library::commands::LibraryState>();
let reco = app.state::<crate::reco_store::RecoState>(); let reco = app.state::<crate::reco_store::RecoState>();
@@ -93,15 +93,14 @@ pub async fn activate_session(app: &tauri::AppHandle, user_id: u64) -> Result<()
musicbrainz.init_at(&data_dir).await?; musicbrainz.init_at(&data_dir).await?;
listenbrainz.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::{ use crate::config::{
subscription_state::SubscriptionStateStore, subscription_state::SubscriptionStateStore,
download_settings::DownloadSettingsStore, 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(&*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(&*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 // Cache-dir stores
offline_cache.init_at(&cache_dir).await?; 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::<crate::config::tray_settings::TraySettingsState>(); let tray_settings = app.state::<crate::config::tray_settings::TraySettingsState>();
let remote_control_settings = app.state::<crate::config::remote_control_settings::RemoteControlSettingsState>(); let remote_control_settings = app.state::<crate::config::remote_control_settings::RemoteControlSettingsState>();
let allowed_origins = app.state::<crate::config::remote_control_settings::AllowedOriginsState>(); let allowed_origins = app.state::<crate::config::remote_control_settings::AllowedOriginsState>();
let legal_settings = app.state::<crate::config::legal_settings::LegalSettingsState>(); // NOTE: legal_settings is GLOBAL - not per-user, not initialized/torn down here
let updates = app.state::<crate::updates::UpdatesState>(); let updates = app.state::<crate::updates::UpdatesState>();
let library = app.state::<crate::library::commands::LibraryState>(); let library = app.state::<crate::library::commands::LibraryState>();
let reco = app.state::<crate::reco_store::RecoState>(); let reco = app.state::<crate::reco_store::RecoState>();
@@ -216,10 +215,10 @@ pub async fn deactivate_session(app: &tauri::AppHandle) -> Result<(), String> {
musicbrainz.teardown().await; musicbrainz.teardown().await;
listenbrainz.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(&*subscription_state);
crate::commands::user_session::teardown_type_alias_state(&*download_settings); 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 // Clear the active user and persisted last_user_id
user_paths.clear_user(); user_paths.clear_user();