A Julia client for LDAP (Lightweight Directory Access Protocol) based on OpenLDAP library.
pkg> add LDAPClient
This implements the use-case of checking if a username and password are valid or not.
r
will be either an AuthOk
for successful authentication, or AuthErr
when the
password is wrong or any other error happened.
r = LDAPClient.authenticate("ldap://ldap.server.net", "my-username", "my-password")
Usually the user needs to bind to a LDAP connection before running queries. The following example shows how to create a connection, bind to it, and unbind when you're finished with it.
conn = LDAPClient.LDAPConnection("ldap://ldap.server.net") # this will not connect to the server yet
LDAPClient.simple_bind(conn, "my-username", "my-password") # here we actually get to connect to the server
# do stuff
LDAPClient.unbind(conn)
Use LDAPClient.search
to perform queries on your LDAP server.
search(ldap::LDAPConnection, base::AbstractString, scope::LDAPScope;
filter::Union{Nothing, AbstractString}=nothing,
attr_desc_only::Bool=false,
size_limit::Integer=-1) :: MessageChain
scope
can be one of these values: LDAP_SCOPE_BASE
, LDAP_SCOPE_ONELEVEL
, LDAP_SCOPE_SUBTREE
, LDAP_SCOPE_CHILDREN
.
The following example queries for users, filtering only results that match users named USER1
or USER2
.
search_string = "CN=Users,DC=server,DC=net" # will query Users on domain server.net
scope = LDAPClient.LDAP_SCOPE_ONELEVEL
chain = LDAPClient.search(conn, search_string, scope, filter="(|(name=USER1)(name=USER2))")
The chain
output is a collection of messages.
Each message can be an Entry
, a Reference
or a Result
.
We can count how many messages of each kind we have with count_messages(chain)
, count_entries(chain)
or count_references(chain)
.
We can iterate messages of each kind with each_message(chain)
, each_entry(chain)
, each_reference(chain)
.
For Entry
messages, we can inspect its attributes. The following shows a complete example.
conn = LDAPClient.LDAPConnection("ldap://ldap.server.net")
LDAPClient.simple_bind(conn, "my-username", "my-password")
search_string = "CN=Users,DC=server,DC=net" # will query Users on domain server.net
scope = LDAPClient.LDAP_SCOPE_ONELEVEL
chain = LDAPClient.search(conn, search_string, scope, filter="(|(name=USER1)(name=USER2))")
for entry in LDAPClient.each_entry(chain)
println("Reading attributes from user $(entry["name"])")
for attr in LDAPClient.each_attribute(entry)
println(attr)
end
end
LDAPClient.unbind(conn)
This example outputs something like this.
Reading attributes from user ["USER1"]
LDAPClient.Attribute("objectClass", ["top", "person", "organizationalPerson", "user"])
LDAPClient.Attribute("cn", ["USER1"])
LDAPClient.Attribute("title", ["Manager"])