-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit enables logging in with SSO through Google, overriding the default behavior of `django-allauth`, which is to not let any social providers vouch that a user definitively owns an email address. We should soon add a "Sign up with Google" button to the sign up page too, but I want to make sure this works in production before I get too much further.
- Loading branch information
Showing
4 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import logging | ||
|
||
from allauth.account.models import EmailAddress | ||
from allauth.account.utils import user_email | ||
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter | ||
from allauth.socialaccount.models import SocialLogin | ||
from django.http import HttpRequest | ||
|
||
from ws import settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TrustGoogleEmailOwnershipAdapter(DefaultSocialAccountAdapter): | ||
"""Let users with an existing account grant Google login access. | ||
This adapter exists to provide a better UX in the following scenario: | ||
1. `[email protected]` signs up for `mitoc-trips` with a email & password | ||
2. Alice signs out, time passes. | ||
3. Later, Alice "logs in with Google" | ||
4. Because an account exists under `[email protected]`, we can't complete login | ||
- Alice must first log in with her password, then associate Google | ||
By default, `django-allauth` will not let you automatically claim ownership | ||
of an account just because a social provider vouches that you exist under | ||
that email address. This makes sense. If you own a Facebook account under | ||
`[email protected]`, there's no guarantees that you're necessarily the same | ||
person. | ||
However, because Google is an email provider, I think it's fair to assume | ||
that if Google vouches for your identity, auto sign-in can be completed. | ||
Related: https://github.com/pennersr/django-allauth/issues/418 | ||
""" | ||
|
||
def pre_social_login(self, request: HttpRequest, sociallogin: SocialLogin) -> None: | ||
"""Connect any Google-asserted email to accounts if existing.""" | ||
if sociallogin.is_existing: # Social account exists (normal login) | ||
return | ||
|
||
# I don't think there's an easy way to identify the provider in use... | ||
# `request.path` should be at least be '/accounts/google/login/callback/' | ||
assert set(settings.SOCIALACCOUNT_PROVIDERS) == {'google'} | ||
|
||
email: str = user_email(sociallogin.user) | ||
|
||
try: | ||
verified_email = EmailAddress.objects.get( | ||
email__iexact=email, | ||
# This is critical. If we didn't require a *verified* email, then | ||
# we could end up linking this to an existing account which belongs | ||
# to a malicious user hoping that somebody will link their Google account. | ||
verified=True, | ||
) | ||
except EmailAddress.DoesNotExist: | ||
return | ||
|
||
sociallogin.connect(request, verified_email.user) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters