| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 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<Self::Type, Self::Error> {
- 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<LdapError> {
- conn.simple_bind("", "").await?;
- Ok(())
- }
- }
- pub fn get_ldap_pool(config: LdapConfig) -> Pool<LdapManager> {
- let ldap_manager = LdapManager::new(config);
- let pool = Pool::builder(ldap_manager).max_size(16)
- .build()
- .unwrap();
- return pool;
- }
|