use std::time::Duration;
#[derive(Debug, Clone)]
pub struct LdapResponse<T> {
pub data: T,
pub backend_used: LdapBackendUsed,
}
impl<T> LdapResponse<T> {
pub fn new(data: T, backend_used: LdapBackendUsed) -> Self {
Self { data, backend_used }
}
}
#[derive(Debug, Clone)]
pub struct LdapEmptyResponse {
pub backend_used: LdapBackendUsed,
}
impl LdapEmptyResponse {
pub fn new(backend_used: LdapBackendUsed) -> Self {
Self { backend_used }
}
}
#[derive(Debug, Clone)]
pub struct LdapBackendUsed {
pub server_type: ServerType,
pub tls_mode_used: TlsMode,
pub auth_method_used: String,
pub connection_time_ms: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServerType {
OpenLDAP,
DirectoryServer389,
ActiveDirectory,
Unknown,
}
impl std::fmt::Display for ServerType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::OpenLDAP => write!(f, "OpenLDAP"),
Self::DirectoryServer389 => write!(f, "389 Directory Server"),
Self::ActiveDirectory => write!(f, "Active Directory"),
Self::Unknown => write!(f, "Unknown"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TlsMode {
None,
StartTLS,
LDAPS,
}
impl std::fmt::Display for TlsMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::None => write!(f, "None"),
Self::StartTLS => write!(f, "StartTLS"),
Self::LDAPS => write!(f, "LDAPS"),
}
}
}
#[derive(Debug, Clone)]
pub enum LdapAuthMethod {
Simple {
bind_dn: String,
password: String,
},
#[cfg(feature = "ldap-sasl")]
SaslPlain {
identity: String,
password: String,
},
#[cfg(feature = "ldap-sasl")]
SaslExternal,
Anonymous,
}
impl std::fmt::Display for LdapAuthMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Simple { .. } => write!(f, "simple"),
#[cfg(feature = "ldap-sasl")]
Self::SaslPlain { .. } => write!(f, "sasl_plain"),
#[cfg(feature = "ldap-sasl")]
Self::SaslExternal => write!(f, "sasl_external"),
Self::Anonymous => write!(f, "anonymous"),
}
}
}
#[derive(Debug, Clone)]
pub struct LdapSpec {
pub uri: String,
pub auth: LdapAuthMethod,
pub tls_mode: TlsMode,
pub ca_certificate: Option<String>,
pub verify_certificate: bool,
pub connect_timeout: Duration,
pub operation_timeout: Duration,
pub page_size: Option<usize>,
}
impl LdapSpec {
pub fn new(uri: String, auth: LdapAuthMethod) -> Self {
Self {
uri,
auth,
tls_mode: TlsMode::None,
ca_certificate: None,
verify_certificate: false,
connect_timeout: Duration::from_secs(5),
operation_timeout: Duration::from_secs(30),
page_size: Some(500),
}
}
pub fn with_tls_mode(mut self, tls_mode: TlsMode) -> Self {
self.tls_mode = tls_mode;
self
}
pub fn with_ca_certificate(mut self, ca_cert: String) -> Self {
self.ca_certificate = Some(ca_cert);
self
}
pub fn with_verify_certificate(mut self, verify: bool) -> Self {
self.verify_certificate = verify;
self
}
pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = timeout;
self
}
pub fn with_operation_timeout(mut self, timeout: Duration) -> Self {
self.operation_timeout = timeout;
self
}
pub fn with_page_size(mut self, size: usize) -> Self {
self.page_size = Some(size);
self
}
}