diff options
Diffstat (limited to 'backend/routes/auth')
-rw-r--r-- | backend/routes/auth/authorize.py | 31 |
1 files changed, 31 insertions, 0 deletions
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() + }) |