Skip to content

Commit

Permalink
304 bugfix remove player (OperationXen#338)
Browse files Browse the repository at this point in the history
* Wrong command context fixed

* Corrected call to player utils

* Fixed duplicated "in"

* Improvements to discord auth logic
  • Loading branch information
OperationXen authored Jan 14, 2024
1 parent 08c8560 commit 7df3014
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 6 additions & 4 deletions discord_login/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
]
25 changes: 24 additions & 1 deletion discord_login/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 = {
Expand Down Expand Up @@ -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)

0 comments on commit 7df3014

Please sign in to comment.