doorboy-proxy/app/doorboy-proxy.py

116 lines
3.8 KiB
Python
Raw Permalink Normal View History

2022-12-17 10:46:39 +00:00
#!/usr/bin/env python3
2022-02-21 20:10:57 +00:00
from sanic import Sanic
from sanic.response import text, json
2022-12-17 10:46:39 +00:00
from sanic_prometheus import monitor
2021-04-02 09:10:34 +00:00
from motor.motor_asyncio import AsyncIOMotorClient
import httpx
2021-04-02 09:10:34 +00:00
import pymongo
2021-03-17 07:58:03 +00:00
import os
2021-04-02 09:10:34 +00:00
app = Sanic(__name__)
2022-12-17 10:46:39 +00:00
monitor(app).expose_endpoint()
2021-04-02 09:10:34 +00:00
2023-08-01 21:32:17 +00:00
INVENTORY_API_KEY = os.environ["INVENTORY_API_KEY"]
2023-08-01 19:21:57 +00:00
DOORBOY_SECRET_FLOOR = os.environ["DOORBOY_SECRET_FLOOR"]
DOORBOY_SECRET_WORKSHOP = os.environ["DOORBOY_SECRET_WORKSHOP"]
2023-07-30 10:17:14 +00:00
CARD_URI = os.environ["CARD_URI"]
2023-08-01 19:21:57 +00:00
FLOOR_ACCESS_GROUP = os.environ["FLOOR_ACCESS_GROUP"]
WORKSHOP_ACCESS_GROUP = os.environ["WORKSHOP_ACCESS_GROUP"]
2023-08-10 17:46:38 +00:00
MONGO_URI = os.environ["MONGO_URI"]
2023-08-24 17:35:07 +00:00
SWIPE_URI = os.environ["SWIPE_URI"]
2021-03-17 07:58:03 +00:00
2023-08-01 19:21:57 +00:00
assert len(DOORBOY_SECRET_FLOOR) >= 10
assert len(DOORBOY_SECRET_WORKSHOP) >= 10
2021-03-17 07:58:03 +00:00
2021-10-26 17:17:48 +00:00
@app.listener("before_server_start")
async def setup_db(app, loop):
# TODO: find cleaner way to do this, for more see
# https://github.com/sanic-org/sanic/issues/919
2022-02-21 20:10:57 +00:00
app.ctx.db = AsyncIOMotorClient(MONGO_URI).get_default_database()
2021-10-26 17:17:48 +00:00
2021-03-17 07:58:03 +00:00
@app.route("/allowed")
2021-04-02 09:10:34 +00:00
async def view_doorboy_uids(request):
2023-08-01 19:21:57 +00:00
key = request.headers.get("KEY")
2023-08-01 20:02:58 +00:00
if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]:
2021-04-03 14:09:51 +00:00
return text("how about no")
2023-08-10 16:42:18 +00:00
groups = []
2023-08-01 19:21:57 +00:00
if key == DOORBOY_SECRET_FLOOR:
2023-08-10 16:42:18 +00:00
groups.append(FLOOR_ACCESS_GROUP)
if key == DOORBOY_SECRET_WORKSHOP:
groups.append(WORKSHOP_ACCESS_GROUP)
if not groups:
2023-08-01 19:21:57 +00:00
return "fail", 500
async with httpx.AsyncClient() as client:
2023-08-01 19:21:57 +00:00
r = await client.post(CARD_URI, json={
2023-08-10 16:42:18 +00:00
"groups": groups
2023-08-01 21:32:17 +00:00
}, headers={
"Content-Type": "application/json",
"Authorization": f"Basic {INVENTORY_API_KEY}"
})
j = r.json()
2021-03-17 07:58:03 +00:00
allowed_uids = []
for obj in j:
allowed_uids.append({
"token": obj["token"]
})
2021-04-03 14:09:51 +00:00
return json({"allowed_uids": allowed_uids})
2021-03-17 07:58:03 +00:00
2022-02-21 20:10:57 +00:00
@app.route("/longpoll", stream=True)
2021-04-02 09:10:34 +00:00
async def view_longpoll(request):
2023-08-10 16:35:43 +00:00
key = request.headers.get("KEY")
2023-08-01 20:02:58 +00:00
if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]:
2022-02-21 20:10:57 +00:00
return text("Invalid token")
2023-08-10 17:45:24 +00:00
response = await request.respond(content_type="text/event-stream")
2022-02-21 20:10:57 +00:00
await response.send("data: response-generator-started\n\n")
pipeline = [
{
"$match": {
"operationType": "insert",
}
}
]
try:
async with app.ctx.db.eventlog.watch(pipeline) as stream:
await response.send("data: watch-stream-opened\n\n")
async for event in stream:
2023-08-11 15:20:01 +00:00
if event["fullDocument"].get("type") == "open-door" and event["fullDocument"].get("approved", False):
2022-02-21 20:10:57 +00:00
await response.send("data: %s\n\n" %
event["fullDocument"]["door"])
2023-08-10 17:45:24 +00:00
except pymongo.errors.PyMongoError as e:
print(e)
await response.send("data: response-generator-ended\n\n")
2022-02-21 20:10:57 +00:00
return
2021-03-17 07:58:03 +00:00
2023-08-18 01:08:02 +00:00
@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
2023-09-05 14:41:58 +00:00
doors = set()
2023-08-18 01:08:02 +00:00
if key == DOORBOY_SECRET_FLOOR:
2023-09-05 14:41:58 +00:00
doors.update(["backdoor", "frontdoor", "grounddoor"])
2023-08-18 01:08:02 +00:00
if key == DOORBOY_SECRET_WORKSHOP:
2023-09-05 14:41:58 +00:00
doors.add("workshopdoor")
2023-08-18 01:08:02 +00:00
if data.get("door") not in doors:
2023-09-05 14:41:58 +00:00
print("Door", repr(data.get("door")), "not in", doors)
2023-08-18 01:08:02 +00:00
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)
2021-03-17 07:58:03 +00:00
2022-02-21 20:10:57 +00:00
if __name__ == "__main__":
2022-12-17 13:24:13 +00:00
app.run(debug=False, host="0.0.0.0", port=5000, single_process=True, access_log=True)