From 438ac2818a4a0508d93aa4cfed9bc3a273b2e9d3 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Thu, 28 Oct 2021 23:24:01 +0900 Subject: [PATCH] ldap: Add support for "dn" attribute --- server/src/infra/ldap_handler.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/server/src/infra/ldap_handler.rs b/server/src/infra/ldap_handler.rs index f9150c3..ebb1233 100644 --- a/server/src/infra/ldap_handler.rs +++ b/server/src/infra/ldap_handler.rs @@ -88,7 +88,7 @@ fn get_user_id_from_distinguished_name( } } -fn get_user_attribute(user: &User, attribute: &str) -> Result> { +fn get_user_attribute(user: &User, attribute: &str, dn: &str) -> Result> { match attribute { "objectClass" => Ok(vec![ "inetOrgPerson".to_string(), @@ -96,6 +96,7 @@ fn get_user_attribute(user: &User, attribute: &str) -> Result> { "mailAccount".to_string(), "person".to_string(), ]), + "dn" => Ok(vec![dn.to_string()]), "uid" => Ok(vec![user.user_id.clone()]), "mail" => Ok(vec![user.email.clone()]), "givenName" => Ok(vec![user.first_name.clone()]), @@ -112,14 +113,15 @@ fn make_ldap_search_user_result_entry( base_dn_str: &str, attributes: &[String], ) -> Result { + let dn = format!("cn={},ou=people,{}", user.user_id, base_dn_str); Ok(LdapSearchResultEntry { - dn: format!("cn={},ou=people,{}", user.user_id, base_dn_str), + dn: dn.clone(), attributes: attributes .iter() .map(|a| { Ok(LdapPartialAttribute { atype: a.to_string(), - vals: get_user_attribute(&user, a)?, + vals: get_user_attribute(&user, a, &dn)?, }) }) .collect::>>()?, @@ -707,6 +709,7 @@ mod tests { filter: LdapFilter::And(vec![]), attrs: vec![ "objectClass".to_string(), + "dn".to_string(), "uid".to_string(), "mail".to_string(), "givenName".to_string(), @@ -729,6 +732,10 @@ mod tests { "person".to_string() ] }, + LdapPartialAttribute { + atype: "dn".to_string(), + vals: vec!["cn=bob_1,ou=people,dc=example,dc=com".to_string()] + }, LdapPartialAttribute { atype: "uid".to_string(), vals: vec!["bob_1".to_string()] @@ -763,6 +770,10 @@ mod tests { "person".to_string() ] }, + LdapPartialAttribute { + atype: "dn".to_string(), + vals: vec!["cn=jim,ou=people,dc=example,dc=com".to_string()] + }, LdapPartialAttribute { atype: "uid".to_string(), vals: vec!["jim".to_string()]