A ldap client library that wraps ldap3 to make it easy to use.
Adding simple_ldap
as a dependency to your project:
cargo add simple-ldap
Other useful pieces you'll likely need:
cargo add url serde --features serde/derive
By default, this library enables the tls
feature, which is an alias for tls-native
. If you want to use tls-rustls
, you have to disable the default feature by using default-features = false
in your Cargo.toml
file, as it conflicts with tls-native
. Then, enable the feature you want to use.
There are plenty more examples in the documentation!
use simple_ldap::{
LdapClient, LdapConfig,
filter::EqFilter,
ldap3::Scope
};
use url::Url;
use serde::Deserialize;
// A type for deserializing the search result into.
#[derive(Debug, Deserialize)]
struct User {
uid: String,
cn: String,
sn: String,
}
#[tokio::main]
async fn main(){
let ldap_config = LdapConfig {
bind_dn: String::from("cn=manager"),
bind_password: String::from("password"),
ldap_url: Url::parse("ldaps://localhost:1389/dc=example,dc=com").unwrap(),
dn_attribute: None,
connection_settings: None
};
let mut client = LdapClient::new(ldap_config).await.unwrap();
let name_filter = EqFilter::from("cn".to_string(), "Sam".to_string());
let user: User = client
.search::<User>(
"ou=people,dc=example,dc=com",
Scope::OneLevel,
&name_filter,
&vec!["cn", "sn", "uid"],
).await.unwrap();
}