Izik
Izik

Reputation: 958

python ldap3 authentication sometimes succeed and sometimes fails

I'm trying to verify a user with ldap3 with the next Python code, sometimes it goes to the 'Authentication successful' message, and sometimes it fails and goes to the 'LDAP search failed' with the exception: 'LDAPConfidentialityRequiredResult - 13 - confidentialityRequired - None - None - bindResponse - None'.

I cannot figure out why, even when running the code 10 times in the same minute I get different results, it seems very random. I couldn't find a good explanation of how to solve this error. if the server needs a certificate how come sometimes it does work?

tls_configuration = Tls(validate=ssl.CERT_NONE, version=ssl.PROTOCOL_TLSv1)
tls_configuration.validate = ssl.CERT_NONE
server = Server(ldap_server, port=ldap_port, get_info=ALL, tls=tls_configuration)
try:
    conn = Connection(server, user=ldap_user, password=ldap_password, check_names=True, lazy=False, raise_exceptions=True)
    conn.open()
except Exception as e:
    print('Connection to LDAP failed: ' + str(e))
    return
try:
    conn.search(search_base=search_base, search_filter=f'(uid={username})', attributes=['*'])
    if conn.entries:
        user_entry = conn.entries[0]
        user_dn = conn.entries[0].entry_dn
        try:
            conn = Connection(server, user=user_dn, password=user_password, authentication='SIMPLE', raise_exceptions=True)
            conn.open()
        except Exception as e:
            print('connecting user failed: ' + str(e))
            return
        if conn.bind():
            print('Authentication successful')
            conn.unbind()
        else:
            print('Authentication failed')
    else:
        print('no user is found')
except Exception as e:
    print('LDAP search failed: ' + str(e))
    return

Thank you for any help

Upvotes: 0

Views: 471

Answers (1)

Izik
Izik

Reputation: 958

For anyone viewing this in the future - this is a known issue caused by compatibility breaks starting from Python 3.10 and above when used with OpenSSL version 3. While I couldn't find a straightforward solution, there are 2 different ways to avoid this issue:

  1. using Python version 3.9 and below.
  2. using OpenSSL version 1.

Upvotes: 1

Related Questions