Conditionally compile MacOS key storage code

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-05-23 15:13:27 -04:00
parent 9fad35485a
commit 0c88c63460
3 changed files with 16 additions and 0 deletions

View File

@@ -41,6 +41,8 @@ strum = "0.26"
strum_macros = "0.26"
bitflags = "2.5.0"
egui_virtual_list = "0.3.0"
[target.'cfg(target_os = "macos")'.dependencies]
security-framework = "2.11.0"

View File

@@ -1,11 +1,14 @@
use enostr::FullKeypair;
#[cfg(target_os = "macos")]
use crate::macos_key_storage::MacOSKeyStorage;
#[cfg(target_os = "macos")]
pub const SERVICE_NAME: &str = "Notedeck";
pub enum KeyStorage {
None,
#[cfg(target_os = "macos")]
MacOS,
// TODO:
// Linux,
@@ -17,6 +20,7 @@ impl KeyStorage {
pub fn get_keys(&self) -> Result<Vec<FullKeypair>, KeyStorageError> {
match self {
Self::None => Ok(Vec::new()),
#[cfg(target_os = "macos")]
Self::MacOS => Ok(MacOSKeyStorage::new(SERVICE_NAME).get_all_fullkeypairs()),
}
}
@@ -25,6 +29,7 @@ impl KeyStorage {
let _ = key;
match self {
Self::None => Ok(()),
#[cfg(target_os = "macos")]
Self::MacOS => MacOSKeyStorage::new(SERVICE_NAME).add_key(key),
}
}
@@ -33,6 +38,7 @@ impl KeyStorage {
let _ = key;
match self {
Self::None => Ok(()),
#[cfg(target_os = "macos")]
Self::MacOS => MacOSKeyStorage::new(SERVICE_NAME).delete_key(&key.pubkey),
}
}
@@ -43,6 +49,7 @@ pub enum KeyStorageError {
Retrieval,
Addition(String),
Removal(String),
UnsupportedPlatform,
}
impl std::fmt::Display for KeyStorageError {
@@ -51,6 +58,10 @@ impl std::fmt::Display for KeyStorageError {
Self::Retrieval => write!(f, "Failed to retrieve keys."),
Self::Addition(key) => write!(f, "Failed to add key: {:?}", key),
Self::Removal(key) => write!(f, "Failed to remove key: {:?}", key),
Self::UnsupportedPlatform => write!(
f,
"Attempted to use a key storage impl from an unsupported platform."
),
}
}
}

View File

@@ -1,4 +1,7 @@
#![cfg(target_os = "macos")]
use enostr::{FullKeypair, Pubkey, SecretKey};
use security_framework::item::{ItemClass, ItemSearchOptions, Limit, SearchResult};
use security_framework::passwords::{delete_generic_password, set_generic_password};