I am working on a django app and have to incorporate the LDAP authentication mechanism. Currently my files look like:
---settings.py
import ldap
from django_auth_ldap.config import LDAPSearch
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
AUTH_LDAP_SERVER_URI = 'ip_address'
AUTH_LDAP_BIND_DN = 'cn=admin,dc=******,dc=com'
AUTH_LDAP_BIND_PASSWORD = '*****'
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Users,dc=*****,dc=com",ldap.SCOPE_SUBTREE,"(uid = (%Users))" )
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sh",
"email": "mail"
}
import logging
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
----urls.py
urlpatterns = [
url(r'^info/$','django.contrib.auth.views.login',{'template_name': 'auth.html'}),
url(r'^info/login/$',login),]
----auth.html
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="login/" method="post">{%csrf_token%}
Email address: <input type="text" name="email"/>
Password: <input type="password" name="password" />
<input type="submit" value="Log in" />
</form>
</body>
</html>
---- views.py
def login(request):
user = authenticate( username= request.REQUEST.get('email'), password= request.REQUEST.get('password')) #email and password supplied through auth.html
if user is not None:
return getInfo(request)
else:
return render(request,'invalidUser.html')
Currently I am using it on localhost for the testing purpose but LDAP ip_address in AUTH_LDAP_SERVER_URI is public for the company(i have taken this from the administrator). When i try to login, the errors that are thrown with same settings are:
1. Caught LDAPError while authenticating anshul: LDAPError(0, 'Error')
or
2. Caught LDAPError while authenticating anshul: LDAPError(2, 'No such file or directory')
My question is:
1. Why the error and meaning of 1st error? is it because of that access is not there for me at LDAP?
2. What is the purpose of
import logging
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
In settings.py?
This is the first time i am working on django. Is it because of improper configuration or i cant access AUTH_LDAP_SERVER_URI through localhost and directly deploy on actual server with current settings?
LDAPError(0, 'Error')
, on this lineAUTH_LDAP_SERVER_URI = 'ip_address'
, you need to add'ldap://ip_address'
hence it should beAUTH_LDAP_SERVER_URI = 'ldap://ip_address'