3

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?

1
  • In regards to LDAPError(0, 'Error'), on this line AUTH_LDAP_SERVER_URI = 'ip_address', you need to add 'ldap://ip_address' hence it should be AUTH_LDAP_SERVER_URI = 'ldap://ip_address'
    – Brian
    Commented Nov 5, 2020 at 19:36

1 Answer 1

1

AUTH_LDAP_SERVER_URI is a URI, not a bare IP address. For example, ldap://localhost/. If you're not sure about a value, open a Python shell and test it:

> import ldap
> conn = ldap.initialize('ldap://<host-or-ip>/')

Also, AUTH_LDAP_USER_SEARCH should contain %(user)s, not %(Users).

The logging configuration just hooks up the django_auth_ldap debug output to your console so that you'll see it. The default logging settings in Python/Django will just eat the output. These days, you can also set this up in Django's LOGGING setting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.