use crate::{active_children, secret_value, split_keyword, split_name, Secret};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AuthenticationSecret {
pub method: String,
pub value: Secret,
}
fn from_statement(statement: &str) -> Option<AuthenticationSecret> {
let (method, rest) = split_keyword(statement);
let value = secret_value((!rest.is_empty()).then(|| split_name(rest).0))?;
Some(AuthenticationSecret {
method: method.to_string(),
value,
})
}
pub(super) fn authentication_secrets(auth: &ConfigLine) -> Vec<AuthenticationSecret> {
active_children(auth).filter_map(|c| from_statement(&c.text)).collect()
}
pub(super) fn password_secrets(system: &ConfigLine, keyword: &str) -> Vec<AuthenticationSecret> {
let mut secrets = Vec::new();
for child in active_children(system) {
if child.text == keyword {
secrets.extend(authentication_secrets(child));
} else if let Some(secret) = child.arg(keyword).and_then(from_statement) {
secrets.push(secret);
}
}
secrets
}