define fixed pin auth mode
All checks were successful
Build and push image for doorman / docker (push) Successful in 44s
All checks were successful
Build and push image for doorman / docker (push) Successful in 44s
This commit is contained in:
parent
b696380d34
commit
d218d95104
13
packages/server/src/middlewares/DoorAuthModes.ts
Normal file
13
packages/server/src/middlewares/DoorAuthModes.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { RequestHandler } from "express";
|
||||||
|
import { getRedisClient } from "../clients/db/RedisDbProvider";
|
||||||
|
|
||||||
|
const client = await getRedisClient();
|
||||||
|
|
||||||
|
export const ValidQueryParamAuth: RequestHandler = async (req, res, next) => {
|
||||||
|
if (req.query['key'] !== '123') {
|
||||||
|
res.status(401).json({ msg: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
||||||
@ -1,23 +1,13 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { getRedisClient } from "../clients/db/RedisDbProvider";
|
import { getRedisClient } from "../clients/db/RedisDbProvider";
|
||||||
import { IDoor } from "../types/IDoor";
|
import { RedisKeys, concatKeys, doorStatusKey } from "../types/RedisKeys";
|
||||||
import { RedisKeys, concatKeys } from "../types/RedisKeys";
|
import { ValidQueryParamAuth } from "../middlewares/DoorAuthModes";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const client = await getRedisClient();
|
const client = await getRedisClient();
|
||||||
|
|
||||||
router.post('/:id', async(req, res) => {
|
|
||||||
const door: IDoor = req.body;
|
|
||||||
|
|
||||||
await client.getClient().hSet(concatKeys(RedisKeys.DOORS, req.params.id), {
|
|
||||||
...door
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200).json(door);
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/:id/status', async(req, res) => {
|
router.get('/:id/status', async(req, res) => {
|
||||||
const isOpen = await client.getClient().exists(concatKeys(RedisKeys.DOORS, req.params.id, 'open'));
|
const isOpen = await client.getClient().exists(doorStatusKey(req.params.id));
|
||||||
|
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
res.status(200).json({ status: 'open' });
|
res.status(200).json({ status: 'open' });
|
||||||
@ -27,4 +17,12 @@ router.get('/:id/status', async(req, res) => {
|
|||||||
res.status(401).json({ status: 'closed' });
|
res.status(401).json({ status: 'closed' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.all('/:id/auth', ValidQueryParamAuth, async(req, res) => {
|
||||||
|
const statusKey = doorStatusKey(req.params.id);
|
||||||
|
|
||||||
|
await client.put(statusKey, 'true');
|
||||||
|
await client.getClient().expire(statusKey, Bun.env.DOOR_OPEN_TIMEOUT);
|
||||||
|
res.status(200).json({ msg: `Opened the door "${req.params.id}" for ${Bun.env.DOOR_OPEN_TIMEOUT}s` });
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
@ -3,5 +3,7 @@ declare module "bun" {
|
|||||||
CHALLENGE_EXPIRE_MS: number;
|
CHALLENGE_EXPIRE_MS: number;
|
||||||
BASE_DOMAIN: string;
|
BASE_DOMAIN: string;
|
||||||
REDIS_CONNECT_URL: string; // `redis[s]://[[username][:password]@][host][:port][/db-number]`
|
REDIS_CONNECT_URL: string; // `redis[s]://[[username][:password]@][host][:port][/db-number]`
|
||||||
|
DOOR_OPEN_TIMEOUT: number;
|
||||||
|
DOOR_FIXED_PIN: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +0,0 @@
|
|||||||
export interface IDoor {
|
|
||||||
id: string;
|
|
||||||
open?: string;
|
|
||||||
}
|
|
||||||
@ -3,6 +3,10 @@ export enum RedisKeys {
|
|||||||
DOORS = "doors"
|
DOORS = "doors"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function doorStatusKey(id: string) {
|
||||||
|
return concatKeys(RedisKeys.DOORS, id, 'open');
|
||||||
|
}
|
||||||
|
|
||||||
export function concatKeys(...keys: String[]) {
|
export function concatKeys(...keys: String[]) {
|
||||||
return keys.join(':');
|
return keys.join(':');
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user