-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
provider.py
59 lines (44 loc) · 1.68 KB
/
provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from allauth.account.models import EmailAddress
from allauth.socialaccount.app_settings import QUERY_EMAIL
from allauth.socialaccount.providers.base import AuthAction, ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class Scope(object):
EMAIL = "email"
PROFILE = "profile"
class GoogleAccount(ProviderAccount):
def get_profile_url(self):
return self.account.extra_data.get("link")
def get_avatar_url(self):
return self.account.extra_data.get("picture")
def to_str(self):
dflt = super(GoogleAccount, self).to_str()
return self.account.extra_data.get("name", dflt)
class GoogleProvider(OAuth2Provider):
id = "google"
name = "Google"
account_class = GoogleAccount
def get_default_scope(self):
scope = [Scope.PROFILE]
if QUERY_EMAIL:
scope.append(Scope.EMAIL)
return scope
def get_auth_params(self, request, action):
ret = super(GoogleProvider, self).get_auth_params(request, action)
if action == AuthAction.REAUTHENTICATE:
ret["prompt"] = "select_account consent"
return ret
def extract_uid(self, data):
return str(data["id"])
def extract_common_fields(self, data):
return dict(
email=data.get("email"),
last_name=data.get("family_name"),
first_name=data.get("given_name"),
)
def extract_email_addresses(self, data):
ret = []
email = data.get("email")
if email and data.get("verified_email"):
ret.append(EmailAddress(email=email, verified=True, primary=True))
return ret
provider_classes = [GoogleProvider]