Return enumerated codes

This commit is contained in:
Nexus 2023-04-09 21:22:55 +01:00
parent eccdaa1274
commit 42682c6378
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -1,4 +1,5 @@
import ipaddress import ipaddress
import sys
import discord import discord
import os import os
@ -8,6 +9,7 @@ from hashlib import sha512
from fastapi import FastAPI, HTTPException, Request from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse, RedirectResponse, HTMLResponse from fastapi.responses import JSONResponse, RedirectResponse, HTMLResponse
from http import HTTPStatus
from utils import Student, get_or_none, VerifyCode, console, BannedStudentID from utils import Student, get_or_none, VerifyCode, console, BannedStudentID
from config import guilds from config import guilds
@ -68,6 +70,7 @@ async def authenticate(req: Request, code: str = None, state: str = None):
if not (code and state) or state not in app.state.states: if not (code and state) or state not in app.state.states:
value = os.urandom(4).hex() value = os.urandom(4).hex()
if value in app.state.states: if value in app.state.states:
print("Generated a state that already exists. Cleaning up", file=sys.stderr)
# remove any states older than 5 minutes # remove any states older than 5 minutes
for _value in list(app.state.states): for _value in list(app.state.states):
if (datetime.now() - app.state.states[_value]).total_seconds() > 300: if (datetime.now() - app.state.states[_value]).total_seconds() > 300:
@ -82,7 +85,7 @@ async def authenticate(req: Request, code: str = None, state: str = None):
redirect_uri=OAUTH_REDIRECT_URI, redirect_uri=OAUTH_REDIRECT_URI,
scopes=('identify',) scopes=('identify',)
) + f"&state={value}&prompt=none", ) + f"&state={value}&prompt=none",
status_code=301, status_code=HTTPStatus.TEMPORARY_REDIRECT,
headers={ headers={
"Cache-Control": "no-store, no-cache" "Cache-Control": "no-store, no-cache"
} }
@ -130,7 +133,7 @@ async def authenticate(req: Request, code: str = None, state: str = None):
student = await get_or_none(Student, user_id=user["id"]) student = await get_or_none(Student, user_id=user["id"])
if not student: if not student:
raise HTTPException( raise HTTPException(
status_code=404, status_code=HTTPStatus.NOT_FOUND,
detail="Student not found. Please run /verify first." detail="Student not found. Please run /verify first."
) )
@ -148,7 +151,7 @@ async def authenticate(req: Request, code: str = None, state: str = None):
data = response.json() data = response.json()
if data["status"] != "success": if data["status"] != "success":
raise HTTPException( raise HTTPException(
status_code=500, status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=f"Failed to get IP data for {req.client.host}: {data}." detail=f"Failed to get IP data for {req.client.host}: {data}."
) )
else: else: