[Django]-How can I integrate LDAP and AllAuth in a Django project?

4👍

There seems to be (still) no direct LDAP integration provided by allauth (no “ldap” is mentioned in neither the source code nor the documentation). Hence,

  1. LDAP needs to be integrated with another Python package (python-ldap or django-auth-ldap), first.
  2. User creation in allauth then needs to be customized via DefaultAccountAdapter (allauth docs) probably, depending on your needs and potential username conflicts. GitHub issue 199 explains some of the problematics.

Note that this solution applies to the case of a combined use of allauth and LDAP. If you want to use LDAP exclusively for authentication then keeping allauth in the codebase probably makes little sense, or may just be an overhead; you’ll have to tear it out.

1👍

ldap configuration is fairly simple in django. I use the following settings for my application. I also use a python library called python-ldap

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
)

AUTH_LDAP_SERVER_URI = "ldap://127.0.0.1:389"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=People,dc=local,dc=domain,dc=com",
                                   ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "sn",
}

Leave a comment