Google Account authentication

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# set the following in settings.py:
#
INSTALLED_APPS = ('django_openid_auth', ...) # https://launchpad.net/django-openid-auth
AUTHENTICATION_BACKENDS = ('somewhere.auth.GoogleBackend', ...)
LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = '/logout/'
OPENID_SSO_SERVER_URL = 'https://www.google.com/accounts/o8/id'

# urlconfs needed in urls.py
#
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'),


# And finally, here is auth.py
#
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:
      # create a new user, or send a message to admins, etc.
      return None

    return user

  def get_user(self, user_id):

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

More like this

  1. Registering Facebook user by username before loggin in by zjor 6 days, 22 hours ago
  2. Use email addresses for user name by chris 6 years, 3 months ago
  3. Login with email or username by zeeg 4 years, 10 months ago
  4. User activation codes without additional database tables or fields by badzong 7 months, 4 weeks ago
  5. Partial OpenID provider implementation from idproxy.net by simon 5 years, 11 months ago

Comments

natR (on May 13, 2012):

thanks!

#

natR (on May 14, 2012):

I'm receiving the following error (on Google App Engine).

DatabaseError at /login-complete/ This query is not supported by the database.

Any ideas?

#

natR (on May 14, 2012):

think i figured it out (with a clue from this page: http://groups.google.com/group/django-non-relational/tree/browse_frm/month/2010-11/cbcd40a7537fafd4?rnum=191&lnk=ol )

you didn't say otherwise, but i realized 'auth.GoogleBackend' needs to be listed ahead of other rear-ends. i faintly recollect reading in the Django docs that the backends are tried sequentially.

#

(Forgotten your password?)