use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use super::{Entry, Error, Result};
pub trait CredentialApi {
fn set_password(&self, password: &str) -> Result<()> {
self.set_secret(password.as_bytes())
}
fn set_secret(&self, secret: &[u8]) -> Result<()>;
fn get_password(&self) -> Result<String> {
let secret = self.get_secret()?;
super::error::decode_password(secret)
}
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<()> {
Err(Error::NotSupportedByStore(String::from(
"This store does not allow attribute updates",
)))
}
fn delete_credential(&self) -> Result<()>;
fn get_credential(&self) -> Result<Option<Arc<Credential>>>;
fn get_specifiers(&self) -> Option<(String, String)>;
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,
UntilLogout,
UntilReboot,
UntilDelete,
Unspecified,
}
pub trait CredentialStoreApi {
fn vendor(&self) -> String;
fn id(&self) -> String;
fn build(
&self,
service: &str,
user: &str,
modifiers: Option<&HashMap<&str, &str>>,
) -> Result<Entry>;
fn search(&self, _spec: &HashMap<&str, &str>) -> Result<Vec<Entry>> {
let msg = "This store does not provide search capabilities";
Err(Error::NotSupportedByStore(msg.to_string()))
}
fn as_any(&self) -> &dyn Any;
fn persistence(&self) -> CredentialPersistence {
CredentialPersistence::UntilDelete
}
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self.as_any(), f)
}
}
impl std::fmt::Debug for CredentialStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.debug_fmt(f)
}
}
pub type CredentialStore = dyn CredentialStoreApi + Send + Sync;