2021-01-20 00:22:54 +00:00
|
|
|
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)
|
2021-01-20 03:16:29 +00:00
|
|
|
if settings.DISCORD_BOT_TOKEN is None and settings.DEBUG:
|
|
|
|
return login
|
2021-01-20 00:22:54 +00:00
|
|
|
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)
|