I have seen almost every question and tutorial about this topic and i still cannot connect to my ldap with the django. Here are my settings.py and views.py below this. I really need to solve this, if anybody could help me I would really appreciate it, just somebody tell me what am I doing wrong because i cannot figure it out.
settings.py
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend'
'django.contrib.auth.backends.ModelBackend',
)
main_dn = 'dc=fx3x,dc=com'
groups_dn = 'ou=Groups,' + main_dn
users_dn = 'ou=Users,' + main_dn
AUTH_LDAP_SERVER_URI = 'ldap://ldap.xxxmk.com'
#AUTH_LDAP_BIND_DN = 'dc=fx3x,dc=com'
#AUTH_LDAP_BIND_PASSWORD = ""
AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=Users,dc=fx3x,dc=com"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Users,dc=fx3x,dc=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=Groups,dc=fx3x,dc=com", ldap.SCOPE_SUBTREE, "(objectClass=posixGroup)")
AUTH_LDAP_CONNECTION_OPTIONS = {
ldap.OPT_REFERRALS: 0
}
AUTH_LDAP_USER_ATTR_MAP = {
"full_name": "sn",
"username": "uid",
"password": "userPassword"
}
AUTH_LDAP_MIRROR_GROUPS = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 2
views.py
from django.contrib.auth import authenticate, login, logout
import ldap
from django_auth_ldap.backend import LDAPBackend
def log_in_form_event(request):
response = {'success': False}
if request.method == "POST":
try:
ldap_username = request.POST["name"]
ldap_password = request.POST["password"]
#l_username = django_auth_ldap.django_to_ldap_username(ldap_username)
#ip_server = settings.LDAP_BASES.get('ip')
#userdn = settings.LDAP_BASES.get('users')
ldap_connection = ldap.initialize('ldap://ldap.xxxmk.com')
ldap_connection.simple_bind_s(username=ldap_username, password=ldap_password)
auth = LDAPBackend()
#user = ldap_connection.
user = auth.authenticate(username=ldap_username, password=ldap_password)
res = ldap_connection.search_ext_s(settings.AUTH_LDAP_USER_SEARCH, ldap.SCOPE_SUBTREE, "uid=%s" % ldap_username)
login(request, user)
response = {'success': True, 'note': "logged in"}
except:
response = {'success': False, 'note': "not logged in"}
return HttpResponse(simplejson.dumps(response), mimetype='application/json')