| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.account.models import EmailAddress |
| 6 |
from allauth.socialaccount.providers.base import AuthAction, ProviderAccount |
| 7 |
from allauth.socialaccount.providers.discord.views import DiscordOAuth2Adapter |
| 8 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 9 |
|
| 10 |
|
| 11 |
class DiscordAccount(ProviderAccount): |
| 12 |
def validate_descriminator(self, discriminator): |
| 13 |
if not isinstance(discriminator, str): |
| 14 |
return False |
| 15 |
|
| 16 |
# As of 2023-06-22, Discord returns string literal '0' for users |
| 17 |
# with no discriminator |
| 18 |
|
| 19 |
return len(discriminator) == 4 if discriminator.isdigit() else False |
| 20 |
|
| 21 |
def is_new_username_system(self): |
| 22 |
if not isinstance(self.account.extra_data, dict): |
| 23 |
return None |
| 24 |
|
| 25 |
discriminator = self.account.extra_data.get("discriminator") |
| 26 |
|
| 27 |
if self.validate_descriminator(discriminator): |
| 28 |
return False |
| 29 |
|
| 30 |
if self.account.extra_data.get("global_name") is not None: |
| 31 |
return True |
| 32 |
|
| 33 |
return None |
| 34 |
|
| 35 |
def to_str(self): |
| 36 |
fallback = super().to_str() |
| 37 |
|
| 38 |
# If the extra_data is malformed, exit early |
| 39 |
if not isinstance(self.account.extra_data, dict): |
| 40 |
return fallback |
| 41 |
|
| 42 |
is_new_system = self.is_new_username_system() |
| 43 |
|
| 44 |
if is_new_system is None: |
| 45 |
# We couldn't determine if the username is new or old |
| 46 |
# so we'll just return the username on it's own. |
| 47 |
display_name = self.account.extra_data.get("username") |
| 48 |
elif is_new_system: |
| 49 |
# global_name can be None or even undefined |
| 50 |
# so we'll use the username as a fallback |
| 51 |
global_name = self.account.extra_data.get("global_name") |
| 52 |
username = self.account.extra_data.get("username") |
| 53 |
display_name = global_name or username |
| 54 |
else: |
| 55 |
# Looks like it's the old username system |
| 56 |
# so we'll just use the username and discriminator |
| 57 |
display_name = "{username}#{discriminator}".format( |
| 58 |
username=self.account.extra_data.get("username"), |
| 59 |
discriminator=self.account.extra_data.get("discriminator"), |
| 60 |
) |
| 61 |
|
| 62 |
# It's very unlikely but still possible that the display_name is None |
| 63 |
# so we'll return or'd against the fallback just incase. We don't want |
| 64 |
# to return None as users of the library expect this to be str. |
| 65 |
return display_name or fallback |
| 66 |
|
| 67 |
def get_avatar_url(self): |
| 68 |
if ( |
| 69 |
"id" in self.account.extra_data.keys() |
| 70 |
and "avatar" in self.account.extra_data.keys() |
| 71 |
): |
| 72 |
return "https://cdn.discordapp.com/avatars/{id}/{avatar}.png".format( |
| 73 |
**self.account.extra_data |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
class DiscordProvider(OAuth2Provider): |
| 78 |
id = "discord" |
| 79 |
name = "Discord" |
| 80 |
account_class = DiscordAccount |
| 81 |
oauth2_adapter_class = DiscordOAuth2Adapter |
| 82 |
|
| 83 |
def extract_uid(self, data): |
| 84 |
return str(data["id"]) |
| 85 |
|
| 86 |
def extract_common_fields(self, data): |
| 87 |
return dict( |
| 88 |
email=data.get("email"), |
| 89 |
username=data.get("username"), |
| 90 |
name=data.get("username"), |
| 91 |
) |
| 92 |
|
| 93 |
def get_auth_params_from_request(self, request: HttpRequest, action): |
| 94 |
ret = super().get_auth_params_from_request(request, action) |
| 95 |
if action == AuthAction.REAUTHENTICATE: |
| 96 |
ret["prompt"] = "consent" |
| 97 |
return ret |
| 98 |
|
| 99 |
def get_default_scope(self): |
| 100 |
return ["email", "identify"] |
| 101 |
|
| 102 |
def extract_email_addresses(self, data): |
| 103 |
ret = [] |
| 104 |
email = data.get("email") |
| 105 |
if email and data.get("verified"): |
| 106 |
ret.append(EmailAddress(email=email, verified=True, primary=True)) |
| 107 |
return ret |
| 108 |
|
| 109 |
|
| 110 |
provider_classes = [DiscordProvider] |