From 176a012f4b70cfb89c93af954a410afc86e42835 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Sun, 25 Oct 2020 19:50:43 +0000 Subject: Add some basic routes --- backend/routes/auth/authorize.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 backend/routes/auth/authorize.py (limited to 'backend/routes/auth/authorize.py') diff --git a/backend/routes/auth/authorize.py b/backend/routes/auth/authorize.py new file mode 100644 index 0000000..0a90856 --- /dev/null +++ b/backend/routes/auth/authorize.py @@ -0,0 +1,31 @@ +""" +Use a token received from the Discord OAuth2 system to fetch user information. +""" + +import jwt +from starlette.responses import JSONResponse + +from backend.constants import SECRET_KEY +from backend.route import Route +from backend.discord import fetch_bearer_token, fetch_user_details + + +class AuthorizeRoute(Route): + """ + Use the authorization code from Discord to generate a JWT token. + """ + + name = "authorize" + path = "/authorize" + + async def post(self, request): + data = await request.json() + + bearer_token = await fetch_bearer_token(data["token"]) + user_details = await fetch_user_details(bearer_token["access_token"]) + + token = jwt.encode(user_details, SECRET_KEY, algorithm="HS256") + + return JSONResponse({ + "token": token.decode() + }) -- cgit v1.2.3