[Python] What's the best way to write an LDAP client in Python

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I don't know LDAP but I need to develop an application (essentially a LDAP client) for authentication on an LDAP Server and perform a query/search i the directory managed by the LDAP Server. My first problem is to try authentication on the LDAP server available in the company where I work.

    Use ldapsearch to test authentication


    I know the program ldapsearch. This link explains that is it possible to use it for LDAP authentication. So ldapsearch can be used to test the authentication on an LDAP server

    One way to use of ldapsearch by command line is:

    ldapsearch -H ldap://ldap.example.com -D "cn=admin,dc=example,dc=com" -W -b "cn=admin,dc=example,dc=com"


    Previous command uses the following options:

    • -H ldapURI: Specify URI referring to the LDAP server
    • -D bindDN: Use the Distinguished Name bindDN to bind to the LDAP directory
    • -W: Prompt for simple authentication
    • -b searchbase: Use searchbase as the starting point for the search instead of the default.

    The command execution queries the LDAP server ldap.example.com and asks the insertion of the password for the user with Common Name admin (cn=admin). If the password of admin is not correct the result is the error message:

    ldap_bind: Invalid credentials (49)


    If the inserted password is correct the output contains the following 0 Success message:

    ...
    # search result
    search: 2
    result: 0 Success
    ...

    Integrate ldapsearch inside an application?


    By ldapsearch I have tested with success the LDAP authentication on my company LDAP Server. Now I have a question: is ldapsearch suited for direct used inside a LDAP client? And in this case:

    • Is it correct to use the different ldapsearch output (described above) to know if a user has inserted the correct password?
    • The LDAP client have to call the external program ldapsearch?
    How to write a client to direct authenticate on a LDAP server?


    For example in this post ldapsearch is used in a shell script to verified which users of a list are present on a LDAP Server but I suspect that it isn't suited for direct use inside a program write in Python or C.

    If I exclude the use of ldapsearch I have to direct implement the LDAP authentication in a Client application (may be in Python language).

    Without ldapsearch how can I implement the protocol LDAP in Python or in other language?

    Thank you

    Continue reading...

Compartilhe esta Página