-
Notifications
You must be signed in to change notification settings - Fork 3
feat: logs for remote access #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
25d38f2
1cb9722
5b54612
ea3e59d
1c13629
a518937
4b3dbbb
b372fc3
a104cca
cfc75b1
313b184
e0de99a
7da12c8
6a6b81f
6a32460
6f1d8f2
2e907fa
a0f10b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,34 @@ | ||
| import { Router } from "express"; | ||
| import { doorHeartbeats } from "../state.js"; | ||
| import { checkAccess } from "../access.js"; | ||
| import { checkAccess, recordDoorUnlock } from "../access.js"; | ||
| import { ObjectId } from "mongodb"; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.get("/:doorId/status", (req, res) => { | ||
| function getDoorStatus(doorId){ | ||
| // If it's been more than 1 minute, we assume something is broken... | ||
| const lastHeartbeat = doorHeartbeats.get(req.params.doorId); | ||
| const lastHeartbeat = doorHeartbeats.get(doorId); | ||
| if (lastHeartbeat) { | ||
| res.json({ | ||
| return { | ||
| guess: Date.now() - lastHeartbeat > 1000 * 60 ? "offline" : "online", | ||
| lastHeartbeat, | ||
| }); | ||
| }; | ||
| } else { | ||
| res.json({ | ||
| return { | ||
| guess: "offline", | ||
| lastHeartbeat: 0, | ||
| }); | ||
| }; | ||
| } | ||
| } | ||
|
Comment on lines
+8
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call |
||
|
|
||
| function isDoorOffline(doorId) { | ||
| const lastHeartbeat = doorHeartbeats.get(doorId); | ||
| if (!lastHeartbeat) return true; | ||
| return Date.now() - lastHeartbeat > 1000 * 60; | ||
| } | ||
|
|
||
| router.get("/:doorId/status", (req, res) => { | ||
| res.json(getDoorStatus(req.params.doorId)); | ||
| }); | ||
|
|
||
| router.get("/", async (req, res) => { | ||
|
|
@@ -46,6 +57,23 @@ router.post("/:doorId/unlock", async (req, res) => { | |
| return res.status(403).json({ message: "Access denied" }); | ||
| } | ||
| } | ||
|
|
||
| if (isDoorOffline(req.params.doorId)) { | ||
| return res.status(400).json({ message: "Door is offline" }); | ||
| } | ||
|
|
||
| if (req.ctx.authMethod === "oidc") { | ||
| const doorId = req.params.doorId; | ||
| const doorDoc = await req.ctx.db.collection("doors").findOne({ _id: req.params.doorId }); | ||
|
|
||
| await recordDoorUnlock(req.ctx.db, { | ||
| doorId: req.params.doorId, | ||
| doorName: doorDoc?.name, | ||
| username: req.ctx.username ?? req.ctx.userId, | ||
| name: req.ctx.name, | ||
| }); | ||
|
Comment on lines
+69
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we only recording when a door is unlocked when the user is authed with SSO and not any other possible method?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh okay it seems like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Physical door taps are logged separately in server.js
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just as a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it is inserted into the collection. It still has a console.log which I was using for debugging. That can be cleaned There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They get logged in the db, if you change the date range locally or on gatekeeper.csh to be when doors were online and people were on floor you can see them
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand this a little better now, but I'm still confused. It seems like there are only two auth methods: secret and oidc. Why would we not want to log for both (and other methods if we ever add them)?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. secret is meant for non-oidc api calls using secrets like GK_DRINK_SECRETS for Drink. It shouldn't be used for unlocking doors because there is no identity attached to it. Good catch W. What we can do instead is check for userId rather than checking for oidc and deny secret from being used and it also leaves room to add more auth methods later. Physical access is handled via MQTT so there won't be any issue there. Thoughts?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that all sounds good. Unconditional log on this endpoint is a must IMO either way |
||
| } | ||
|
|
||
| req.ctx.mqtt.publish(`gk/${req.params.doorId}/unlock`, ""); | ||
| res.status(204).send(null); | ||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.