Change swipe endpoint to accept json
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2023-08-10 22:56:54 +03:00
parent 9aadb8f24d
commit 9469324da9

View File

@ -211,59 +211,57 @@ class FormSwipe(FlaskForm):
@page_doorboy.route("/m/doorboy/swipe", methods=["POST"]) @page_doorboy.route("/m/doorboy/swipe", methods=["POST"])
def view_swipe(): def view_swipe():
form = FormSwipe(request.form) form = request.json
now = datetime.utcnow() now = datetime.utcnow()
if form.validate_on_submit(): # Make sure token exists
# Make sure token exists db.inventory.update_one({
db.inventory.update_one({ "type": "token",
"type": "token", "component": "doorboy",
"token.uid_hash": form["uid_hash"]
}, {
"$set": {
"last_seen": form.get("timestamp", now)
},
"$setOnInsert": {
"component": "doorboy", "component": "doorboy",
"token.uid_hash": form.uid_hash.data "type": "token",
}, { "first_seen": now,
"$set": { "inventory": {
"last_seen": form.timestamp.data or now, "claimable": True,
},
"$setOnInsert": {
"component": "doorboy",
"type": "token",
"first_seen": now,
"inventory": {
"claimable": True,
}
} }
}, upsert=True)
# Fetch token to read owner
token = db.inventory.find_one({
"type": "token",
"component": "doorboy",
"token.uid_hash": form.uid_hash.data
})
event_swipe = {
"component": "doorboy",
"timestamp": form.timestamp.data,
"door": form.door.data,
"event": "card-swiped",
"success": form.success.data,
"token": {
"uid_hash": form.uid_hash.data
},
"inventory": {}
} }
}, upsert=True)
if token.get("inventory", {}).get("owner", {}).get("username", None): # Fetch token to read owner
event_swipe["inventory"]["owner_id"] = token["inventory"]["owner"]["username"] token = db.inventory.find_one({
"type": "token",
"component": "doorboy",
"token.uid_hash": form["uid_hash"]
})
db.eventlog.insert_one(event_swipe) event_swipe = {
"component": "doorboy",
"timestamp": form["timestamp"]
"door": form["door"]
"event": "card-swiped",
"success": form["success"]
"token": {
"uid_hash": form["uid_hash"]
},
"inventory": {}
}
status = "Permitted" if form.success.data else "Denied" if token.get("inventory", {}).get("owner", {}).get("username", None):
username = token.get("inventory", {}).get("owner", {}).get("username", None) event_swipe["inventory"]["owner_id"] = token["inventory"]["owner"]["username"]
if username and username in users_lookup:
subject = users_lookup[username].display_name or username db.eventlog.insert_one(event_swipe)
else:
subject = "Unknown" status = "Permitted" if form["success"] else "Denied"
msg = "%s %s door access for %s identified by keycard/keyfob" % (status, form.door.data, subject) username = token.get("inventory", {}).get("owner", {}).get("username", None)
spam(msg) if username and username in users_lookup:
return "ok" subject = users_lookup[username].display_name or username
raise ValueError() else:
subject = "Unknown"
msg = "%s %s door access for %s identified by keycard/keyfob" % (status, form["door"], subject)
spam(msg)
return "ok"