| 1 | //! OS keyring storage for CalDAV app passwords (ADR 0019). |
| 2 | |
| 3 | const SERVICE: &str = "rusty-teams.caldav"; |
| 4 | |
| 5 | pub fn store_password(account_id: &str, password: &str) -> Result<(), String> { |
| 6 | let entry = keyring::Entry::new(SERVICE, account_id).map_err(|e| e.to_string())?; |
| 7 | entry.set_password(password).map_err(|e| e.to_string()) |
| 8 | } |
| 9 | |
| 10 | pub fn load_password(account_id: &str) -> Result<String, String> { |
| 11 | let entry = keyring::Entry::new(SERVICE, account_id).map_err(|e| e.to_string())?; |
| 12 | entry.get_password().map_err(|e| e.to_string()) |
| 13 | } |
| 14 | |
| 15 | pub fn delete_password(account_id: &str) -> Result<(), String> { |
| 16 | let entry = keyring::Entry::new(SERVICE, account_id).map_err(|e| e.to_string())?; |
| 17 | match entry.delete_credential() { |
| 18 | Ok(()) => Ok(()), |
| 19 | Err(keyring::Error::NoEntry) => Ok(()), |
| 20 | Err(error) => Err(error.to_string()), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | pub async fn store_password_async(account_id: String, password: String) -> Result<(), String> { |
| 25 | tokio::task::spawn_blocking(move || store_password(&account_id, &password)) |
| 26 | .await |
| 27 | .map_err(|error| error.to_string())? |
| 28 | } |
| 29 | |
| 30 | pub async fn load_password_async(account_id: String) -> Result<String, String> { |
| 31 | tokio::task::spawn_blocking(move || load_password(&account_id)) |
| 32 | .await |
| 33 | .map_err(|error| error.to_string())? |
| 34 | } |
| 35 | |
| 36 | pub async fn delete_password_async(account_id: String) -> Result<(), String> { |
| 37 | tokio::task::spawn_blocking(move || delete_password(&account_id)) |
| 38 | .await |
| 39 | .map_err(|error| error.to_string())? |
| 40 | } |
| 41 | |
| 42 | /// Redact credentials that must never appear in diagnostics or support bundles. |
| 43 | pub fn redact_secret_text(text: &str) -> String { |
| 44 | let mut out = text.to_owned(); |
| 45 | // user:pass@host |
| 46 | if let Some(scheme_end) = out.find("://") { |
| 47 | let after = scheme_end + 3; |
| 48 | if let Some(at) = out[after..].find('@') { |
| 49 | let creds = &out[after..after + at]; |
| 50 | if creds.contains(':') { |
| 51 | out.replace_range(after..after + at, "***:***"); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | out |
| 56 | } |
| 57 | |
| 58 | #[cfg(test)] |
| 59 | mod tests { |
| 60 | use super::*; |
| 61 | |
| 62 | #[test] |
| 63 | fn redacts_userinfo_in_urls() { |
| 64 | let scrubbed = redact_secret_text("GET https://joe:s3cret@cloud.example/dav/ ok"); |
| 65 | assert!(!scrubbed.contains("s3cret")); |
| 66 | assert!(scrubbed.contains("***:***@cloud.example")); |
| 67 | } |
| 68 | } |