Skip to content
This repository was archived by the owner on May 13, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Optional settings::
# list of default group names to assign to new users
GOOGLEAUTH_GROUPS = []

# when creating username from email convert to lowercase and strip non alpha characters, default False
GOOGLEAUTH_APPS_CLEAN_USERNAME = True

URL routes
~~~~~~~~~~

Expand Down
28 changes: 27 additions & 1 deletion googleauth/backends.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import requests
# import requests

from django.conf import settings
from django.db.models import get_model
from django.contrib.auth.models import User, Group

IS_STAFF = getattr(settings, 'GOOGLEAUTH_IS_STAFF', False)
GROUPS = getattr(settings, 'GOOGLEAUTH_GROUPS', tuple())
APPS_DOMAIN = getattr(settings, 'GOOGLEAUTH_APPS_DOMAIN', None)
CLEAN_USERNAME = getattr(settings, 'GOOGLEAUTH_APPS_CLEAN_USERNAME', False)
USERPROFILE_MODEL = getattr(settings, 'GOOGLEAUTH_USERPROFILE_MODEL', None)
PROFILE_FIELDS = getattr(settings, 'GOOGLEAUTH_PROFILE_FIELDS', None)


class GoogleAuthBackend(object):
Expand All @@ -14,6 +19,9 @@ def authenticate(self, identifier=None, attributes=None):
email = attributes.get('email', None)
(username, domain) = email.split('@')

if CLEAN_USERNAME:
username = filter(str.isalpha, str(username).lower())

if APPS_DOMAIN and APPS_DOMAIN != domain:
return None

Expand Down Expand Up @@ -44,10 +52,28 @@ def authenticate(self, identifier=None, attributes=None):

user.save()

if USERPROFILE_MODEL and PROFILE_FIELDS:
model_details = USERPROFILE_MODEL.rsplit('.', 1)

model = get_model(model_details[0], model_details[1])
try:
model.objects.get(user=user)
except model.DoesNotExist:
self.save_user_profile(model, user, attributes)

return user

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
pass

def save_user_profile(self, model, user, attributes):

to_save = {'user': user}
for google_field, profile_field in PROFILE_FIELDS.iteritems():
to_save[profile_field] = attributes.get(google_field, None)

userprofile = model(**to_save)
userprofile.save()
10 changes: 8 additions & 2 deletions googleauth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
CALLBACK_DOMAIN = getattr(settings, 'GOOGLEAUTH_CALLBACK_DOMAIN', None)
APPS_DOMAIN = getattr(settings, 'GOOGLEAUTH_APPS_DOMAIN', None)
GET_PROFILE = getattr(settings, 'GOOGLEAUTH_GET_PROFILE', True)
USERPROFILE_MODEL = getattr(settings, 'GOOGLEAUTH_USERPROFILE_MODEL', None)
PROFILE_FIELDS = getattr(settings, 'GOOGLEAUTH_PROFILE_FIELDS', None)


#
# utility methods
Expand Down Expand Up @@ -56,7 +59,7 @@ def login(request):
params['hd'] = APPS_DOMAIN

request.session['googleauth_csrf'] = csrf_token
request.session['next'] = request.META.get('HTTP_REFERER', None)
request.session['next'] = request.GET.get('next', None) or request.META.get('HTTP_REFERER', None)

return HttpResponseRedirect("%s?%s" % (GOOGLE_AUTH_ENDPOINT, urllib.urlencode(params)))

Expand Down Expand Up @@ -104,10 +107,13 @@ def callback(request):

profile = resp.json()

if USERPROFILE_MODEL and PROFILE_FIELDS:
for google_field in PROFILE_FIELDS.iterkeys():
attributes[google_field] = profile.get(google_field, None)

attributes['first_name'] = profile.get('given_name')
attributes['last_name'] = profile.get('family_name')


# authenticate user

user = auth.authenticate(attributes=attributes)
Expand Down