Skip to content

Instantly share code, notes, and snippets.

@hanleybrand
Last active August 29, 2015 14:18
Show Gist options
  • Save hanleybrand/f6df2212987829ec0d44 to your computer and use it in GitHub Desktop.
Save hanleybrand/f6df2212987829ec0d44 to your computer and use it in GitHub Desktop.
python-ldap with bind user for django
import ldap
from django.conf import settings
ldap_auth = settings.LDAP_AUTH[0]
ldap_uri = ldap_auth['uri']
l_user = ldap_auth['bind_user']
l_pass = ldap_auth['bind_password']
searchScope = ldap_auth['scope']
base = ldap_auth['base']
def get_dn(uname, dn=None):
base = ldap_auth['base']
l = ldap.initialize(ldap_uri)
bind_user = ldap_auth['bind_user']
bind_pass = ldap_auth['bind_password']
l.simple_bind(bind_user, bind_pass)
try:
uid = ldap_auth['dn']
nic = l.search_s(base, 1, 'uid=%s' % uname, [uid])[0][1].get(ldap_auth.get('dn', 'dn'))[0]
dn = '%s=%s,%s' % (uid, nic, 'ou=people,dc=temple,dc=edu')
except ldap.LDAPERROR as e:
print(e)
return dn
def user_bind(username, password):
l = ldap.initialize(ldap_uri)
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS, 0)
l_response = l.simple_bind(get_dn(username), password)
bind_result = l.result(l_response)
if bind_result[0] == 97:
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment