<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>johnsonroad.net &#187; Django</title>
	<atom:link href="http://johnsonroad.net/blog/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://johnsonroad.net/blog</link>
	<description>Quidquid latine dictum sit, altum sonatur. Quod erat demonstrandum.</description>
	<lastBuildDate>Sat, 25 Jul 2009 23:55:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>How to allow Django to authenticate against Active Directory</title>
		<link>http://johnsonroad.net/blog/2007/10/19/how-to-allow-django-to-authenticate-against-active-directory/</link>
		<comments>http://johnsonroad.net/blog/2007/10/19/how-to-allow-django-to-authenticate-against-active-directory/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 21:13:57 +0000</pubDate>
		<dc:creator>Jordan</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://johnsonroad.net/blog/2007/10/19/how-to-allow-django-to-authenticate-against-active-directory/</guid>
		<description><![CDATA[I used PyWin32 on a Django 0.96 instance after struggling and giving up on using python-ldap. This doesn&#8217;t query any of the AD user object&#8217;s attributes; it just lets them log in to the Django site and creates a Django user object for them. Thanks to Benji York for posting the code that talks to [...]]]></description>
			<content:encoded><![CDATA[<p>I used <a href="https://sourceforge.net/projects/pywin32/">PyWin32</a> on a <a href="http://www.djangoproject.com/">Django</a> 0.96 instance after struggling and giving up on using python-ldap.  This doesn&#8217;t query any of the AD user object&#8217;s attributes; it just lets them log in to the Django site and creates a Django user object for them.</p>
<p>Thanks to <a href="http://www.benjiyork.com/">Benji York</a> for posting the code that talks to <a href="http://mail.python.org/pipermail/python-list/2006-May/381575.html">Active Directory using PyWin32</a> to the python mailing list.</p>
<p><code>from django.conf import settings<br />
from django.contrib.auth.models import User, check_password<br />
from win32security import LogonUser, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT<br />
import pywintypes<br />
class ADBackend:<br />
	"""<br />
	Authenticates against Active Directory.<br />
	"""<br />
	def attempt_ad_login(self, username, password):<br />
		try:<br />
			handle=LogonUser(username, None, password,<br />
						     LOGON32_LOGON_NETWORK,<br />
							 LOGON32_PROVIDER_DEFAULT)<br />
			# We're not going to use the handle, just seeing if we can get it.<br />
			handle.Close()<br />
			return True<br />
		except pywintypes.error, e:<br />
			# Because of the sheer number of Windows-specific errors that can<br />
			# occur here, we have to assume any of them mean that the<br />
			# credentials were not valid.<br />
			return False<br />
def authenticate(self, username=None, password=None):<br />
		if self.attempt_ad_login(username, password):<br />
			try:<br />
				user = User.objects.get(username=username)<br />
			except User.DoesNotExist:<br />
				# Create a new user.  This password will not be checked during login, so it doesn't matter.<br />
				user = User(username=username, password='dummy password')<br />
				# Comment or uncomment these as appropriate.<br />
				user.is_staff = True<br />
				#user.is_superuser = True<br />
				user.save()<br />
			return user<br />
		else:<br />
			return None<br />
def get_user(self, user_id):<br />
		try:<br />
			return User.objects.get(pk=user_id)<br />
		except User.DoesNotExist:<br />
			return None<br />
</code></p>
<p>Save this to a file such as &#8220;ADBackend.py&#8221; in the same path as your application, then add the following to your settings.py, replacing <strong>mysite </strong>and <strong>myapp</strong> to match your Django site and application:</p>
<p><code>AUTHENTICATION_BACKENDS = (<br />
	'django.contrib.auth.backends.ModelBackend',<br />
	'mysite.myapp.ADBackend.ADBackend'<br />
)<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://johnsonroad.net/blog/2007/10/19/how-to-allow-django-to-authenticate-against-active-directory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

