use crate::config::{ApiKeyConfig, AppConfig};
pub fn find_key<'a>(config: &'a AppConfig, key_id: &str) -> Option<&'a ApiKeyConfig> {
config.security.api_keys.iter().find(|k| k.id == key_id)
}
pub fn domain_permitted_globally(config: &AppConfig, domain: &str) -> bool {
config.mail.allowed_recipient_domains.is_empty()
|| config
.mail
.allowed_recipient_domains
.iter()
.any(|d| d.eq_ignore_ascii_case(domain))
}
pub fn domain_permitted_for_key(key: &ApiKeyConfig, domain: &str) -> bool {
key.allowed_recipient_domains.is_empty()
|| key
.allowed_recipient_domains
.iter()
.any(|d| d.eq_ignore_ascii_case(domain))
}
pub fn address_permitted_for_key(key: &ApiKeyConfig, address: &str) -> bool {
if !key.allowed_recipients.is_empty() {
return key
.allowed_recipients
.iter()
.any(|r| r.eq_ignore_ascii_case(address));
}
domain_permitted_for_key(key, extract_domain(address))
}
fn extract_domain(address: &str) -> &str {
address.rfind('@').map(|i| &address[i + 1..]).unwrap_or("")
}