use std::collections::HashMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use keyring_core::api::{CredentialPersistence, CredentialStoreApi};
use keyring_core::attributes::parse_attributes;
use keyring_core::{Entry, Result};
use super::Cred;
#[derive(Clone)]
pub struct Store {
pub id: String,
pub delimiters: [String; 3],
pub service_no_divider: bool,
}
impl std::fmt::Debug for Store {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Store")
.field("vendor", &self.vendor())
.field("id", &self.id())
.field("delimiters", &self.delimiters)
.field("service_no_divider", &self.service_no_divider)
.finish()
}
}
impl Store {
pub fn new() -> Result<Arc<Self>> {
Ok(Self::new_internal(
["keyring:".to_string(), "@".to_string(), "".to_string()],
false,
))
}
pub fn new_with_configuration(config: &HashMap<&str, &str>) -> Result<Arc<Self>> {
let config = parse_attributes(
&["prefix", "divider", "suffix", "*service_no_divider"],
Some(config),
)?;
let prefix = config
.get("prefix")
.map(|s| s.as_str())
.unwrap_or("keyring:")
.to_string();
let divider = config
.get("divider")
.map(|s| s.as_str())
.unwrap_or("@")
.to_string();
let suffix = config
.get("suffix")
.map(|s| s.as_str())
.unwrap_or("")
.to_string();
let service_no_divider = config
.get("service_no_divider")
.map(|s| s.as_str())
.unwrap_or("false")
.eq("true");
Ok(Self::new_internal(
[prefix, divider, suffix],
service_no_divider,
))
}
fn new_internal(delimiters: [String; 3], service_no_divider: bool) -> Arc<Self> {
let now = SystemTime::now();
let elapsed = if now.lt(&UNIX_EPOCH) {
UNIX_EPOCH.duration_since(now).unwrap()
} else {
now.duration_since(UNIX_EPOCH).unwrap()
};
Arc::new(Store {
id: format!(
"Crate version {}, Instantiated at {}",
env!("CARGO_PKG_VERSION"),
elapsed.as_secs_f64()
),
delimiters,
service_no_divider,
})
}
}
impl CredentialStoreApi for Store {
fn vendor(&self) -> String {
"Linux keyutils, https://crates.io/crates/linux-keyutils-keyring-store".to_string()
}
fn id(&self) -> String {
self.id.clone()
}
fn build(
&self,
service: &str,
user: &str,
modifiers: Option<&HashMap<&str, &str>>,
) -> Result<Entry> {
let mods = parse_attributes(&["description"], modifiers)?;
let description = mods.get("description").map(|s| s.as_str());
let cred = Cred::build_from_specifiers(
description,
&self.delimiters,
self.service_no_divider,
service,
user,
)?;
Ok(Entry::new_with_credential(Arc::new(cred)))
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn persistence(&self) -> CredentialPersistence {
CredentialPersistence::UntilReboot
}
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}