djangoldapdjango-auth-ldap

How to use django_auth_ldap for django project?


The following is the snippet of the setting for our project.The problem is that it did not seem like it is connecting to the LDAP server. I added a couple print statement in the django_auth_ldap module and none of them is printed out when I run the server and try to log in.

Can any body help look at the setting and give some idea what went wrong?

settings.py

import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType
AUTH_LDAP_SERVER_URI = "ldap://ldap.corperate.com:port"
AUTH_LDAP_BIND_DN = "CN=Network Inventory Management,OU=Service Accounts,DC=corperate,DC=com"
AUTH_LDAP_BIND_PASSWORD = "PASSWORD"
AUTH_LDAP_CONNECTION_OPTIONS={
    ldap.OPT_DEBUG_LEVEL:1,
    ldap.OPT_REFERRALS:0,
}
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=accounts,dc=corperate,dc=com",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=groups,dc=corperate,dc=com", ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)")
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")


AUTH_LDAP_USER_ATTR_MAP = {
    "user_id": "employeeid",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail"
}
AUTH_LDAP_PROFILE_ATTR_MAP = {
    "location":"physicalDeliveryOfficeName",
    "employee_id":"employeeID",
    "phone":"telephoneNumber",
    "account_status":"userAccountControl",
    "employee_id":"employeeID",
    "distinguished_name":"distinguishedName",
}
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
"is_staff": "CN=Network Inventory Management Group,OU=django,OU=Groups,DC=corperate,DC=com",}
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'stream_to_console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django_auth_ldap': {
            'handlers': ['stream_to_console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

Solution

  • any error message ? Enabling logging might help. Put this in the settings file:

    import logging, logging.handlers
    logfile = "/tmp/django-ldap-debug.log"
    my_logger = logging.getLogger('django_auth_ldap')
    my_logger.setLevel(logging.DEBUG)
    handler = logging.handlers.RotatingFileHandler(
       logfile, maxBytes=1024 * 500, backupCount=5)
    my_logger.addHandler(handler)
    

    and

    tail -f /tmp/django-ldap-debug.log