<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=338168267432629&amp;ev=PageView&amp;noscript=1">
Programming

How to setup a Google Account Authentication

One of the awesome things that Django offers us is it lets us use our Google account as authentication to the website that we are building. I am going to show you simple steps to set it up. Here are the steps: 1. install django-openid-auth in your virtualenv: pip install …


One of the awesome things that Django offers us is it lets us use our Google account as authentication to the website that we are building. I am going to show you simple steps to set it up.

Here are the steps:
1. install django-openid-auth in your virtualenv:
pip install django-openid-auth

2. install python-openid:
pip install python-openid

3. Open your “settings.py”.

4. add this to your installed apps: ‘django_openid_auth’

5. add these following setups:

AUTHENTICATION_BACKENDS = (‘django.contrib.auth.backends.ModelBackend’,’auth.GoogleBackend’,)
LOGIN_URL = ‘/login/’
LOGIN_REDIRECT_URL = ‘/’
LOGOUT_URL = ‘/logout/’
OPENID_SSO_SERVER_URL = ‘https://www.google.com/accounts/o8/id’

6. open your urls.py in your project root.

7. Add this to your urlconf:

url(r’^login/$’, ‘django_openid_auth.views.login_begin’, name=’openid-login’),
url(r’^login-complete/$’, ‘django_openid_auth.views.login_complete’, name=’openid-complete’),
url(r’^logout/$’, ‘django.contrib.auth.views.logout’, {‘next_page’: ‘/’,}, name=’logout’),

8. Create a file in your project root and name it auth.py.

9. Insert this code to the file you just created:

from django.contrib.auth.models import User
from openid.consumer.consumer import SUCCESS
from django.core.mail import mail_admins

class GoogleBackend:

    def authenticate(self, openid_response):
        if openid_response is None:
            return None
        if openid_response.status != SUCCESS:
            return None

        google_email = openid_response.getSigned('http://openid.net/srv/ax/1.0',  'value.email')
        google_firstname = openid_response.getSigned('http://openid.net/srv/ax/1.0', 'value.firstname')
        google_lastname = openid_response.getSigned('http://openid.net/srv/ax/1.0', 'value.lastname')
        try:
            user = User.objects.get(username=google_email)
        except User.DoesNotExist:
            user = User.objects.create_user(google_email, google_email,
                                             'password')
            user.save()
            user = User.objects.get(username=google_email)
            return user

        return user

    def get_user(self, user_id):

        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

That’s it. Those are the steps you need to setup Google Account authentication.

Some information you need to know:

In step 7, we added the link to login using Google account and a logout link which logs out the user in the website. The login link which brings the user to Google Account login page looks like this “yoursite.com/login_openid”. If the user has successfully login with his Google account, it automatically redirects back to the site. The user is already logged in to “yoursite.com”. The logout link looks like this “yoursite.com/logout/”. This will logout the user to “yoursite.com”.

In the file auth.py, we have a class GoogleBackend with a function authenticate. The way authenticate function works here is it creates a user with the username equal to it’s google email. You can always modify the code according to your needs.

Source:
http://djangosnippets.org/snippets/2183/

Similar posts

Get notified about the latest in Tech

Be the first to know about new tech from the experts at Bixly!