2013-03-29 17:39:53 +01:00
|
|
|
from zephyr.models import UserProfile, get_user_profile_by_id, \
|
|
|
|
get_user_profile_by_email
|
2013-01-09 22:45:23 +01:00
|
|
|
from django.conf import settings
|
2013-02-20 22:26:06 +01:00
|
|
|
|
|
|
|
from openid.consumer.consumer import SUCCESS
|
|
|
|
|
2012-09-21 16:10:36 +02:00
|
|
|
class EmailAuthBackend(object):
|
|
|
|
"""
|
|
|
|
Email Authentication Backend
|
|
|
|
|
|
|
|
Allows a user to sign in using an email/password pair rather than
|
|
|
|
a username/password pair.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def authenticate(self, username=None, password=None):
|
|
|
|
""" Authenticate a user based on email address as the user name. """
|
2012-10-29 19:17:03 +01:00
|
|
|
if username is None or password is None:
|
|
|
|
# Return immediately. Otherwise we will look for a SQL row with
|
|
|
|
# NULL username. While that's probably harmless, it's needless
|
|
|
|
# exposure.
|
|
|
|
return None
|
|
|
|
|
2012-09-21 16:10:36 +02:00
|
|
|
try:
|
2013-03-29 17:39:53 +01:00
|
|
|
user_profile = UserProfile.objects.get(email__iexact=username)
|
|
|
|
if user_profile.check_password(password):
|
|
|
|
return user_profile
|
|
|
|
except UserProfile.DoesNotExist:
|
2012-09-21 16:10:36 +02:00
|
|
|
return None
|
|
|
|
|
2013-03-29 17:39:53 +01:00
|
|
|
def get_user(self, user_profile_id):
|
|
|
|
""" Get a UserProfile object from the user_profile_id. """
|
|
|
|
try:
|
|
|
|
return get_user_profile_by_id(user_profile_id)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
return None
|
2013-02-20 22:26:06 +01:00
|
|
|
|
|
|
|
# Adapted from http://djangosnippets.org/snippets/2183/ by user Hangya (September 1, 2010)
|
|
|
|
|
2013-03-29 17:39:53 +01:00
|
|
|
class GoogleBackend(object):
|
2013-02-20 22:26:06 +01:00
|
|
|
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')
|
|
|
|
|
|
|
|
try:
|
2013-03-29 17:39:53 +01:00
|
|
|
user_profile = get_user_profile_by_email(google_email)
|
|
|
|
except UserProfile.DoesNotExist:
|
2013-02-20 22:26:06 +01:00
|
|
|
# create a new user, or send a message to admins, etc.
|
|
|
|
return None
|
|
|
|
|
2013-03-29 17:39:53 +01:00
|
|
|
return user_profile
|
2013-02-20 22:26:06 +01:00
|
|
|
|
2013-03-29 17:39:53 +01:00
|
|
|
def get_user(self, user_profile_id):
|
|
|
|
""" Get a UserProfile object from the user_profile_id. """
|
2013-02-20 22:26:06 +01:00
|
|
|
try:
|
2013-03-29 17:39:53 +01:00
|
|
|
return get_user_profile_by_id(user_profile_id)
|
|
|
|
except UserProfile.DoesNotExist:
|
2012-09-21 16:10:36 +02:00
|
|
|
return None
|