feat(auth): add secure credential persistence using system keyring

- Add keyring crate for secure credential storage (KWallet/GNOME Keyring)
- Create credentials module with save/load/clear functions
- Add auto_login command that uses saved credentials
- Add "Remember me" checkbox to login form (default: checked)
- Auto-login on app startup if credentials are saved
- Clear credentials from keyring on logout
- Show dynamic status during initialization (connecting, checking, logging in)
This commit is contained in:
vicrodh committed 2026-01-09 14:37:49 -06:00
1 parent 541f46e430
commit 6eed045404
6 files changed
+280 -9

No files matched your search

+69
View File
@@ -2,6 +2,7 @@
use tauri::State;
use crate::credentials;
use crate::AppState;
#[derive(serde::Serialize)]
@@ -72,3 +73,71 @@ pub async fn get_user_info(state: State<'_, AppState>) -> Result<Option<UserInfo
subscription: sub,
}))
}
// === Credential persistence commands ===
/// Check if saved credentials exist in system keyring
#[tauri::command]
pub fn has_saved_credentials() -> bool {
credentials::has_saved_credentials()
}
/// Save credentials to system keyring
#[tauri::command]
pub fn save_credentials(email: String, password: String) -> Result<(), String> {
credentials::save_qobuz_credentials(&email, &password)
}
/// Clear saved credentials from system keyring
#[tauri::command]
pub fn clear_saved_credentials() -> Result<(), String> {
credentials::clear_qobuz_credentials()
}
/// Auto-login using saved credentials
/// Returns LoginResponse with success status
#[tauri::command]
pub async fn auto_login(state: State<'_, AppState>) -> Result<LoginResponse, String> {
// Check for saved credentials
let creds = match credentials::load_qobuz_credentials() {
Ok(Some(c)) => c,
Ok(None) => {
return Ok(LoginResponse {
success: false,
user_name: None,
subscription: None,
error: Some("No saved credentials".to_string()),
});
}
Err(e) => {
return Ok(LoginResponse {
success: false,
user_name: None,
subscription: None,
error: Some(e),
});
}
};
// Try to login with saved credentials
let client = state.client.lock().await;
match client.login(&creds.email, &creds.password).await {
Ok(session) => Ok(LoginResponse {
success: true,
user_name: Some(session.display_name),
subscription: Some(session.subscription_label),
error: None,
}),
Err(e) => {
// Credentials might be invalid, but don't clear them automatically
// Let the user decide
log::warn!("Auto-login failed: {}", e);
Ok(LoginResponse {
success: false,
user_name: None,
subscription: None,
error: Some(e.to_string()),
})
}
}
}
+122
View File
@@ -0,0 +1,122 @@
//! Secure credential storage using system keyring
//!
//! Uses the system's secure credential storage:
//! - Linux: Secret Service (GNOME Keyring, KWallet via D-Bus)
//! - macOS: Keychain
//! - Windows: Credential Manager
use keyring::Entry;
use serde::{Deserialize, Serialize};
const SERVICE_NAME: &str = "qbz-nix";
const QOBUZ_CREDENTIALS_KEY: &str = "qobuz-credentials";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QobuzCredentials {
pub email: String,
pub password: String,
}
/// Save Qobuz credentials to system keyring
pub fn save_qobuz_credentials(email: &str, password: &str) -> Result<(), String> {
let entry = Entry::new(SERVICE_NAME, QOBUZ_CREDENTIALS_KEY)
.map_err(|e| format!("Failed to create keyring entry: {}", e))?;
let credentials = QobuzCredentials {
email: email.to_string(),
password: password.to_string(),
};
let json = serde_json::to_string(&credentials)
.map_err(|e| format!("Failed to serialize credentials: {}", e))?;
entry
.set_password(&json)
.map_err(|e| format!("Failed to save to keyring: {}", e))?;
log::info!("Qobuz credentials saved to system keyring");
Ok(())
}
/// Load Qobuz credentials from system keyring
pub fn load_qobuz_credentials() -> Result<Option<QobuzCredentials>, String> {
let entry = Entry::new(SERVICE_NAME, QOBUZ_CREDENTIALS_KEY)
.map_err(|e| format!("Failed to create keyring entry: {}", e))?;
match entry.get_password() {
Ok(json) => {
let credentials: QobuzCredentials = serde_json::from_str(&json)
.map_err(|e| format!("Failed to parse credentials: {}", e))?;
log::info!("Loaded Qobuz credentials from keyring for: {}", credentials.email);
Ok(Some(credentials))
}
Err(keyring::Error::NoEntry) => {
log::debug!("No saved Qobuz credentials found");
Ok(None)
}
Err(e) => Err(format!("Failed to load from keyring: {}", e)),
}
}
/// Check if credentials are saved
pub fn has_saved_credentials() -> bool {
let entry = match Entry::new(SERVICE_NAME, QOBUZ_CREDENTIALS_KEY) {
Ok(e) => e,
Err(_) => return false,
};
entry.get_password().is_ok()
}
/// Clear saved Qobuz credentials
pub fn clear_qobuz_credentials() -> Result<(), String> {
let entry = Entry::new(SERVICE_NAME, QOBUZ_CREDENTIALS_KEY)
.map_err(|e| format!("Failed to create keyring entry: {}", e))?;
match entry.delete_credential() {
Ok(()) => {
log::info!("Qobuz credentials cleared from keyring");
Ok(())
}
Err(keyring::Error::NoEntry) => {
// Already cleared, that's fine
Ok(())
}
Err(e) => Err(format!("Failed to clear credentials: {}", e)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_credentials_roundtrip() {
// Note: This test requires a working keyring service
// Skip in CI environments
if std::env::var("CI").is_ok() {
return;
}
let email = "test@example.com";
let password = "testpass123";
// Save
save_qobuz_credentials(email, password).expect("Failed to save");
// Load
let loaded = load_qobuz_credentials()
.expect("Failed to load")
.expect("No credentials found");
assert_eq!(loaded.email, email);
assert_eq!(loaded.password, password);
// Clear
clear_qobuz_credentials().expect("Failed to clear");
// Verify cleared
let after_clear = load_qobuz_credentials().expect("Failed to check");
assert!(after_clear.is_none());
}
}
+6
View File
@@ -7,6 +7,7 @@ pub mod api;
pub mod cache;
pub mod commands;
pub mod config;
pub mod credentials;
pub mod discogs;
pub mod lastfm;
pub mod library;
@@ -105,6 +106,11 @@ pub fn run() {
commands::logout,
commands::is_logged_in,
commands::get_user_info,
// Credential persistence commands
commands::has_saved_credentials,
commands::save_credentials,
commands::clear_saved_credentials,
commands::auto_login,
// Search commands
commands::search_albums,
commands::search_tracks,