Simplified keychain management on non-Windows platforms

This commit is contained in:
2025-12-21 12:35:17 +08:00
parent 0e0cb8f33a
commit e1de05de7a

View File

@@ -187,6 +187,8 @@ pub fn save_auth_pass(auth_pass: &AuthPass) -> Result<(), OAuthError> {
let encoded = URL_SAFE_NO_PAD.encode(&compressed); let encoded = URL_SAFE_NO_PAD.encode(&compressed);
info!("Encoded length: {}", encoded.len()); info!("Encoded length: {}", encoded.len());
#[cfg(target_os = "windows")]
{
// Windows keyring has a 2560-byte UTF-16 limit, which means 1280 chars max // Windows keyring has a 2560-byte UTF-16 limit, which means 1280 chars max
// Split into chunks of 1200 chars to be safe // Split into chunks of 1200 chars to be safe
const CHUNK_SIZE: usize = 1200; const CHUNK_SIZE: usize = 1200;
@@ -212,6 +214,14 @@ pub fn save_auth_pass(auth_pass: &AuthPass) -> Result<(), OAuthError> {
"Auth pass saved to keyring successfully in {} chunks", "Auth pass saved to keyring successfully in {} chunks",
chunks.len() chunks.len()
); );
}
#[cfg(not(target_os = "windows"))]
{
let entry = Entry::new(SERVICE_NAME, "auth_pass")?;
entry.set_password(&encoded)?;
info!("Auth pass saved to keyring successfully");
}
Ok(()) Ok(())
} }
@@ -219,6 +229,8 @@ pub fn save_auth_pass(auth_pass: &AuthPass) -> Result<(), OAuthError> {
pub fn load_auth_pass() -> Result<Option<AuthPass>, OAuthError> { pub fn load_auth_pass() -> Result<Option<AuthPass>, OAuthError> {
info!("Reading credentials from keyring"); info!("Reading credentials from keyring");
#[cfg(target_os = "windows")]
let encoded = {
// Get chunk count // Get chunk count
let count_entry = Entry::new(SERVICE_NAME, "auth_pass_count")?; let count_entry = Entry::new(SERVICE_NAME, "auth_pass_count")?;
let chunk_count = match count_entry.get_password() { let chunk_count = match count_entry.get_password() {
@@ -253,6 +265,24 @@ pub fn load_auth_pass() -> Result<Option<AuthPass>, OAuthError> {
} }
} }
} }
encoded
};
#[cfg(not(target_os = "windows"))]
let encoded = {
let entry = Entry::new(SERVICE_NAME, "auth_pass")?;
match entry.get_password() {
Ok(pass) => pass,
Err(keyring::Error::NoEntry) => {
info!("No auth pass found in keyring");
return Ok(None);
}
Err(e) => {
error!("Failed to load auth pass from keyring");
return Err(OAuthError::KeyringError(e));
}
}
};
info!("Reassembled encoded length: {}", encoded.len()); info!("Reassembled encoded length: {}", encoded.len());
@@ -288,6 +318,8 @@ pub fn load_auth_pass() -> Result<Option<AuthPass>, OAuthError> {
/// Clear auth_pass from secure storage and app state. /// Clear auth_pass from secure storage and app state.
pub fn clear_auth_pass() -> Result<(), OAuthError> { pub fn clear_auth_pass() -> Result<(), OAuthError> {
#[cfg(target_os = "windows")]
{
// Try to get chunk count // Try to get chunk count
let count_entry = Entry::new(SERVICE_NAME, "auth_pass_count")?; let count_entry = Entry::new(SERVICE_NAME, "auth_pass_count")?;
let chunk_count = match count_entry.get_password() { let chunk_count = match count_entry.get_password() {
@@ -303,6 +335,13 @@ pub fn clear_auth_pass() -> Result<(), OAuthError> {
// Delete chunk count // Delete chunk count
let _ = count_entry.delete_credential(); let _ = count_entry.delete_credential();
}
#[cfg(not(target_os = "windows"))]
{
let entry = Entry::new(SERVICE_NAME, "auth_pass")?;
let _ = entry.delete_credential();
}
info!("Auth pass cleared from keyring successfully"); info!("Auth pass cleared from keyring successfully");
Ok(()) Ok(())