aboutsummaryrefslogtreecommitdiffstats
path: root/backend/routes/auth/authorize.py
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2020-10-25 19:50:43 +0000
committerGravatar Joe Banks <[email protected]>2020-10-25 19:50:43 +0000
commit176a012f4b70cfb89c93af954a410afc86e42835 (patch)
tree2fb08d74ea238ad224069d299812d708af86edbe /backend/routes/auth/authorize.py
parentAdd route class and route manager for dynamic route loading (diff)
Add some basic routes
Diffstat (limited to 'backend/routes/auth/authorize.py')
-rw-r--r--backend/routes/auth/authorize.py31
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()
+ })