|
| 1 | +//! The saved Builderlab browser sign-in credential of a build whose |
| 2 | +//! Builderlab plugin does not use NIP-FI, so that sign-in survives restarts. |
| 3 | +//! It is one OS secure item of its own per profile, never another item. |
| 4 | +use zeroize::Zeroizing; |
| 5 | + |
| 6 | +#[cfg_attr(test, allow(dead_code))] |
| 7 | +const SERVICE: &str = if cfg!(debug_assertions) { |
| 8 | + "xyz.block.buzz.desktop.builderlab-session.debug" |
| 9 | +} else { |
| 10 | + "xyz.block.buzz.desktop.builderlab-session" |
| 11 | +}; |
| 12 | +const LIMIT: usize = 4096; |
| 13 | + |
| 14 | +#[cfg_attr(test, allow(dead_code))] |
| 15 | +fn account() -> String { |
| 16 | + std::env::var("BUZZODZ_PROFILE").unwrap_or_else(|_| "default".into()) |
| 17 | +} |
| 18 | + |
| 19 | +#[cfg(all( |
| 20 | + any(target_os = "macos", target_os = "windows", target_os = "linux"), |
| 21 | + not(test) |
| 22 | +))] |
| 23 | +mod platform { |
| 24 | + use super::*; |
| 25 | + use buzz_credential_store::{self as credentials, Error}; |
| 26 | + fn error(error: Error) -> String { |
| 27 | + match error { |
| 28 | + Error::Denied => "Secure storage access was denied", |
| 29 | + Error::Busy => "Another Buzz app is using secure storage", |
| 30 | + _ => "Secure storage is unavailable", |
| 31 | + } |
| 32 | + .into() |
| 33 | + } |
| 34 | + pub(super) fn read() -> Result<Option<Zeroizing<Vec<u8>>>, String> { |
| 35 | + match credentials::read(SERVICE, &account()) { |
| 36 | + Ok(value) => Ok(Some(value)), |
| 37 | + Err(Error::Absent) => Ok(None), |
| 38 | + Err(e) => Err(error(e)), |
| 39 | + } |
| 40 | + } |
| 41 | + pub(super) fn replace(value: &[u8]) -> Result<(), String> { |
| 42 | + #[cfg(target_os = "macos")] |
| 43 | + return security_framework::os::macos::keychain::SecKeychain::default() |
| 44 | + .and_then(|keychain| keychain.set_generic_password(SERVICE, &account(), value)) |
| 45 | + .map_err(|_| "Secure storage is unavailable".to_string()); |
| 46 | + #[cfg(not(target_os = "macos"))] |
| 47 | + credentials::replace(SERVICE, &account(), value).map_err(error) |
| 48 | + } |
| 49 | + pub(super) fn delete() -> Result<(), String> { |
| 50 | + match credentials::delete(SERVICE, &account()) { |
| 51 | + Ok(()) | Err(Error::Absent) => Ok(()), |
| 52 | + Err(e) => Err(error(e)), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Native tests and other platforms never touch an OS credential store. |
| 58 | +#[cfg(any( |
| 59 | + not(any(target_os = "macos", target_os = "windows", target_os = "linux")), |
| 60 | + test |
| 61 | +))] |
| 62 | +mod platform { |
| 63 | + use super::*; |
| 64 | + const NONE: &str = "Secure storage is not available here"; |
| 65 | + pub(super) fn read() -> Result<Option<Zeroizing<Vec<u8>>>, String> { |
| 66 | + Err(NONE.into()) |
| 67 | + } |
| 68 | + pub(super) fn replace(_: &[u8]) -> Result<(), String> { |
| 69 | + Err(NONE.into()) |
| 70 | + } |
| 71 | + pub(super) fn delete() -> Result<(), String> { |
| 72 | + Err(NONE.into()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async fn blocking<T: Send + 'static>( |
| 77 | + run: impl FnOnce() -> Result<T, String> + Send + 'static, |
| 78 | +) -> Result<T, String> { |
| 79 | + tokio::task::spawn_blocking(run) |
| 80 | + .await |
| 81 | + .map_err(|_| "Secure storage is unavailable".to_string())? |
| 82 | +} |
| 83 | + |
| 84 | +/// The saved credential record, or `None` when there is none. |
| 85 | +#[tauri::command] |
| 86 | +pub(crate) async fn builderlab_session_load() -> Result<Option<String>, String> { |
| 87 | + let value = blocking(platform::read).await?; |
| 88 | + value |
| 89 | + .map(|bytes| { |
| 90 | + String::from_utf8(bytes.to_vec()).map_err(|_| "The saved sign-in is damaged".into()) |
| 91 | + }) |
| 92 | + .transpose() |
| 93 | +} |
| 94 | + |
| 95 | +#[tauri::command] |
| 96 | +pub(crate) async fn builderlab_session_save(value: String) -> Result<(), String> { |
| 97 | + let value = Zeroizing::new(value); |
| 98 | + if value.is_empty() || value.len() > LIMIT { |
| 99 | + return Err("Invalid Builderlab sign-in".into()); |
| 100 | + } |
| 101 | + blocking(move || platform::replace(value.as_bytes())).await |
| 102 | +} |
| 103 | + |
| 104 | +#[tauri::command] |
| 105 | +pub(crate) async fn builderlab_session_clear() -> Result<(), String> { |
| 106 | + blocking(platform::delete).await |
| 107 | +} |
0 commit comments