From 84543396f7408b9c595ea12b9c8382ac5bc4ecfb Mon Sep 17 00:00:00 2001 From: ak4zh Date: Sun, 30 Aug 2020 14:17:37 +0530 Subject: [PATCH 1/2] added django-allauth support for obtaining google token --- django_mailbox/google_utils.py | 41 +++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/django_mailbox/google_utils.py b/django_mailbox/google_utils.py index b834f59c..422defcc 100644 --- a/django_mailbox/google_utils.py +++ b/django_mailbox/google_utils.py @@ -3,7 +3,10 @@ from django.conf import settings import requests from social.apps.django_app.default.models import UserSocialAuth - +try: + from allauth.socialaccount.models import SocialAccount, SocialApp +except ImportError: + SocialAccount, SocialApp = None, None logger = logging.getLogger(__name__) @@ -17,34 +20,46 @@ class RefreshTokenNotFound(Exception): def get_google_consumer_key(): + if SocialApp: + app = SocialApp.objects.filter(provider='google').first() + if app: + return app.client_id return settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY def get_google_consumer_secret(): + if SocialApp: + app = SocialApp.objects.filter(provider='google').first() + if app: + return app.secret return settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET -def get_google_access_token(email): - # TODO: This should be cacheable +def get_google_account(email, key=None): try: - me = UserSocialAuth.objects.get(uid=email, provider="google-oauth2") - return me.extra_data['access_token'] - except (UserSocialAuth.DoesNotExist, KeyError): + if SocialAccount: + me = SocialAccount.objects.get(uid=email, provider='google') + else: + me = UserSocialAuth.objects.get(uid=email, provider="google-oauth2") + return me.extra_data[key] if key else me + except (UserSocialAuth.DoesNotExist, SocialAccount.DoesNotExist, KeyError): raise AccessTokenNotFound +def get_google_access_token(email): + # TODO: This should be cacheable + return get_google_account(email, key='access_token') + + def update_google_extra_data(email, extra_data): - try: - me = UserSocialAuth.objects.get(uid=email, provider="google-oauth2") - me.extra_data = extra_data - me.save() - except (UserSocialAuth.DoesNotExist, KeyError): - raise AccessTokenNotFound + me = get_google_account(email) + me.extra_data = extra_data + me.save() def get_google_refresh_token(email): try: - me = UserSocialAuth.objects.get(uid=email, provider="google-oauth2") + me = get_google_account(email) return me.extra_data['refresh_token'] except (UserSocialAuth.DoesNotExist, KeyError): raise RefreshTokenNotFound From ac6b25e737aa9f533d27aa4711b3d3d35967dcaa Mon Sep 17 00:00:00 2001 From: ak4zh Date: Sun, 30 Aug 2020 14:21:40 +0530 Subject: [PATCH 2/2] removed duplicate codes for obtaining different data from google extra data dict --- django_mailbox/google_utils.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/django_mailbox/google_utils.py b/django_mailbox/google_utils.py index 422defcc..a3e3fbaa 100644 --- a/django_mailbox/google_utils.py +++ b/django_mailbox/google_utils.py @@ -43,7 +43,7 @@ def get_google_account(email, key=None): me = UserSocialAuth.objects.get(uid=email, provider="google-oauth2") return me.extra_data[key] if key else me except (UserSocialAuth.DoesNotExist, SocialAccount.DoesNotExist, KeyError): - raise AccessTokenNotFound + raise RefreshTokenNotFound if key == 'refresh_token' else AccessTokenNotFound def get_google_access_token(email): @@ -58,11 +58,7 @@ def update_google_extra_data(email, extra_data): def get_google_refresh_token(email): - try: - me = get_google_account(email) - return me.extra_data['refresh_token'] - except (UserSocialAuth.DoesNotExist, KeyError): - raise RefreshTokenNotFound + return get_google_account(email, key='refresh_token') def google_api_get(email, url):