Google Authentification

 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
#models.py
from django.conf import settings
from django.contrib.auth.models import User, check_password
from django.contrib.auth.backends import ModelBackend
from django.utils.translation import ugettext as _
import gdata.contacts
import gdata.contacts.service

class GoogleAuthenticator(ModelBackend):
	def __init__(self):
		"""Make autentication using a google account."""
		self.google_service = gdata.contacts.service.ContactsService()
		
	def authenticate(self, username=None, password=None):
		user = None
		try:
			self.google_service.email = username
			self.google_service.password = password
			self.google_service.source = 'CampanhaEleicoesGerenciamento'
			self.google_service.ProgrammaticLogin()
			login_valid = True
			pwd_valid = True
		except gdata.service.BadAuthentication:
			print "A autenticacao falhou"
			login_valid = True
			pwd_valid = True
			return None
		
		if login_valid and pwd_valid:
			try:
				user = User.objects.get(email=username)
			except User.DoesNotExist:
				return None
			return user
		return None

#settings.py
# Customize authentication backend
AUTHENTICATION_BACKENDS = (
	'campanha_gerenciamento.custom.models.GoogleAuthenticator',
)

More like this

  1. Strip Google Analytics cookies for caching middleware purposes by nf 3 years, 6 months ago
  2. newforms self-contained login form by miracle2k 5 years, 10 months ago
  3. GoogleAdmin: GMaps base layer in Geographic Admin (GeoDjango) by jbronn 4 years, 7 months ago
  4. Goo.gl URL shortener by aabele 2 years, 7 months ago
  5. Using Google Apps Premium infrastructure for user management by tommy 4 years, 10 months ago

Comments

peterbe (on January 31, 2010):

Nice but really poor user confidence-experience. With this you'd be able to capture a user's username & password all in clear text and thus you can access his email, saved searches, google checkout, etc.

I would not go near a service that uses this snippet. That's what OAuth was built to solve.

#

(Forgotten your password?)