pool.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. use ldap3::{Ldap, LdapConnAsync, LdapConnSettings, LdapError};
  2. use deadpool::managed::{self, Pool};
  3. #[derive(Clone)]
  4. pub struct LdapConfig {
  5. pub hostname: String,
  6. pub port: String,
  7. pub basedn: String,
  8. pub binddn: String,
  9. pub bindpw: String,
  10. pub starttls: bool,
  11. }
  12. pub struct LdapManager {config: LdapConfig}
  13. impl LdapManager {
  14. fn new(config: LdapConfig) -> LdapManager {
  15. LdapManager {config}
  16. }
  17. }
  18. impl managed::Manager for LdapManager {
  19. type Type = Ldap;
  20. type Error = LdapError;
  21. async fn create(&self) -> Result<Self::Type, Self::Error> {
  22. let ldap_settings = LdapConnSettings::new()
  23. .set_starttls(self.config.starttls);
  24. let ldap_url = format!("ldap://{}:{}", self.config.hostname, self.config.port);
  25. let (conn, ldap) = LdapConnAsync::with_settings(ldap_settings, &ldap_url)
  26. .await?;
  27. ldap3::drive!(conn);
  28. Ok(ldap)
  29. }
  30. async fn recycle(&self, conn: &mut Self::Type, _: &managed::Metrics) -> managed::RecycleResult<LdapError> {
  31. conn.simple_bind("", "").await?;
  32. Ok(())
  33. }
  34. }
  35. pub fn get_ldap_pool(config: LdapConfig) -> Pool<LdapManager> {
  36. let ldap_manager = LdapManager::new(config);
  37. let pool = Pool::builder(ldap_manager).max_size(16)
  38. .build()
  39. .unwrap();
  40. return pool;
  41. }