0

I am using Django 1.5 and Python 3. I am new to Django & LDAP in general.

I am including the following configuration (modified for my case) in my settings.py file, which was taken from here:

AUTH_LDAP_SERVER_URI = "ldap://example.fr"

AUTH_LDAP_BIND_DN = 'cn=a_user,dc=example,dc=fr'
AUTH_LDAP_BIND_PASSWORD=''
AUTH_LDAP_USER_SEARCH = LDAPSearch('ou=users,dc=example,dc=fr', ldap.SCOPE_SUBTREE, '(uid=%(user)s)')
AUTH_LDAP_GROUP_SEARCH = LDAPSearch('ou=groups,dc=example,dc=fr', ldap.SCOPE_SUBTREE, '(objectClass=groupOfNames)')

AUTH_LDAP_GROUP_TYPE = ActiveDirectoryGroupType()

#Populate the Django user from the LDAP directory
AUTH_LDAP_USER_ATTR_MAP = {
    'first_name': 'sAMAccountName',
    'last_name': 'displayName',
    'email': 'mail'
}


AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

However, I have also followed the discussion here where it says that, the django-auth-ldap module is not ported to Python 3.

I have a couple of questions:

  1. What are the module imports that need to go into the settings.py file in order to get the configuration above to work?

  2. Which of those imports are not available for Python 3?

  3. Is it possible to get LDAP-based authentication going for Django with Python 3? Is it some modification of the approach above, or something different?

Any advice appreciated.

Community
  • 1
  • 1
tchakravarty
  • 10,736
  • 12
  • 72
  • 116

2 Answers2

4

A late follow up, but a better option for Python 3 may now be available. I have started using this Django app for my needs:

https://github.com/etianen/django-python3-ldap

It runs great with Python 3 and the package ldap3. I've only tested against OpenLDAP so far.

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
2

It looks like you got your answer through the linked discussion, but I'll post it here to help anyone who comes across this question.

python-ldap does not support Python3 yet, but there is a fork which does:

pip install git+https://github.com/rbarrois/python-ldap.git@py3

It looks like the changes have been accepted upstream, but it was easier to follow the instructions at django-auth-ldap to install that fork than to figure out what version at pypi would have the changes.

You also need to install django-auth-ldap:

pip install django-auth-ldap

At this point, most of your code should work with two imports:

import ldap
from django_auth_ldap.config import LDAPSearch

You will probably also need to import the ActiveDirectoryGroupType from somewhere, but I haven't ever used that.

Based on the discussion, python-ldap is an old and fragile code base. I have been very happy with python3-ldap, and hope that django-auth-ldap will move to that module in the future.

http://pythonhosted.org/python3-ldap/

resplin
  • 436
  • 5
  • 12
  • Thanks, I actually had difficulty following the linked discussion (and abandoned it). I will try your suggested steps and report back. – tchakravarty May 21 '14 at 04:15