Skip to content

Instantly share code, notes, and snippets.

@paul-schwendenman
Last active December 31, 2015 01:09
Show Gist options
  • Save paul-schwendenman/7911600 to your computer and use it in GitHub Desktop.
Save paul-schwendenman/7911600 to your computer and use it in GitHub Desktop.
LDAP for Django

Introduction

This document will include the author's entire notes on installing and configuring ldap for django.

Installation

  1. Create a directory in /opt:

    mkdir /opt/that
    
  2. Create a virtualenv in that directory:

    virtualenv example
    

Make sure that you also source the directory before running pip commands. i.e.:

source /opt/that/example/bin/activate
  1. Install django:

    pip install django
    
  2. Install python-ldap

Here you have two options. Either install with pip or install using the package manager.

  1. Installing with package-manager:

    sudo apt-get install python-ldap
    
  2. Install with pip

First install dependencies:

sudo apt-get install libldap2-dev
sudo apt-get install libsasl2-dev

Then you should be able to install with pip:

pip install python-ldap

You may also have to install python-dev, if you haven't already

Dependency sources:

  1. Install django-auth-ldap

Here again you have two options. Either install with pip of install using your package manager.

  1. Installing with package-manager:

    sudo apt-get install python-django-auth-ldap
    

You may also want the docs package:

sudo apt-get install python-django-auth-ldap-doc
  1. Install with pip:

    pip install django-auth-ldap
    

LDAP Debugging

If you are unhappy with the quantity of logs produced by the ldap module patch the file "venv0/lib/python2.7/site-packages/ldap/ldapobject.py," in my expierence logging before and after line 100 works magically.

For example changing:

result = func(*args,**kwargs)

To:

import logging
logger = logging.getLogger('django_auth_ldap')
logger.info("in  Func: %s Args: %s, Kw: %s" % (str(func), str(args), str(kwargs)))
result = func(*args,**kwargs)
logger.info("out Func: %s Result: %s" % (str(func), str(result)))

Please remember to see logs generated by the previous lines in django, you must also set up logging for "django_auth_ldap" in the settings file.

Active directory Error 49 Codes

Quick Guide to Error code 49 on AD systems

Data Message
525 user not found
52e invalid credentials
530 not permitted to logon at this time
531 not permitted to logon at this workstation
532 password expired
533 account disabled
701 account expired
773 user must reset password
775 user account locked
source:https://confluence.atlassian.com/display/CROWD/Active+Directory+LDAP+Errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment