How To Setup a Django Project with OpenID 2.x Authentication

This is a brief tutorial on how to setup a new django-project with openid authentication support.

django-authopenid basics

We will use django-authopenid for this project. It provides support for openid-2.x which is not available in other projects like django-openid.

In a login process, after a successful authentication via OpenID, django-authopenid requires the user to connect the OpenID-URL with a local user (existing or newly created). Creating a new user is as simple as providing an username and email; no activation is required by default. This is the (simplified) authentication process of django-authopenid:

Django-authopenid associates the users and OpenID-URL's in a database-table called "user_associations", which is available via the admin interface. The package provides quite some functionality like password reset, password change, ... and provides this URL structure by default:

  /account/ .................... main page of account for user  
  /account/signin/ ............. go to signin page
  /account/signout/ ............ signout url
  /account/signin/complete/ .... register openid after signin 
  /account/signup/ ............. legacy authentification 
  /account/email/ .............. change email page
  /account/delete/ ............. delete account
  /account/openid/ ............. associate a new openid for an account
  /account/password/ ........... change password page
  /account/password/confirm/ ... confirm change of password
  /account/sendpw/password/ .... send a new password

More infos can be found in the project's README.

Requirements

First of all we need to setup the environment with a few required packages:

Project Setup

Have a look at the websvn for the structure of the demo-project. There are downloads for the full project we are going to create in the next steps zipped and as tar.gz. If you download and extract one, you can skip directly to starting the server.

Let's start with creating our project, which will have this basic directory-structure:

  /demoproject/ ....................... urls.py, settings.py, manage.py
  /demoproject/main/ .................. our own app (models.py, views.py)
  /demoproject/django_authopenid/ ..... the django-authopenid libraries
  /demoproject/templates/ ............. all templates for our project and django-authopenid

Let's create the project with this structure now:

django-admin startproject demoproject
cd demoproject
python manage.py startapp main
svn checkout http://django-authopenid.googlecode.com/svn/trunk/django_authopenid
mv django_authopenid/templates .

Thats pretty much it :-) Perhaps get rid of the .svn directories:

rm -rf django_authopenid/.svn templates/.svn templates/authopenid/.svn

At this point we want to set up our settings.py:

  1. Database support (eg. sqlite3 with database_name "main.db")
  2. OpenIDMiddleware to MIDDLEWARE_CLASSES
    MIDDLEWARE_CLASSES = (
        ...
        'django_authopenid.middleware.OpenIDMiddleware',
    )
    
  3. Add LOGIN_URL somewhere:
    LOGIN_URL = "/account/signin/"
  4. Add django_authopenid and our main app to INSTALLED_APPS:
    INSTALLED_APPS = (
        ...
        'django_authopenid',
        'demoproject.main',
    )
    
  5. Add the template directory:
    import os.path
    TEMPLATE_DIRS = (
        os.path.join(os.path.dirname(__file__), 'templates'),
    )
  6. Maybe set variables for email
One more thing about the templates: the default django-authopenid templates extend a base.html file, which we can create like this:
 echo "{% block content %}{% endblock %}" > templates/base.html

Finally, we want to add the django-authopenid url's to urls.py:

    (r'^account/', include('django_authopenid.urls')),

And last but not least: creating the database.

python manage.py syncdb

Starting the Server

The basic setup is completed, let's start the django-server!

python manage.py runserver

Test the login-procedure and account-functions by browsing to http://localhost:8000/account/

Once a user logged in via OpenID, the users URL is stored in request.openid

This could be an example html template:

{% extends "base.html" %}

{% block content %}
    <h1>Django AuthOpenID Test</h1>
    
    <p>You are logged in as {{ request.user }}</p>
    <p>Your OpenID URL: {{ request.openid }}</p>
    <p><a href="/account">Account</p>
{% endblock %}



Hope this could help a bit and you enjoy using OpenID in your Django projects!

submit to reddit    Digg!

educativos, acheter cialis en france, :-), cialis acheter, :]]],

flytte, cialis belgique, 2564, generica cialis, obxp,

efteraret, cialis viagra, mjboir, acheter cialis original, plnq,

organisation, cialis generico comprar, incqlx, cialis generico, =]]], cialis generico, =-[,

szabadon, generica cialis, 194597, achat cialis france, =-]],

assume, viagra ou cialis, >:((, achat cialis france, %-)),

murica, cialis sin receta, 8), generica cialis, 24865,

transformee, comprar cialis generico, 125622, cialis acheter, :-DDD,

accompagnato, cialis acheter, 8391, viagra cialis generica, 161053, vendita cialis, 013,

deriteni, cialis efectos secundarios, dfbyz, acheter cialis, :OO, cialis 5 mg, %-OOO,

godkender, acheter cialis en france, :-))), cialis viagra, 311053, cialis effet secondaire, 91894,

oppbygging, comprar cialis, 914, cialis effet secondaire, 74474,

valille, effetti cialis, >:DD, comprar cialis generico, 8-PPP,

pinnoitteena, comprare viagra italia cialis online, ztacja, cialis 20 mg, %-[[,

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
10 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.