From 8cd5c923d5a36dd76333ed846effdb695c691c8f Mon Sep 17 00:00:00 2001 From: Tom Anthony Date: Thu, 27 Nov 2014 15:52:36 +0000 Subject: [PATCH 1/4] Added GOOGLEAUTH_APPS_CLEAN_USERNAME option. --- README.rst | 3 +++ googleauth/backends.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/README.rst b/README.rst index 18a32a7..61f199c 100644 --- a/README.rst +++ b/README.rst @@ -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 ~~~~~~~~~~ diff --git a/googleauth/backends.py b/googleauth/backends.py index 919868b..cbbbb1b 100644 --- a/googleauth/backends.py +++ b/googleauth/backends.py @@ -5,6 +5,7 @@ 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) class GoogleAuthBackend(object): @@ -14,6 +15,9 @@ def authenticate(self, identifier=None, attributes=None): email = attributes.get('email', None) (username, domain) = email.split('@') + if CLEAN_USERNAME: + username = filter(str.isalpha, username.lower()) + if APPS_DOMAIN and APPS_DOMAIN != domain: return None From 76cf0049a4017129c5d4a714eb1bcb82d9a9b2ae Mon Sep 17 00:00:00 2001 From: Tom Anthony Date: Thu, 27 Nov 2014 16:06:59 +0000 Subject: [PATCH 2/4] Handled unicode with GOOGLEAUTH_APPS_CLEAN_USERNAME feature. --- googleauth/backends.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googleauth/backends.py b/googleauth/backends.py index cbbbb1b..1d9b8ec 100644 --- a/googleauth/backends.py +++ b/googleauth/backends.py @@ -16,7 +16,7 @@ def authenticate(self, identifier=None, attributes=None): (username, domain) = email.split('@') if CLEAN_USERNAME: - username = filter(str.isalpha, username.lower()) + username = filter(str.isalpha, str(username).lower()) if APPS_DOMAIN and APPS_DOMAIN != domain: return None From e39f8bba35e0eec2c182b00c871d2f27e833527c Mon Sep 17 00:00:00 2001 From: Duncan Morris Date: Wed, 21 Jan 2015 13:16:59 +0000 Subject: [PATCH 3/4] Enable saving profile information returned by google to be saved to a different model --- googleauth/backends.py | 24 +++++++++++++++++++++++- googleauth/views.py | 8 +++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/googleauth/backends.py b/googleauth/backends.py index 1d9b8ec..c17d1fe 100644 --- a/googleauth/backends.py +++ b/googleauth/backends.py @@ -1,11 +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): @@ -48,6 +52,15 @@ 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): @@ -55,3 +68,12 @@ def get_user(self, user_id): 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() diff --git a/googleauth/views.py b/googleauth/views.py index be44735..106d4fb 100644 --- a/googleauth/views.py +++ b/googleauth/views.py @@ -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 @@ -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) From 506cf505bcf70b54ba94f9a6fe886373346affd6 Mon Sep 17 00:00:00 2001 From: Duncan Morris Date: Fri, 23 Jan 2015 15:46:37 +0000 Subject: [PATCH 4/4] Redirect to 'next' parameter if set --- googleauth/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googleauth/views.py b/googleauth/views.py index 106d4fb..798a81f 100644 --- a/googleauth/views.py +++ b/googleauth/views.py @@ -59,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)))