use std::path::{Path, PathBuf};
use zeroize::Zeroizing;
#[derive(Clone)]
pub enum Secret {
Prompt,
File(PathBuf),
Stdin,
Value(Zeroizing<String>),
}
impl std::fmt::Debug for Secret {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Prompt => formatter.write_str("Prompt"),
Self::File(path) => formatter.debug_tuple("File").field(path).finish(),
Self::Stdin => formatter.write_str("Stdin"),
Self::Value(_) => formatter.write_str("Value([redacted])"),
}
}
}
impl Secret {
pub fn value(value: impl Into<String>) -> Self {
Self::Value(Zeroizing::new(value.into()))
}
pub fn file(path: impl Into<PathBuf>) -> Self {
Self::File(path.into())
}
}
#[derive(Clone, Debug)]
pub enum Credential {
Pkcs12 { path: PathBuf, secret: Secret },
CertificateKey {
certificates: PathBuf,
key: PathBuf,
additional: Option<PathBuf>,
secret: Secret,
},
Pkcs11(Pkcs11),
}
impl Credential {
pub fn pkcs12(path: impl AsRef<Path>, secret: Secret) -> Self {
Self::Pkcs12 {
path: path.as_ref().to_path_buf(),
secret,
}
}
pub fn certificate_key(
certificates: impl AsRef<Path>,
key: impl AsRef<Path>,
secret: Secret,
) -> Self {
Self::CertificateKey {
certificates: certificates.as_ref().to_path_buf(),
key: key.as_ref().to_path_buf(),
additional: None,
secret,
}
}
}
#[derive(Clone, Debug)]
pub struct Pkcs11 {
pub module: PathBuf,
pub cert: Option<String>,
pub engine: Option<String>,
pub provider: Option<String>,
pub login: bool,
pub engine_ctrls: Vec<String>,
pub secret: Secret,
}
impl Pkcs11 {
pub fn module(path: impl AsRef<Path>, secret: Secret) -> Self {
Self {
module: path.as_ref().to_path_buf(),
cert: None,
engine: None,
provider: None,
login: false,
engine_ctrls: Vec::new(),
secret,
}
}
}