From 094eafa9dafbab15f20c323980bc38dfcb07c0a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madis=20M=C3=A4gi?= Date: Fri, 18 Aug 2023 04:08:02 +0300 Subject: [PATCH] Add swipe proxy endpoint --- app/doorboy-proxy.py | 24 ++++++++++++++++++++++++ docker-compose.yml | 1 + 2 files changed, 25 insertions(+) diff --git a/app/doorboy-proxy.py b/app/doorboy-proxy.py index 1182a93..92496e4 100755 --- a/app/doorboy-proxy.py +++ b/app/doorboy-proxy.py @@ -84,6 +84,30 @@ async def view_longpoll(request): await response.send("data: response-generator-ended\n\n") return +@app.post("/swipe") +async def forward_swipe(request): + key = request.headers.get("KEY") + if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]: + return text("Invalid token", status=401) + data = request.json + doors = [] + if key == DOORBOY_SECRET_FLOOR: + doors.extend(["backdoor, frontdoor, grounddoor"]) + if key == DOORBOY_SECRET_WORKSHOP: + doors.append("workshopdoor") + if data.get("door") not in doors: + return text("Not allowed", 403) + + async with httpx.AsyncClient() as client: + r = await client.post(SWIPE_URI, json=data, headers={ + "Content-Type": "application/json", + "Authorization": f"Basic {INVENTORY_API_KEY}" + }) + if r.status_code == 200: + return text("ok") + else: + return text("Failed", 500) + if __name__ == "__main__": app.run(debug=False, host="0.0.0.0", port=5000, single_process=True, access_log=True) diff --git a/docker-compose.yml b/docker-compose.yml index 02ba561..c216584 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,5 +30,6 @@ services: FLOOR_ACCESS_GROUP: "k-space:floor" WORKSHOP_ACCESS_GROUP: "k-space:workshop" CARD_URI: "https://inventory-app-72zn4.codemowers.ee/cards" + SWIPE_URI: "https://inventory-app-72zn4.codemowers.ee/m/doorboy/swipe" build: context: .