35 lines
1.4 KiB
Python
35 lines
1.4 KiB
Python
from .provider import DiscordGuildPermissionsProvider
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.conf import settings
|
|
import requests
|
|
from allauth.socialaccount.providers.discord.views import DiscordOAuth2Adapter
|
|
from allauth.socialaccount.providers.oauth2.views import (
|
|
OAuth2CallbackView,
|
|
OAuth2LoginView,
|
|
)
|
|
|
|
|
|
class DiscordGuildPermissionsOAuth2Adapter(DiscordOAuth2Adapter):
|
|
provider_id = DiscordGuildPermissionsProvider.id
|
|
|
|
def complete_login(self, request, app, token, **kwargs):
|
|
login = super().complete_login(request, app, token, **kwargs)
|
|
if settings.DISCORD_BOT_TOKEN is None and settings.DEBUG:
|
|
return login
|
|
guild_data = requests.get(
|
|
f"https://discord.com/api/guilds/{settings.DISCORD_GUILD_ID}/members/{login.account.uid}",
|
|
headers={
|
|
"Authorization": f"Bot {settings.DISCORD_BOT_TOKEN}",
|
|
"Content-Type": "application/json",
|
|
},
|
|
)
|
|
if guild_data.status_code == 404:
|
|
raise PermissionDenied("You're not a BFOBer.")
|
|
guild_data.raise_for_status()
|
|
login.account.extra_data["guild_data"] = guild_data.json()
|
|
return login
|
|
|
|
|
|
oauth2_login = OAuth2LoginView.adapter_view(DiscordGuildPermissionsOAuth2Adapter)
|
|
oauth2_callback = OAuth2CallbackView.adapter_view(DiscordGuildPermissionsOAuth2Adapter)
|