| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 4 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 5 |
from allauth.socialaccount.providers.twitch.views import TwitchOAuth2Adapter |
| 6 |
|
| 7 |
|
| 8 |
class TwitchAccount(ProviderAccount): |
| 9 |
def get_profile_url(self): |
| 10 |
return f"https://twitch.tv/{self.account.extra_data.get('login')}" |
| 11 |
|
| 12 |
def get_avatar_url(self): |
| 13 |
# We're using `logo` as a failback for legacy profiles retrieved |
| 14 |
# with the old https://api.twitch.tv/kraken/user endpoint. |
| 15 |
logo = self.account.extra_data.get("logo") |
| 16 |
return self.account.extra_data.get("profile_image_url", logo) |
| 17 |
|
| 18 |
def to_str(self): |
| 19 |
dflt = super().to_str() |
| 20 |
return self.account.extra_data.get("login", dflt) |
| 21 |
|
| 22 |
|
| 23 |
class TwitchProvider(OAuth2Provider): |
| 24 |
id = "twitch" |
| 25 |
name = "Twitch" |
| 26 |
account_class = TwitchAccount |
| 27 |
oauth2_adapter_class = TwitchOAuth2Adapter |
| 28 |
|
| 29 |
def extract_uid(self, data): |
| 30 |
return str(data["id"]) |
| 31 |
|
| 32 |
def extract_common_fields(self, data): |
| 33 |
return { |
| 34 |
"username": data.get("login"), |
| 35 |
"name": data.get("display_name"), |
| 36 |
"email": data.get("email"), |
| 37 |
} |
| 38 |
|
| 39 |
def get_default_scope(self): |
| 40 |
return ["user:read:email"] |
| 41 |
|
| 42 |
|
| 43 |
provider_classes = [TwitchProvider] |