slack /open-xxx from inventory-app

This commit is contained in:
2025-08-08 05:15:29 +03:00
parent eeeb5ecace
commit f5cfb3454a
5 changed files with 125 additions and 11 deletions

View File

@@ -4,14 +4,13 @@ from datetime import date, datetime
from functools import wraps
from typing import List
import kube
from dateutil.parser import parse
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.errors import PyMongoError
from sanic import Sanic
from sanic.response import json, text
from sanic_prometheus import monitor
import kube
from slack import slack_app
app = Sanic(__name__)
@@ -53,6 +52,7 @@ def authenticate_door(wrapped):
return decorator(wrapped)
# Door controllers query uid_hashes for offline use. There is no online uid_hash authn/z, only logging.
@app.route("/allowed")
@authenticate_door
async def view_doorboy_uids(request):
@@ -73,7 +73,7 @@ async def view_doorboy_uids(request):
"inventory.owner.username": {"$in": users},
}
prj = {"token.uid_hash": True}
tokens = await app.ctx.db.inventory.find(flt, prj)
tokens = await request.app.ctx.db.inventory.find(flt, prj)
print(f"delegating {len(tokens)} doorkey tokens")
return json({"allowed_uids": tokens})
@@ -131,6 +131,7 @@ async def view_open_door_events(request):
return json(transformed, default=datetime_to_json_formatting)
# Door controllers listen for open events (web, slack).
@app.route("/longpoll", stream=True)
@authenticate_door
async def view_longpoll(request):
@@ -144,7 +145,7 @@ async def view_longpoll(request):
}
]
try:
async with app.ctx.db.eventlog.watch(pipeline) as stream:
async with request.app.ctx.db.eventlog.watch(pipeline) as stream:
await response.send("data: watch-stream-opened\n\n")
async for event in stream:
ev = event["fullDocument"]
@@ -160,7 +161,7 @@ async def view_longpoll(request):
return
# Called by the door to log a card swipe. Does not decide whether the door should be opened.
# Door controller reporting a card swipe. No authn/z about the event (done offline on door controller), only logging.
@app.post("/swipe")
@authenticate_door
async def swipe(request):
@@ -183,7 +184,7 @@ async def swipe(request):
)
# Update token, create if unknown
await app.ctx.db.inventory.update_one(
await request.app.ctx.db.inventory.update_one(
{"component": "doorboy", "type": "token", "token.uid_hash": data["uid_hash"]},
{
"$set": {"last_seen": timestamp},
@@ -197,7 +198,7 @@ async def swipe(request):
upsert=True,
)
token = await app.ctx.db.inventory.find_one(
token = await request.app.ctx.db.inventory.find_one(
{"component": "doorboy", "type": "token", "token.uid_hash": data["uid_hash"]}
)
@@ -215,7 +216,7 @@ async def swipe(request):
},
"uid_hash": data["uid_hash"],
}
await app.ctx.db.eventlog.insert_one(event_swipe)
await request.app.ctx.db.eventlog.insert_one(event_swipe)
return text("ok")