inejge / ldap3

A pure-Rust LDAP library using the Tokio stack
Apache License 2.0
220 stars 38 forks source link

Suggestion: Compile-time safe queries using serde #119

Closed 2ndDerivative closed 7 months ago

2ndDerivative commented 8 months ago

Here's an idea to make search queries more manageable regarding escaping and such: You could probably make serde translate them directly into the relevant escaped structures. A query syntax supporting something like

#[derive(Serialize)]
struct Query {
    objectClass: String,
    sn: String,
    givenName: String
}

fn main() {
    let mut ldap = LdapConn::new(ADDRESS).unwrap();
    ldap.simple_bind(USERNAME, PASSWORD).unwrap();
    let (_, _) = ldap
        .search(
            "dc=myhost,dc=com",
            Scope::Subtree,
            &Query { objectClass: String::from("user"), sn: String::from("Foo"), givenName: String::from("Bar"),
            [ATTRIBUTE]
        ).unwrap().success().unwrap();
}

Similarly applicable for SearchEntry results that could deserialize from the attributes in [ATTRIBUTE].

I'll look into a solution like this, as this is just a faint idea, but I'm preliminarily putting this out there.

inejge commented 7 months ago

Interesting, but filter building is not where I see the best use of (de)serialization -- that would be constructing data structs from result entries, and serde is not a good match for ASN.1 anyway.

Safe value substitution would need a filter templating system, which would necessarily be a custom creation because there is no standard I'm aware of. I don't think I'll expend effort in that direction, so I'll close this issue, but it can be reopened once you have your idea fleshed out.