diff options
author | 2020-10-25 19:50:43 +0000 | |
---|---|---|
committer | 2020-10-25 19:50:43 +0000 | |
commit | 176a012f4b70cfb89c93af954a410afc86e42835 (patch) | |
tree | 2fb08d74ea238ad224069d299812d708af86edbe /backend/routes | |
parent | Add route class and route manager for dynamic route loading (diff) |
Add some basic routes
Diffstat (limited to 'backend/routes')
-rw-r--r-- | backend/routes/auth/authorize.py | 31 | ||||
-rw-r--r-- | backend/routes/index.py | 24 |
2 files changed, 55 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() + }) diff --git a/backend/routes/index.py b/backend/routes/index.py new file mode 100644 index 0000000..1b5b404 --- /dev/null +++ b/backend/routes/index.py @@ -0,0 +1,24 @@ +""" +Index route for the forms API. +""" + +from starlette.responses import JSONResponse + +from backend.route import Route + + +class IndexRoute(Route): + """ + Return a generic hello world message with some information to the client. + + Can be used as a healthcheck for Kubernetes or a frontend connection check. + """ + + name = "index" + path = "/" + + def get(self, request): + return JSONResponse({ + "message": "Hello, world!", + "client": request.client.host + }) |