use std::any::Any;
use std::collections::HashMap;
use crate::Result;
#[async_trait::async_trait]
pub trait CredentialApi {
async fn set_password(&self, password: &str) -> Result<()> {
self.set_secret(password.as_bytes()).await
}
async fn set_secret(&self, password: &[u8]) -> Result<()>;
async fn get_password(&self) -> Result<String> {
let secret = self.get_secret().await?;
crate::error::decode_password(secret)
}
async fn get_secret(&self) -> Result<Vec<u8>>;
async fn get_attributes(&self) -> Result<HashMap<String, String>> {
self.get_secret().await?;
Ok(HashMap::new())
}
async fn update_attributes(&self, _: &HashMap<&str, &str>) -> Result<()> {
self.get_secret().await?;
Ok(())
}
async 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;
struct NopCredentialBuilder;
impl CredentialBuilderApi for NopCredentialBuilder {
fn build(&self, _: Option<&str>, _: &str, _: &str) -> Result<Box<Credential>> {
Err(super::Error::NoDefaultCredentialBuilder)
}
fn as_any(&self) -> &dyn Any {
self
}
fn persistence(&self) -> CredentialPersistence {
CredentialPersistence::EntryOnly
}
}
pub fn nop_credential_builder() -> Box<CredentialBuilder> {
Box::new(NopCredentialBuilder)
}