use std::sync::{Arc, OnceLock};
use keyring_core::{CredentialStore, Entry, Error as KeyringError};
use zeroize::Zeroizing;
use super::{hex, KeyStore, StoredSecret, KEYCHAIN_SERVICE};
use crate::crypto::SALT_LEN;
use crate::error::{MnemoError, Result};
pub(super) struct OsKeyStore;
const PROBE_ACCOUNT: &str = "mnemo-availability-probe";
fn platform_store() -> std::result::Result<&'static Arc<CredentialStore>, &'static str> {
static STORE: OnceLock<std::result::Result<Arc<CredentialStore>, String>> = OnceLock::new();
STORE
.get_or_init(|| {
let made: keyring_core::Result<Arc<CredentialStore>> = {
#[cfg(target_os = "macos")]
{
apple_native_keyring_store::keychain::Store::new()
.map(|s| s as Arc<CredentialStore>)
}
#[cfg(windows)]
{
windows_native_keyring_store::Store::new().map(|s| s as Arc<CredentialStore>)
}
#[cfg(target_os = "linux")]
{
dbus_secret_service_keyring_store::Store::new()
.map(|s| s as Arc<CredentialStore>)
}
};
made.map_err(|e| e.to_string())
})
.as_ref()
.map_err(String::as_str)
}
fn entry(account: &str) -> Result<Entry> {
let store = platform_store().map_err(|e| {
MnemoError::Io(std::io::Error::other(format!(
"{} (opening it): {e}",
OsKeyStore.describe()
)))
})?;
store
.build(KEYCHAIN_SERVICE, account, None)
.map_err(|e| store_error("opening an entry", e))
}
fn store_error(what: &str, e: KeyringError) -> MnemoError {
MnemoError::Io(std::io::Error::other(format!(
"{} ({what}): {e}",
OsKeyStore.describe()
)))
}
impl KeyStore for OsKeyStore {
fn describe(&self) -> String {
if cfg!(target_os = "macos") {
"the macOS keychain".into()
} else if cfg!(windows) {
"Windows Credential Manager".into()
} else {
"the Secret Service keyring".into()
}
}
fn get(&self, salt: &[u8; SALT_LEN]) -> Result<Option<StoredSecret>> {
match entry(&hex(salt))?.get_password() {
Ok(text) => StoredSecret::decode(&Zeroizing::new(text)).map(Some),
Err(KeyringError::NoEntry) => Ok(None),
Err(e) => Err(store_error("reading", e)),
}
}
fn put(&self, salt: &[u8; SALT_LEN], secret: &StoredSecret, _label: &str) -> Result<()> {
entry(&hex(salt))?
.set_password(&secret.encode())
.map_err(|e| store_error("writing", e))
}
fn delete(&self, salt: &[u8; SALT_LEN]) -> Result<()> {
match entry(&hex(salt))?.delete_credential() {
Ok(()) | Err(KeyringError::NoEntry) => Ok(()),
Err(e) => Err(store_error("removing", e)),
}
}
fn check_available(&self) -> Result<()> {
match entry(PROBE_ACCOUNT)?.get_password() {
Ok(_) | Err(KeyringError::NoEntry) => Ok(()),
Err(e) => Err(store_error("checking it can be used", e)),
}
}
}