Skip to content

Instantly share code, notes, and snippets.

@yannbriancon

yannbriancon/ldap.py

Created Jun 10, 2019
Embed
What would you like to do?
Check if a user can authenticate in a LDAP and get his information
from ldap3 import Server, Connection, ALL
LDAP_URL = 'ldap.forumsys.com'
# Check user authentication in the LDAP and return his information
def get_LDAP_user(username, password):
try:
server = Server(LDAP_URL, get_info=ALL)
connection = Connection(server,
'uid={username},dc=example,dc=com'.format(
username=username),
password, auto_bind=True)
connection.search('dc=example,dc=com', '({attr}={login})'.format(
attr='uid', login=username), attributes=['cn'])
if len(connection.response) == 0:
return None
return connection.response[0]
except:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.