Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django-allauth support for obtaining google access-token #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 28 additions & 17 deletions django_mailbox/google_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -17,37 +20,45 @@ class RefreshTokenNotFound(Exception):


def get_google_consumer_key():
if SocialApp:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be a configuration option as using django-allauth and django-guardian in one application doesn't necessarily mean that django-allauth provides a secret for django-guardian.

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Picking the first all auth google account is not deterministic enough if it's used to provide end-user authentication...

if app:
return app.secret
return settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET


def get_google_account(email, key=None):
try:
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 RefreshTokenNotFound if key == 'refresh_token' else AccessTokenNotFound


def get_google_access_token(email):
# TODO: This should be cacheable
try:
me = UserSocialAuth.objects.get(uid=email, provider="google-oauth2")
return me.extra_data['access_token']
except (UserSocialAuth.DoesNotExist, KeyError):
raise AccessTokenNotFound
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")
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):
Expand Down