| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.account.models import EmailAddress |
| 4 |
from allauth.socialaccount.providers.amazon_cognito.utils import ( |
| 5 |
convert_to_python_bool_if_value_is_json_string_bool, |
| 6 |
) |
| 7 |
from allauth.socialaccount.providers.amazon_cognito.views import ( |
| 8 |
AmazonCognitoOAuth2Adapter, |
| 9 |
) |
| 10 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 11 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 12 |
|
| 13 |
|
| 14 |
class AmazonCognitoAccount(ProviderAccount): |
| 15 |
def get_avatar_url(self): |
| 16 |
return self.account.extra_data.get("picture") |
| 17 |
|
| 18 |
def get_profile_url(self): |
| 19 |
return self.account.extra_data.get("profile") |
| 20 |
|
| 21 |
|
| 22 |
class AmazonCognitoProvider(OAuth2Provider): |
| 23 |
id = "amazon_cognito" |
| 24 |
name = "Amazon Cognito" |
| 25 |
account_class = AmazonCognitoAccount |
| 26 |
oauth2_adapter_class = AmazonCognitoOAuth2Adapter |
| 27 |
|
| 28 |
def extract_uid(self, data): |
| 29 |
return str(data["sub"]) |
| 30 |
|
| 31 |
def extract_common_fields(self, data): |
| 32 |
return { |
| 33 |
"email": data.get("email"), |
| 34 |
"first_name": data.get("given_name"), |
| 35 |
"last_name": data.get("family_name"), |
| 36 |
} |
| 37 |
|
| 38 |
def get_default_scope(self): |
| 39 |
return ["openid", "profile", "email"] |
| 40 |
|
| 41 |
def extract_email_addresses(self, data): |
| 42 |
email = data.get("email") |
| 43 |
verified = convert_to_python_bool_if_value_is_json_string_bool( |
| 44 |
data.get("email_verified", False) |
| 45 |
) |
| 46 |
|
| 47 |
return ( |
| 48 |
[EmailAddress(email=email, verified=verified, primary=True)] |
| 49 |
if email |
| 50 |
else [] |
| 51 |
) |
| 52 |
|
| 53 |
def extract_extra_data(self, data): |
| 54 |
ret = dict(data) |
| 55 |
phone_number_verified = data.get("phone_number_verified") |
| 56 |
if phone_number_verified is not None: |
| 57 |
ret["phone_number_verified"] = ( |
| 58 |
convert_to_python_bool_if_value_is_json_string_bool( |
| 59 |
"phone_number_verified" |
| 60 |
) |
| 61 |
) |
| 62 |
return ret |
| 63 |
|
| 64 |
@classmethod |
| 65 |
def get_slug(cls): |
| 66 |
# IMPORTANT: Amazon Cognito does not support `_` characters |
| 67 |
# as part of their redirect URI. |
| 68 |
return super().get_slug().replace("_", "-") |
| 69 |
|
| 70 |
|
| 71 |
provider_classes = [AmazonCognitoProvider] |