Here you can see a full example very well guided showing how to create a custom LDAPBackend
.
You need to configure your LDAP settings in settings.py
(as shown in the link you posted) and add your LDAPBackend
to AUTHENTICATION_BACKENDS
. You can use the default LDAPBackend provided or create a custom one and use that.
Using the default LDAPBackend
provided by django-auth-ldap
:
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
Using a custom LDAPBackend
if you need to add extra logic to the authentication:
AUTHENTICATION_BACKENDS = (
'accounts.backends.MyLDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
Then in accounts/backends.py
:
from django_auth_ldap.backend import LDAPBackend
class MyLDAPBackend(LDAPBackend):
""" A custom LDAP authentication backend """
def authenticate(self, username, password):
""" Overrides LDAPBackend.authenticate to add custom logic """
user = LDAPBackend().authenticate(self, username, password)
""" Add custom logic here """
return user
Check the example linked above for more details.
If you are new to LDAP I would recommend to take a look at this answer (and the other one as well) in another question regarding this topic.
UPDATE FOR NEWER VERSIONS OF django-auth-ldap
Thanks @wolf2600 for pointing out that now instead of authenticate
you'll need to override authenticate_ldap_user
instead.