From 7df3014c3b7fadf78930a79fb5c04d0efd22e10b Mon Sep 17 00:00:00 2001 From: OperationXen Date: Sun, 14 Jan 2024 18:21:26 +0000 Subject: [PATCH] 304 bugfix remove player (#338) * Wrong command context fixed * Corrected call to player utils * Fixed duplicated "in" * Improvements to discord auth logic --- config/settings.py | 1 + discord_login/urls.py | 10 ++++++---- discord_login/views.py | 25 ++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/config/settings.py b/config/settings.py index 3cbf843..ef5cd97 100644 --- a/config/settings.py +++ b/config/settings.py @@ -52,6 +52,7 @@ # Discord OAUTH config DISCORD_CLIENT_ID = getenv("DISCORD_CLIENT_ID") DISCORD_CLIENT_SECRET = getenv("DISCORD_CLIENT_SECRET") + AUTH_REDIRECT_URL = getenv("OAUTH_REDIRECT_URL", "") AUTH_COMPLETE_URL = getenv("OAUTH_COMPLETE_URL", WEBAPP_URL + "/discord_auth/complete") AUTH_FAIL_URL = getenv("OAUTH_FAIL_URL", WEBAPP_URL + "/discord_auth/failed") diff --git a/discord_login/urls.py b/discord_login/urls.py index c46f3fb..ea0875e 100644 --- a/discord_login/urls.py +++ b/discord_login/urls.py @@ -1,10 +1,12 @@ -from django.urls import path +from django.urls import re_path -from discord_login.views import discord_login, discord_auth_done +from discord_login.views import discord_login, discord_auth_done, discord_auth_complete, discord_auth_failed urlpatterns = [ - path('login/', discord_login, name='discord_login'), - path('done/', discord_auth_done, name='discord_auth_done') + re_path("login/?", discord_login, name="discord_login"), + re_path("done/?", discord_auth_done, name="discord_auth_done"), + re_path("complete/?", discord_auth_complete, name="discord_auth_complete"), + re_path("failed/?", discord_auth_failed, name="discord_auth_failed") # path('logout/', discord_logout, name='logout'), ] diff --git a/discord_login/views.py b/discord_login/views.py index cf1eb6e..8d2bb4d 100644 --- a/discord_login/views.py +++ b/discord_login/views.py @@ -1,11 +1,12 @@ import requests -from django.contrib.auth import authenticate, login +from django.contrib.auth import authenticate, login, logout from django.http import JsonResponse from django.shortcuts import redirect from rest_framework.status import * from rest_framework.views import Request from discord_bot.logs import logger as log + from config.settings import DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET, DISCORD_GUILDS from config.settings import AUTH_COMPLETE_URL, AUTH_FAIL_URL, AUTH_REDIRECT_URL @@ -17,6 +18,12 @@ def discord_login(request: Request) -> redirect: return redirect(auth_url_discord) +def discord_logout(request: Request) -> JsonResponse: + """Invalidate the user's current session""" + logout(request) + return JsonResponse({"message": "logged out"}, status=HTTP_200_OK) + + def exchange_code(code: str): """Exchange the code supplied for a longer term token""" data = { @@ -58,4 +65,20 @@ def discord_auth_done(request: Request) -> JsonResponse: if discord_user: login(request, discord_user) return redirect(AUTH_COMPLETE_URL) + else: + log.error("User failed authentication") + else: + log.error("Unable to exchange supplied code for token") + else: + log.error("Unable to get code from request") return redirect(AUTH_FAIL_URL) + + +def discord_auth_complete(request: Request) -> JsonResponse: + """fallback view for showing a successful login""" + return JsonResponse({"message": "Authenticated via discord"}, status=HTTP_200_OK) + + +def discord_auth_failed(request: Request) -> JsonResponse: + """fallback view for showing a failed login""" + return JsonResponse({"message": "Authentication failed"}, status=HTTP_401_UNAUTHORIZED)