Skip to content

Instantly share code, notes, and snippets.

@xdays
Created May 14, 2014 02:07
Show Gist options
  • Save xdays/0bc4231ba516cac55442 to your computer and use it in GitHub Desktop.
Save xdays/0bc4231ba516cac55442 to your computer and use it in GitHub Desktop.
python script for ldap authentication
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ldap
if __name__ == '__main__':
ldap_server='example.com'
base_dn = 'ou=user,dc=example,dc=com'
username = 'foo'
password = 'bar'
# the following is the user_dn format provided by the ldap server
user_dn = 'uid=' + username + ',' + base_dn
print user_dn
# adjust this to your base dn for searching
connect = ldap.open(ldap_server)
search_filter = 'uid=foo'
try:
#if authentication successful, get the full user data
connect.bind_s(user_dn,password)
result = connect.search_s(base_dn,ldap.SCOPE_SUBTREE,search_filter)
# return all user data results
connect.unbind_s()
print "Sucess:", result
except ldap.LDAPError as e:
connect.unbind_s()
print 'authentication error'
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment