#[cfg(any(
feature = "encrypted-vfs",
feature = "memory",
feature = "turso-encrypted-io"
))]
mod cipher;
mod keyringsecret;
#[cfg(feature = "encrypted-vfs")]
pub mod encrypted_vfs;
#[cfg(any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
))]
pub mod file_common;
#[cfg(feature = "indexeddb")]
pub mod indexeddb;
#[cfg(any(feature = "yubihsm-usb", feature = "yubihsm-http"))]
pub mod kms;
#[cfg(all(target_os = "linux", feature = "linux-secret-service"))]
pub mod linux;
#[cfg(all(target_os = "macos", feature = "macos-keychain"))]
pub mod macos;
#[cfg(feature = "memory")]
pub mod memory;
#[cfg(feature = "turso-opfs")]
pub mod opfs;
#[cfg(any(feature = "encrypted-vfs", feature = "turso-encrypted-io"))]
mod page_cipher;
#[cfg(any(
feature = "encrypted-vfs",
feature = "turso",
feature = "turso-encrypted-io",
feature = "turso-opfs"
))]
mod page_store;
#[cfg(feature = "file")]
pub mod sqlcipher;
#[cfg(any(feature = "file", feature = "encrypted-vfs"))]
mod sqlite_common;
#[cfg(feature = "turso")]
pub mod turso;
#[cfg(any(feature = "turso", feature = "turso-encrypted-io"))]
mod turso_common;
#[cfg(feature = "turso-encrypted-io")]
pub mod turso_encrypted_io;
#[cfg(feature = "turso")]
mod turso_web;
#[cfg(feature = "web-storage")]
pub mod web_storage;
#[cfg(any(feature = "turso-opfs", feature = "turso"))]
mod web_time;
#[cfg(all(target_os = "windows", feature = "windows-credentials"))]
pub mod windows;
#[cfg(any(feature = "yubihsm-usb", feature = "yubihsm-http"))]
pub mod yubihsm;
#[cfg(all(target_os = "macos", feature = "macos-keychain"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "macos", feature = "macos-keychain"))))]
pub use self::macos::MacOsKeyRing as OsKeyRing;
#[cfg(all(target_os = "linux", feature = "linux-secret-service"))]
#[cfg_attr(
docsrs,
doc(cfg(all(target_os = "linux", feature = "linux-secret-service")))
)]
pub use self::linux::LinuxOsKeyRing as OsKeyRing;
#[cfg(all(target_os = "windows", feature = "windows-credentials"))]
#[cfg_attr(
docsrs,
doc(cfg(all(target_os = "windows", feature = "windows-credentials")))
)]
pub use self::windows::WindowsOsKeyRing as OsKeyRing;
use std::collections::BTreeMap;
pub type Result<T> = std::result::Result<T, crate::error::KeyRingError>;
pub use keyringsecret::*;
#[cfg(any(
all(target_os = "linux", feature = "linux-secret-service"),
all(target_os = "macos", feature = "macos-keychain"),
))]
use uzers::{get_current_username, get_effective_username};
#[cfg(any(
all(target_os = "macos", feature = "macos-keychain"),
all(target_os = "windows", feature = "windows-credentials"),
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
all(target_os = "macos", feature = "macos-keychain"),
all(target_os = "windows", feature = "windows-credentials"),
)))
)]
pub fn get_os_keyring(service: &str) -> Result<OsKeyRing> {
OsKeyRing::new(service)
}
#[cfg(all(target_os = "linux", feature = "linux-secret-service"))]
#[cfg_attr(
docsrs,
doc(cfg(all(target_os = "linux", feature = "linux-secret-service")))
)]
pub fn get_os_keyring(service: &str) -> Result<OsKeyRing<'_>> {
OsKeyRing::new(service)
}
#[cfg(not(any(
all(target_os = "macos", feature = "macos-keychain"),
all(target_os = "windows", feature = "windows-credentials"),
all(target_os = "linux", feature = "linux-secret-service"),
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io",
feature = "yubihsm-usb",
feature = "yubihsm-http",
)))]
compile_error!("no keyring implementation is selected or available for this platform");
#[cfg(all(
target_family = "wasm",
any(
feature = "file",
feature = "encrypted-vfs",
feature = "yubihsm-usb",
feature = "yubihsm-http",
feature = "linux-secret-service",
feature = "macos-keychain",
feature = "windows-credentials",
)
))]
compile_error!(
"the file, encrypted-vfs, yubihsm and OS keyring backends are not available on wasm; \
use the memory, web-storage, indexeddb, turso-opfs or turso features instead"
);
#[cfg(all(
not(all(target_arch = "wasm32", target_os = "unknown")),
any(
feature = "web-storage",
feature = "indexeddb",
feature = "turso-opfs",
feature = "js"
)
))]
compile_error!(
"the web-storage, indexeddb, turso-opfs and js features require wasm32-unknown-unknown"
);
#[cfg(any(
all(target_os = "linux", feature = "linux-secret-service"),
all(target_os = "macos", feature = "macos-keychain"),
))]
fn get_username() -> String {
fn fallback_username() -> String {
whoami::username().unwrap_or_else(|_| String::from("unknown"))
}
fn get_current_user() -> String {
match get_current_username() {
Some(s) => s.into_string().unwrap_or_else(|_| fallback_username()),
None => fallback_username(),
}
}
match get_effective_username() {
Some(s) => s.into_string().unwrap_or_else(|_| get_current_user()),
None => get_current_user(),
}
}
#[cfg(feature = "file")]
pub const fn allows_file() -> bool {
true
}
#[cfg(not(feature = "file"))]
pub const fn allows_file() -> bool {
false
}
#[cfg(all(
not(test),
any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
)
))]
pub const fn default_memory_cost() -> u32 {
32 * 1024
}
#[cfg(all(
test,
any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
)
))]
pub const fn default_memory_cost() -> u32 {
argon2::Params::DEFAULT_M_COST
}
#[cfg(all(
not(test),
any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
)
))]
pub const fn default_threads() -> u32 {
3
}
#[cfg(all(
test,
any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
)
))]
pub const fn default_threads() -> u32 {
argon2::Params::DEFAULT_T_COST
}
#[cfg(all(
not(test),
any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
)
))]
pub const fn default_parallel() -> u32 {
2
}
#[cfg(all(
test,
any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
)
))]
pub const fn default_parallel() -> u32 {
argon2::Params::DEFAULT_P_COST
}
pub trait KeyRing: Send + Sync {
fn get_secret<S: AsRef<str>>(&mut self, id: S) -> Result<KeyRingSecret>;
fn set_secret<S: AsRef<str>, B: AsRef<[u8]>>(&mut self, id: S, secret: B) -> Result<()>;
fn delete_secret<S: AsRef<str>>(&mut self, id: S) -> Result<()>;
}
pub trait DynKeyRing: Send + Sync {
fn get_secret(&mut self, id: &str) -> Result<KeyRingSecret>;
fn set_secret(&mut self, id: &str, secret: &[u8]) -> Result<()>;
fn delete_secret(&mut self, id: &str) -> Result<()>;
}
impl<D: DynKeyRing> KeyRing for D {
fn get_secret<S: AsRef<str>>(&mut self, id: S) -> Result<KeyRingSecret> {
DynKeyRing::get_secret(self, id.as_ref())
}
fn set_secret<S: AsRef<str>, B: AsRef<[u8]>>(&mut self, id: S, secret: B) -> Result<()> {
DynKeyRing::set_secret(self, id.as_ref(), secret.as_ref())
}
fn delete_secret<S: AsRef<str>>(&mut self, id: S) -> Result<()> {
DynKeyRing::delete_secret(self, id.as_ref())
}
}
pub trait NewKeyRing: Sized {
fn new<S: AsRef<str>>(service: S) -> Result<Self>;
}
pub trait PeekableKeyRing {
fn peek_secret<S: AsRef<str>>(id: S) -> Result<Vec<(String, KeyRingSecret)>>;
}
pub trait ListKeyRing {
fn list_secrets() -> Result<Vec<BTreeMap<String, String>>>;
}
#[cfg(all(
test,
any(
feature = "file",
feature = "encrypted-vfs",
feature = "memory",
feature = "turso",
feature = "turso-encrypted-io"
)
))]
mod tests {
use super::*;
#[test]
fn test_defaults_use_argon2_defaults() {
assert_eq!(default_memory_cost(), argon2::Params::DEFAULT_M_COST);
assert_eq!(default_threads(), argon2::Params::DEFAULT_T_COST);
assert_eq!(default_parallel(), argon2::Params::DEFAULT_P_COST);
}
}
#[cfg(any(
all(target_os = "macos", feature = "macos-keychain"),
all(target_os = "linux", feature = "linux-secret-service"),
))]
pub(crate) fn parse_peek_criteria(id: &str) -> BTreeMap<String, String> {
let mut result = BTreeMap::new();
if !id.is_empty() {
for pair in id.split(',') {
if let Some((key, value)) = pair.split_once('=') {
result.insert(key.to_string(), value.to_string());
}
}
}
result
}