From 6b91ed4d257e7972ebc24deda96dc90698fb4db4 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Thu, 10 Jan 2019 13:33:45 +0100 Subject: [PATCH] Fix account associations by email --- auth/backend.py | 10 +++++++--- auth/pipeline.py | 9 +++++++++ spejstore/settings.py | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/auth/backend.py b/auth/backend.py index 25c6d65..363923c 100644 --- a/auth/backend.py +++ b/auth/backend.py @@ -15,17 +15,21 @@ class HSWawOAuth2(BaseOAuth2): ] def get_user_details(self, response): - """Return user details from GitHub account""" + """Return user details from Hackerspace account""" + personal_email = None + if response.get('personal_email'): + personal_email = response.get('personal_email')[0] + return {'username': response.get('username'), 'email': response.get('email'), + 'personal_email': personal_email, } def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" url = 'https://sso.hackerspace.pl/api/1/profile' headers = { - 'Authorization': 'Bearer {}'.format(access_token) - + 'Authorization': 'Bearer {}'.format(access_token) } return self.get_json(url, headers=headers) diff --git a/auth/pipeline.py b/auth/pipeline.py index 74f6918..8895810 100644 --- a/auth/pipeline.py +++ b/auth/pipeline.py @@ -1,3 +1,12 @@ +from social_core.pipeline.social_auth import associate_by_email + + def staff_me_up(backend, details, response, uid, user, *args, **kwargs): user.is_staff = True user.save() + + +def associate_by_personal_email(backend, details, user=None, *args, **kwargs): + return associate_by_email(backend, { + 'email': details.get('personal_email'), + }, user, *args, **kwargs) diff --git a/spejstore/settings.py b/spejstore/settings.py index 4a1b11c..f5ea2fc 100644 --- a/spejstore/settings.py +++ b/spejstore/settings.py @@ -135,6 +135,7 @@ SOCIAL_AUTH_PIPELINE = ( 'social_core.pipeline.social_auth.social_user', 'social_core.pipeline.user.get_username', 'social_core.pipeline.social_auth.associate_by_email', + 'auth.pipeline.associate_by_personal_email', 'social_core.pipeline.user.create_user', 'social_core.pipeline.social_auth.associate_user', 'social_core.pipeline.social_auth.load_extra_data',