use ldap3::{Ldap, LdapConnAsync, LdapConnSettings, LdapError}; use deadpool::managed::{self, Pool}; #[derive(Clone)] pub struct LdapConfig { pub hostname: String, pub port: String, pub basedn: String, pub binddn: String, pub bindpw: String, pub starttls: bool, } pub struct LdapManager {config: LdapConfig} impl LdapManager { fn new(config: LdapConfig) -> LdapManager { LdapManager {config} } } impl managed::Manager for LdapManager { type Type = Ldap; type Error = LdapError; async fn create(&self) -> Result { let ldap_settings = LdapConnSettings::new() .set_starttls(self.config.starttls); let ldap_url = format!("ldap://{}:{}", self.config.hostname, self.config.port); let (conn, ldap) = LdapConnAsync::with_settings(ldap_settings, &ldap_url) .await?; ldap3::drive!(conn); Ok(ldap) } async fn recycle(&self, conn: &mut Self::Type, _: &managed::Metrics) -> managed::RecycleResult { conn.simple_bind("", "").await?; Ok(()) } } pub fn get_ldap_pool(config: LdapConfig) -> Pool { let ldap_manager = LdapManager::new(config); let pool = Pool::builder(ldap_manager).max_size(16) .build() .unwrap(); return pool; }