I have been researching on how to implement an authentication using python-ldap. Upon trying some codes and reading the documentation I've come up with the code below, upon testing, it always return "Invalid creds" when I input my password. But, it returns True whenever I leave my password blank.
import ldap, sys
secret = ''
un = 'xxx.x.xxxx'
server = "ldap://xxx.xxx.xxx.xxx:xxx"
base = "dc=xxx,dc=xxx"
def checkUser():
try:
l = ldap.initialize(server)
l.set_option(ldap.OPT_REFERRALS, 0)
l.protocol_version = 3
rex = l.simple_bind_s(un, secret)
print(rex)
return True
except ldap.INVALID_CREDENTIALS:
print("Invalid creds")
return False
except ldap.SERVER_DOWN:
print("Server down")
return False
except ldap.LDAPError as e:
print(f"Error {e}")
return False
finally:
l.unbind_s()
x = checkUser()
print(x)
Note: the account I am entering is just a normal user with no admin privileges.