use std::any::Any;
use std::collections::HashMap;
use super::Result;
pub trait CredentialApi {
fn set_password(&self, password: &str) -> Result<()>;
fn set_secret(&self, password: &[u8]) -> Result<()>;
fn get_password(&self) -> Result<String>;
fn get_secret(&self) -> Result<Vec<u8>>;
fn get_attributes(&self) -> Result<HashMap<String, String>> {
self.get_secret()?;
Ok(HashMap::new())
}
fn update_attributes(&self, _: &HashMap<&str, &str>) -> Result<()> {
self.get_secret()?;
Ok(())
}
fn delete_credential(&self) -> Result<()>;
fn as_any(&self) -> &dyn Any;
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self.as_any(), f)
}
}
pub type Credential = dyn CredentialApi + Send + Sync;
impl std::fmt::Debug for Credential {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.debug_fmt(f)
}
}
#[non_exhaustive]
pub enum CredentialPersistence {
EntryOnly,
ProcessOnly,
UntilReboot,
UntilDelete,
}
pub trait CredentialBuilderApi {
fn build(&self, target: Option<&str>, service: &str, user: &str) -> Result<Box<Credential>>;
fn as_any(&self) -> &dyn Any;
fn persistence(&self) -> CredentialPersistence {
CredentialPersistence::UntilDelete
}
}
impl std::fmt::Debug for CredentialBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_any().fmt(f)
}
}
pub type CredentialBuilder = dyn CredentialBuilderApi + Send + Sync;