Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AD as authentication for Django

I'm working on a Django-based application in a corporate environment and would like to use the existing Active Directory system for authentication of users (so they don't get yet another login/password combo). I would also like to continue to use Django's user authorization / permission system to manage user capabilities.

Does anyone have a good example of this?

like image 780
Technical Bard Avatar asked Jan 04 '09 03:01

Technical Bard


People also ask

How do I use LDAP authentication in Python?

In order to use LDAP with Python we need to import the Server and the Connection object, and any additional constant we will use in our LDAP. As you might remember from the LDAP Protocol diagram the authentication operation is called Bind.

Does Django have authentication?

Django provides an authentication and authorization ("permission") system, built on top of the session framework discussed in the previous tutorial, that allows you to verify user credentials and define what actions each user is allowed to perform.


4 Answers

Here's another more recent snippet (July 2008, updated Dec 2015):

Authentication Against Active Directory (LDAP) over SSL

like image 82
Jeff Bauer Avatar answered Sep 23 '22 00:09

Jeff Bauer


The link provided by Jeff indeed works though it assumes you have a you have a default group where users are added to. I simply replaced:

group=Group.objects.get(pk=1) 

by

group,created=Group.objects.get_or_create(name="everyone") 

If you want tighter integration & more features there is also django-auth-ldap which gives you you more control over how ldap users/group are mapped onto django users/groups.

For debugging the ldap connection I found this blog post useful, in particular the command for testing the ldap connection with ldap-utils:

ldapsearch -H ldaps://ldap-x.companygroup.local:636 -D "CN=Something LDAP,OU=Random Group,DC=companygroup,DC=local" -w "p4ssw0rd" -v -d 1 

If you are using ssl there is also the issue of getting hold of a certificate will play nice with. Either you extract it from the server, or you can follow these instructions to generate your own.

like image 43
dgorissen Avatar answered Sep 25 '22 00:09

dgorissen


How about that? Did you try that one?

http://www.djangosnippets.org/snippets/501/

like image 44
lpfavreau Avatar answered Sep 24 '22 00:09

lpfavreau


I had the same problem, and noticed that django-auth-ldap does not support SASL at all -> plain text passwords over the connection if TSL is not available.

Here is what i did for the problem: https://github.com/susundberg/django-auth-ldap-ad

like image 45
susundberg Avatar answered Sep 23 '22 00:09

susundberg