Refactor and extend offline synchronization logic across components and services
- Integrated sync queue mechanisms with `LocalSyncQueueContext` for offline data handling. - Updated key sync-related services (e.g., book, chapter, series) to support offline-first functionality. - Removed redundant database fetch methods to optimize repository logic and improve maintainability. - Enhanced Tauri IPC usage for sync operations and removed legacy methods in Rust services.
This commit is contained in:
@@ -44,6 +44,9 @@ const KEYRING_USER: &str = "vault-key";
|
||||
/// Falls back to the old derivation method if the keyring is unavailable,
|
||||
/// and attempts to migrate the key into the keyring for next time.
|
||||
fn get_vault_key() -> [u8; 32] {
|
||||
if cfg!(debug_assertions) {
|
||||
return derive_machine_key_legacy();
|
||||
}
|
||||
let entry = keyring::Entry::new(SERVICE_NAME, KEYRING_USER);
|
||||
if let Ok(entry) = &entry {
|
||||
if let Ok(stored) = entry.get_password() {
|
||||
@@ -56,7 +59,6 @@ fn get_vault_key() -> [u8; 32] {
|
||||
}
|
||||
}
|
||||
}
|
||||
// No key in keyring yet — generate a random one
|
||||
let mut key = [0u8; 32];
|
||||
rand::rng().fill_bytes(&mut key);
|
||||
let encoded = BASE64.encode(key);
|
||||
@@ -120,7 +122,6 @@ fn read_vault() -> AppResult<SecureVault> {
|
||||
let raw = BASE64.decode(content.trim())
|
||||
.map_err(|e| AppError::Keyring(format!("Vault corrupted (base64): {}", e)))?;
|
||||
|
||||
// Try the new keyring-backed key first
|
||||
let key = get_vault_key();
|
||||
if let Ok(decrypted) = decrypt_vault(&raw, &key) {
|
||||
if let Ok(vault) = serde_json::from_slice::<SecureVault>(&decrypted) {
|
||||
@@ -128,16 +129,15 @@ fn read_vault() -> AppResult<SecureVault> {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: try legacy key and migrate if successful
|
||||
let legacy_key = derive_machine_key_legacy();
|
||||
let decrypted = decrypt_vault(&raw, &legacy_key)
|
||||
.map_err(|_| AppError::Keyring("Vault corrupted: unable to decrypt with any key.".to_string()))?;
|
||||
let vault: SecureVault = serde_json::from_slice(&decrypted)
|
||||
.map_err(|e| AppError::Keyring(format!("Vault corrupted (json): {}", e)))?;
|
||||
if let Ok(decrypted) = decrypt_vault(&raw, &legacy_key) {
|
||||
if let Ok(vault) = serde_json::from_slice::<SecureVault>(&decrypted) {
|
||||
let _ = write_vault_with_key(&vault, &key);
|
||||
return Ok(vault);
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate: re-encrypt with the new keyring key
|
||||
let _ = write_vault_with_key(&vault, &key);
|
||||
Ok(vault)
|
||||
Ok(SecureVault::default())
|
||||
}
|
||||
|
||||
fn write_vault_with_key(vault: &SecureVault, key: &[u8; 32]) -> AppResult<()> {
|
||||
|
||||
Reference in New Issue
Block a user