| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import requests |
| 4 |
|
| 5 |
from django.http import HttpRequest |
| 6 |
|
| 7 |
from allauth.account.models import EmailAddress |
| 8 |
from allauth.socialaccount.adapter import get_adapter |
| 9 |
from allauth.socialaccount.app_settings import QUERY_EMAIL |
| 10 |
from allauth.socialaccount.providers.base import AuthAction, ProviderAccount |
| 11 |
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter |
| 12 |
from allauth.socialaccount.providers.oauth2.client import OAuth2Error |
| 13 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 14 |
|
| 15 |
|
| 16 |
class Scope: |
| 17 |
EMAIL = "email" |
| 18 |
PROFILE = "profile" |
| 19 |
|
| 20 |
|
| 21 |
class GoogleAccount(ProviderAccount): |
| 22 |
""" |
| 23 |
The account data can be in two formats. One, originating from |
| 24 |
the /v2/userinfo endpoint: |
| 25 |
|
| 26 |
{'email': '[email protected]', |
| 27 |
'given_name': 'John', |
| 28 |
'id': '12345678901234567890', |
| 29 |
'locale': 'en', |
| 30 |
'name': 'John', |
| 31 |
'picture': 'https://lh3.googleusercontent.com/a/code', |
| 32 |
'verified_email': True} |
| 33 |
|
| 34 |
The second, which is the payload of the id_token: |
| 35 |
|
| 36 |
{'at_hash': '-someHASH', |
| 37 |
'aud': '123-pqr.apps.googleusercontent.com', |
| 38 |
'azp': '123-pqr.apps.googleusercontent.com', |
| 39 |
'email': '[email protected]', |
| 40 |
'email_verified': True, |
| 41 |
'exp': 1707297277, |
| 42 |
'given_name': 'John', |
| 43 |
'iat': 1707293677, |
| 44 |
'iss': 'https://accounts.google.com', |
| 45 |
'locale': 'en', |
| 46 |
'name': 'John', |
| 47 |
'picture': 'https://lh3.googleusercontent.com/a/code', |
| 48 |
'sub': '12345678901234567890'} |
| 49 |
""" |
| 50 |
|
| 51 |
def get_profile_url(self): |
| 52 |
return self.account.extra_data.get("link") |
| 53 |
|
| 54 |
def get_avatar_url(self): |
| 55 |
return self.account.extra_data.get("picture") |
| 56 |
|
| 57 |
|
| 58 |
class GoogleProvider(OAuth2Provider): |
| 59 |
id = "google" |
| 60 |
name = "Google" |
| 61 |
account_class = GoogleAccount |
| 62 |
oauth2_adapter_class = GoogleOAuth2Adapter |
| 63 |
supports_token_authentication = True |
| 64 |
|
| 65 |
def get_default_scope(self): |
| 66 |
scope = [Scope.PROFILE] |
| 67 |
if QUERY_EMAIL: |
| 68 |
scope.append(Scope.EMAIL) |
| 69 |
return scope |
| 70 |
|
| 71 |
def get_auth_params_from_request(self, request: HttpRequest, action): |
| 72 |
ret = super().get_auth_params_from_request(request, action) |
| 73 |
if action == AuthAction.REAUTHENTICATE: |
| 74 |
ret["prompt"] = "select_account consent" |
| 75 |
return ret |
| 76 |
|
| 77 |
def extract_uid(self, data): |
| 78 |
if "sub" in data: |
| 79 |
return data["sub"] |
| 80 |
return data["id"] |
| 81 |
|
| 82 |
def extract_common_fields(self, data): |
| 83 |
return dict( |
| 84 |
email=data.get("email"), |
| 85 |
last_name=data.get("family_name"), |
| 86 |
first_name=data.get("given_name"), |
| 87 |
) |
| 88 |
|
| 89 |
def extract_email_addresses(self, data): |
| 90 |
ret = [] |
| 91 |
email = data.get("email") |
| 92 |
if email: |
| 93 |
verified = bool(data.get("email_verified") or data.get("verified_email")) |
| 94 |
ret.append(EmailAddress(email=email, verified=verified, primary=True)) |
| 95 |
return ret |
| 96 |
|
| 97 |
def verify_token(self, request: HttpRequest, token): |
| 98 |
from allauth.socialaccount.providers.google import views |
| 99 |
|
| 100 |
credential = token.get("id_token") |
| 101 |
if not credential: |
| 102 |
raise get_adapter().validation_error("invalid_token") |
| 103 |
try: |
| 104 |
identity_data = views._verify_and_decode( |
| 105 |
app=self.app, credential=credential |
| 106 |
) |
| 107 |
except (OAuth2Error, requests.RequestException) as e: |
| 108 |
raise get_adapter().validation_error("invalid_token") from e |
| 109 |
login = self.sociallogin_from_response(request, identity_data) |
| 110 |
return login |
| 111 |
|
| 112 |
|
| 113 |
provider_classes = [GoogleProvider] |