<?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>GrenadePod &#187; openid</title>
	<atom:link href="http://www.grenadepod.com/tag/openid/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.grenadepod.com</link>
	<description>Dispersing the Seeds</description>
	<lastBuildDate>Mon, 22 Feb 2010 20:30:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Using OpenID for authentication in Django</title>
		<link>http://www.grenadepod.com/2009/11/22/using-openid-for-authentication-in-django/</link>
		<comments>http://www.grenadepod.com/2009/11/22/using-openid-for-authentication-in-django/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 18:03:51 +0000</pubDate>
		<dc:creator>pulegium</dc:creator>
				<category><![CDATA[IT Technology]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[openid]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.grenadepod.com/?p=446</guid>
		<description><![CDATA[Tired of memorising login details for million different websites? Wanted to download or check somethning, but been prompted to created a user account on some random website? We&#8217;ve all been in this situation. And for me personally, I would steer away from the site that forces me to register just to see some part of [...]


Related posts:<ol><li><a href='http://www.grenadepod.com/2009/12/13/python-web-crawler/' rel='bookmark' title='Permanent Link: Python web crawler'>Python web crawler</a></li>
<li><a href='http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/' rel='bookmark' title='Permanent Link: How to use generic views in Django'>How to use generic views in Django</a></li>
<li><a href='http://www.grenadepod.com/2009/12/03/building-and-running-google-chrome-os-on-virtualbox/' rel='bookmark' title='Permanent Link: Building and running Google Chrome OS on VirtualBox'>Building and running Google Chrome OS on VirtualBox</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p id="top" />Tired of memorising login details for million different websites? Wanted to download or check somethning, but been prompted to created a user account on some random website?</p>
<p>We&#8217;ve all been in this situation. And for me personally, I would steer away from the site that forces me to register just to see some part of forum or similar. Mostly this is because I don&#8217;t want to provide my details and follow lengthy registration process.</p>
<p>I don&#8217;t mind to have an account (even though I forget about the website next day), but the whole process of typing in all the stuff, deciphering captcha images, logging in to my email account just to confirm registration, etc, etc. Why don&#8217;t they use OpenId? These days if you have Yahoo or Gmail account (and just who doesn&#8217;t?) you can use them to login to all websites that support OpenId.</p>
<p>In a nutshell, OpenId is a decentralised authentication standard. User (U) who has an OpenId account with a trusted company (C) wants to login to a website (W). W prompts with a login screen. U enters his/her OpenId URL (no personal information here provided at all). W then redirects to C login screen, where U is prompted to allow access to some parts of the information (usually it&#8217;s username only, sometimes email). If U is happy to provide this information, he then clicks login button and information is passed back to W. In this scenario, W will never get any personal information about U without explicit permission. And U benefits from having only one login that works for all websites.</p>
<p>Below is a simple diagram that shows how authentication is done when using OpenId to login to the websites:</p>
<div id="attachment_476" class="wp-caption aligncenter" style="width: 476px"><img class="size-full wp-image-476" title="OID identification" src="http://www.grenadepod.com/wp-content/uploads/2009/11/OID-identification.jpg" alt="Authentication with OpenID flow" width="466" height="317" /><p class="wp-caption-text">Authentication with OpenID flow</p></div>
<p>Unfortunately Django does not have native support for OpenId, and their website recommends using <a href="http://bitbucket.org/benoitc/django-authopenid/wiki/Home" target="_blank">django-authopenid</a> library to perform authentication and login using OpenId.</p>
<h3>Install required libraries</h3>
<p>You need to install <a href="http://openidenabled.com/python-openid/" target="_blank">python-openid</a> library first. Installation is a breeze, however there seems to be some issues with internal version number in the package, but let&#8217;s assume it&#8217;s all fine:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># easy_install python-openid
Searching for python-openid
Reading http://pypi.python.org/simple/python-openid/
Reading http://openidenabled.com/python-openid/
Reading http://www.openidenabled.com/openid/libraries/python/
Best match: python-openid 2.2.4
Downloading http://openidenabled.com/files/python-openid/packages/python-openid-2.2.4.zip
Processing python-openid-2.2.4.zip
Running python-openid-2.2.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-Xyxx61/python-openid-2.2.4/egg-dist-tmp-oexXVf
zip_safe flag not set; analyzing archive contents...
Adding python-openid 2.2.4 to easy-install.pth file
&nbsp;
Installed /usr/lib/python2.5/site-packages/python_openid-2.2.4-py2.5.egg
Processing dependencies for python-openid
Finished processing dependencies for python-openid
# python
Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:43)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&amp;gt;&amp;gt;&amp;gt; openid.__version__
'2.2.1'
&amp;gt;&amp;gt;&amp;gt; openid.__path__
['/usr/lib/python2.5/site-packages/python_openid-2.2.4-py2.5.egg/openid']
&amp;gt;&amp;gt;&amp;gt; openid.version_info
(2, 2, 1)
&amp;gt;&amp;gt;&amp;gt;</pre></div></div>

<p>Then install django-authopenid. Installation seems to spit out some errors, but I guess these can be ignored as they don&#8217;t seem to cause any issues:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># easy_install django-authopenid
Searching for django-authopenid
Reading http://pypi.python.org/simple/django-authopenid/
Reading http://hg.e-engura.org/django-authopenid/
Reading http://code.google.com/p/django-authopenid/
Best match: django-authopenid 1.0.1
Downloading http://pypi.python.org/packages/source/d/django-authopenid/django-authopenid-1.0.1.tar.gz#md5=93d44b4ce40de55bed36c9ed292adb49
Processing django-authopenid-1.0.1.tar.gz
Running django-authopenid-1.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-UtVC7d/django-authopenid-1.0.1/egg-dist-tmp-oWjnYf
warning: no files found matching 'CHANGES.MD'
Sorry: TypeError: ('compile() expected string without null bytes',)
...
Adding django-authopenid 1.0.1 to easy-install.pth file
&nbsp;
Installed /usr/lib/python2.5/site-packages/django_authopenid-1.0.1-py2.5.egg
Processing dependencies for django-authopenid
Finished processing dependencies for django-authopenid</pre></div></div>

<h3>Prepare Django application environment</h3>
<p>There are several things you&#8217;d need to do:</p>
<ol>
<li>Create directory for application files</li>
<p>I will use /var/app/application/ in this example</p>
<li>Create directory for static content such as JS, CSS, HTML and images</li>
<p>I will use /var/www/application/ in this example</p>
<li>If you are using Apache web server add Virtual host definition and specify where web root is and when to pass control to mod_python module (note that all requests that do not start with /static/ will be passed to Django):</li>
<pre>    ServerName test.example.com
    DocumentRoot /var/www/application/
    ErrorLog /var/log/apache2/application-error.log
    CustomLog /var/log/apache2/application-access.log combined
    SetHandler mod_python
    PythonHandler django.core.handlers.modpython
    PythonPath sys.path+['/var/app/']
    SetEnv DJANGO_SETTINGS_MODULE application.settings
    SetEnv PYTHON_EGG_CACHE /tmp
    &lt;Location "/static/"&gt;
        SetHandler None
    &lt;/Location&gt;</pre>
<li>Also create database and configure appropriately in settings.py.</li>
<li>Enable administration application for Django</li>
</ol>
<h3 style="font-size: 1.17em;">Enable OpenID support</h3>
<p>These were the basic steps to get your Django environment set-up, now let&#8217;s enable OpenID module:</p>
<ol>
<li>In settings.py add OpenID middleware, application and also tell Django where to find templates</li>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">MIDDLEWARE_CLASSES = <span style="color: black;">&#40;</span>
    ...
    <span style="color: #483d8b;">'django_authopenid.middleware.OpenIDMiddleware'</span>,
<span style="color: black;">&#41;</span>
INSTALLED_APPS = <span style="color: black;">&#40;</span>
    ...
    <span style="color: #483d8b;">'django_authopenid'</span>,
    <span style="color: #483d8b;">'registration'</span>,
<span style="color: black;">&#41;</span>
LOGIN_URL = <span style="color: #483d8b;">&quot;/account/signin/&quot;</span>
TEMPLATE_DIRS = <span style="color: black;">&#40;</span>
    ...
    <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'templates'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#41;</span></pre></div></div>

<li>In urls.py just include urls from django_authopenid package:</li>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">urlpatterns = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
    ...
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^account/'</span>, include<span style="color: black;">&#40;</span><span style="color: #483d8b;">'django_authopenid.urls'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span></pre></div></div>

<li>Add the following context processors list (majority is what&#8217;s loaded by default anyway, you just need to add one line, but since it&#8217;s a tuple, you cannot modify it, so you must redefine it):

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">TEMPLATE_CONTEXT_PROCESSORS = <span style="color: black;">&#40;</span>
   <span style="color: #483d8b;">'django.core.context_processors.auth'</span>,
   <span style="color: #483d8b;">'django.core.context_processors.debug'</span>,
   <span style="color: #483d8b;">'django.core.context_processors.i18n'</span>,
   <span style="color: #483d8b;">'django.core.context_processors.media'</span>,
   <span style="color: #483d8b;">'django.core.context_processors.request'</span>,
   <span style="color: #483d8b;">'django_authopenid.context_processors.authopenid'</span>,
<span style="color: black;">&#41;</span></pre></div></div>

</li>
<li>Copy templates from the examples folder. Of course you&#8217;ll want them eventually to be fully integrated with your site, so that they match styles or even be incorporated in other pages, but for now, let&#8217;s just use the ones provided with the package. Location of the example templates obviously depends on your Linux distribution. On my Debian installation it was in /usr/lib/python2.5/site-packages/django_authopenid-1.0.1-py2.5.egg/example/templates. Copy examples/ directory to your Django application directory.</li>
</ol>
<h3>Test it</h3>
<p>Once you&#8217;re done, test it. Navigate to http:///account/signin/ and if you followed this manual you should see something like this:</p>
<div id="attachment_474" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-474" title="openid-login" src="http://www.grenadepod.com/wp-content/uploads/2009/11/openid-login-300x162.png" alt="Django authopenid login screen" width="300" height="162" /><p class="wp-caption-text">Django authopenid login screen</p></div>
<p>You may wonder what the login prompt on the left side is doing there. This is because Django authopenId logoc is to associate your OpenId account with local account and use standard Django &#8220;registration&#8221; middleware classes to handle all user management functions. If user does not exist &#8211; after you have selected your OpenID URL you will be asked to provide local username. You don&#8217;t need to memorise nor use it &#8211; it&#8217;s just to allow authopenId create association.</p>
<p>This may feel bit unintuitive, and I don&#8217;t particularly like this. I&#8217;m going to make some changes so that internal association is hidden from the end user and happens behind the scenes. This way my users are going to see only OpenId selector and login screen.</p>


<p>Related posts:<ol><li><a href='http://www.grenadepod.com/2009/12/13/python-web-crawler/' rel='bookmark' title='Permanent Link: Python web crawler'>Python web crawler</a></li>
<li><a href='http://www.grenadepod.com/2009/11/09/how-to-use-generic-views-in-django/' rel='bookmark' title='Permanent Link: How to use generic views in Django'>How to use generic views in Django</a></li>
<li><a href='http://www.grenadepod.com/2009/12/03/building-and-running-google-chrome-os-on-virtualbox/' rel='bookmark' title='Permanent Link: Building and running Google Chrome OS on VirtualBox'>Building and running Google Chrome OS on VirtualBox</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.grenadepod.com/2009/11/22/using-openid-for-authentication-in-django/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
