fix: store ToS after successful login instead of gating login on ToS
- Remove require_tos_accepted gate from all login commands (v2_auto_login, v2_manual_login, v2_start_oauth_login, v2_login) - Call accept_tos_best_effort after each successful session activation so the value is persisted the moment login succeeds - Re-initialize LegalSettingsStore in accept_tos_best_effort if torn down (e.g. after factory reset that deletes legal_settings.db) - Bootstrap ToS check now fails open (unwrap_or(true)) so saved credentials still trigger auto-login after a factory reset The frontend checkbox already gates the UI; the backend gate was causing double-failures when the DB was unavailable post-factory-reset.
This commit is contained in:
1 file changed
+39
-72
@@ -161,31 +161,25 @@ fn convert_to_qbz_audio_settings(settings: &AudioSettings) -> qbz_audio::AudioSe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check that Qobuz ToS has been accepted before allowing login.
|
/// Persist ToS acceptance and remove the backend gate for login commands.
|
||||||
///
|
///
|
||||||
/// This is the single enforcement point for ToS gate in backend.
|
/// Calling any login command IS the user's ToS acceptance (they had to check
|
||||||
/// All login commands MUST call this before authenticating.
|
/// the checkbox on the frontend to enable the button). We persist the value
|
||||||
///
|
/// best-effort, re-initializing the store if it was torn down (e.g. after a
|
||||||
/// Returns Ok(()) if ToS accepted, Err with specific error code if not.
|
/// factory reset), so subsequent bootstrap auto-logins work correctly.
|
||||||
fn require_tos_accepted(legal_state: &LegalSettingsState) -> Result<(), (String, String)> {
|
fn accept_tos_best_effort(legal_state: &LegalSettingsState) {
|
||||||
let guard = legal_state
|
use crate::config::legal_settings::LegalSettingsStore;
|
||||||
.lock()
|
if let Ok(mut guard) = legal_state.lock() {
|
||||||
.map_err(|e| ("tos_check_failed".to_string(), format!("Lock error: {}", e)))?;
|
// Re-initialize the store if it was torn down (e.g. after factory reset).
|
||||||
|
if guard.is_none() {
|
||||||
let tos_accepted = guard
|
if let Ok(new_store) = LegalSettingsStore::new() {
|
||||||
.as_ref()
|
*guard = Some(new_store);
|
||||||
.and_then(|store| store.get_settings().ok())
|
}
|
||||||
.map(|s| s.qobuz_tos_accepted)
|
}
|
||||||
.unwrap_or(false);
|
if let Some(store) = guard.as_ref() {
|
||||||
|
let _ = store.set_qobuz_tos_accepted(true);
|
||||||
if !tos_accepted {
|
}
|
||||||
return Err((
|
|
||||||
"tos_not_accepted".to_string(),
|
|
||||||
"Qobuz Terms of Service must be accepted before login".to_string(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rollback runtime auth state after a partial login failure.
|
/// Rollback runtime auth state after a partial login failure.
|
||||||
@@ -440,8 +434,10 @@ pub async fn runtime_bootstrap(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Check ToS acceptance BEFORE attempting auto-login
|
// Step 2: Check ToS acceptance. Fail open if the DB is unavailable
|
||||||
// ToS gate is enforced: if not accepted, skip auto-login entirely
|
// (e.g. after a factory reset that deleted legal_settings.db but left
|
||||||
|
// credentials intact). ToS is now stored AFTER a successful login, so
|
||||||
|
// the very first bootstrap after factory reset will not have it yet.
|
||||||
let tos_accepted: bool = {
|
let tos_accepted: bool = {
|
||||||
let legal_state = app.state::<crate::config::legal_settings::LegalSettingsState>();
|
let legal_state = app.state::<crate::config::legal_settings::LegalSettingsState>();
|
||||||
let guard = legal_state.lock();
|
let guard = legal_state.lock();
|
||||||
@@ -451,12 +447,12 @@ pub async fn runtime_bootstrap(
|
|||||||
store
|
store
|
||||||
.get_settings()
|
.get_settings()
|
||||||
.map(|s| s.qobuz_tos_accepted)
|
.map(|s| s.qobuz_tos_accepted)
|
||||||
.unwrap_or(false)
|
.unwrap_or(true) // fail open when DB read fails
|
||||||
} else {
|
} else {
|
||||||
false
|
true // fail open when store is torn down (e.g. factory reset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => false,
|
Err(_) => true, // fail open on lock error
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -794,19 +790,6 @@ pub async fn v2_auto_login(
|
|||||||
|
|
||||||
log::info!("[V2] v2_auto_login starting...");
|
log::info!("[V2] v2_auto_login starting...");
|
||||||
|
|
||||||
// ToS gate - must be accepted before any login attempt
|
|
||||||
if let Err((error_code, error)) = require_tos_accepted(&legal_state) {
|
|
||||||
return Ok(V2LoginResponse {
|
|
||||||
success: false,
|
|
||||||
user_name: None,
|
|
||||||
user_id: None,
|
|
||||||
subscription: None,
|
|
||||||
subscription_valid_until: None,
|
|
||||||
error: Some(error),
|
|
||||||
error_code: Some(error_code),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load saved credentials
|
// Load saved credentials
|
||||||
let creds = match crate::credentials::load_qobuz_credentials() {
|
let creds = match crate::credentials::load_qobuz_credentials() {
|
||||||
Ok(Some(c)) => c,
|
Ok(Some(c)) => c,
|
||||||
@@ -925,6 +908,12 @@ pub async fn v2_auto_login(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Persist ToS acceptance now that login succeeded.
|
||||||
|
// The frontend checkbox already gated the UI; we store the value so
|
||||||
|
// subsequent bootstrap auto-logins pass without requiring the user to
|
||||||
|
// re-accept (e.g. after a factory reset that wiped the DB).
|
||||||
|
accept_tos_best_effort(&legal_state);
|
||||||
|
|
||||||
// Emit ready event
|
// Emit ready event
|
||||||
let _ = app.emit(
|
let _ = app.emit(
|
||||||
"runtime:event",
|
"runtime:event",
|
||||||
@@ -967,19 +956,6 @@ pub async fn v2_manual_login(
|
|||||||
|
|
||||||
log::info!("[V2] v2_manual_login starting...");
|
log::info!("[V2] v2_manual_login starting...");
|
||||||
|
|
||||||
// ToS gate - must be accepted before any login attempt
|
|
||||||
if let Err((error_code, error)) = require_tos_accepted(&legal_state) {
|
|
||||||
return Ok(V2LoginResponse {
|
|
||||||
success: false,
|
|
||||||
user_name: None,
|
|
||||||
user_id: None,
|
|
||||||
subscription: None,
|
|
||||||
subscription_valid_until: None,
|
|
||||||
error: Some(error),
|
|
||||||
error_code: Some(error_code),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Legacy auth
|
// Legacy auth
|
||||||
let client = app_state.client.read().await;
|
let client = app_state.client.read().await;
|
||||||
let session = match client.login(&email, &password).await {
|
let session = match client.login(&email, &password).await {
|
||||||
@@ -1071,6 +1047,9 @@ pub async fn v2_manual_login(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Persist ToS acceptance now that login succeeded.
|
||||||
|
accept_tos_best_effort(&legal_state);
|
||||||
|
|
||||||
// Emit ready event
|
// Emit ready event
|
||||||
let _ = app.emit(
|
let _ = app.emit(
|
||||||
"runtime:event",
|
"runtime:event",
|
||||||
@@ -1111,19 +1090,6 @@ pub async fn v2_start_oauth_login(
|
|||||||
let manager = runtime.manager();
|
let manager = runtime.manager();
|
||||||
log::info!("[V2] v2_start_oauth_login starting...");
|
log::info!("[V2] v2_start_oauth_login starting...");
|
||||||
|
|
||||||
// ToS gate — same as v2_manual_login
|
|
||||||
if let Err((error_code, error)) = require_tos_accepted(&legal_state) {
|
|
||||||
return Ok(V2LoginResponse {
|
|
||||||
success: false,
|
|
||||||
user_name: None,
|
|
||||||
user_id: None,
|
|
||||||
subscription: None,
|
|
||||||
subscription_valid_until: None,
|
|
||||||
error: Some(error),
|
|
||||||
error_code: Some(error_code),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get app_id from initialized client
|
// Get app_id from initialized client
|
||||||
let app_id = {
|
let app_id = {
|
||||||
let client = app_state.client.read().await;
|
let client = app_state.client.read().await;
|
||||||
@@ -1407,6 +1373,9 @@ pub async fn v2_start_oauth_login(
|
|||||||
log::warn!("[V2] Failed to persist OAuth token: {}", e);
|
log::warn!("[V2] Failed to persist OAuth token: {}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Persist ToS acceptance now that login succeeded.
|
||||||
|
accept_tos_best_effort(&legal_state);
|
||||||
|
|
||||||
let _ = app.emit(
|
let _ = app.emit(
|
||||||
"runtime:event",
|
"runtime:event",
|
||||||
RuntimeEvent::RuntimeReady {
|
RuntimeEvent::RuntimeReady {
|
||||||
@@ -1584,11 +1553,6 @@ pub async fn v2_login(
|
|||||||
) -> Result<UserSession, RuntimeError> {
|
) -> Result<UserSession, RuntimeError> {
|
||||||
let manager = runtime.manager();
|
let manager = runtime.manager();
|
||||||
|
|
||||||
// Step 0: ToS gate - must be accepted before any login attempt
|
|
||||||
if let Err((_, error)) = require_tos_accepted(&legal_state) {
|
|
||||||
return Err(RuntimeError::Internal(error));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 1: Legacy auth
|
// Step 1: Legacy auth
|
||||||
let session = {
|
let session = {
|
||||||
let client = app_state.client.read().await;
|
let client = app_state.client.read().await;
|
||||||
@@ -1621,6 +1585,9 @@ pub async fn v2_login(
|
|||||||
}
|
}
|
||||||
log::info!("[v2_login] Session activated for user {}", session.user_id);
|
log::info!("[v2_login] Session activated for user {}", session.user_id);
|
||||||
|
|
||||||
|
// Persist ToS acceptance now that login succeeded.
|
||||||
|
accept_tos_best_effort(&legal_state);
|
||||||
|
|
||||||
// Convert api::models::UserSession to qbz_models::UserSession
|
// Convert api::models::UserSession to qbz_models::UserSession
|
||||||
Ok(UserSession {
|
Ok(UserSession {
|
||||||
user_auth_token: session.user_auth_token,
|
user_auth_token: session.user_auth_token,
|
||||||
|
|||||||
Reference in new issue
Block a user