change to env var config
This commit is contained in:
parent
3510f1e031
commit
4c6399f0c3
@ -1,13 +1,35 @@
|
|||||||
import { RequestHandler } from "express";
|
import { Request, RequestHandler } from "express";
|
||||||
import { getRedisClient } from "../clients/db/RedisDbProvider";
|
import { getRedisClient } from "../clients/db/RedisDbProvider";
|
||||||
|
import { getAuthModes, getDoorSettingString } from "../util/EnvConfigUtil";
|
||||||
|
import { IAuthMode } from "../types/IAuthMode";
|
||||||
|
import { IDoorConfig } from "../types/IDoorConfig";
|
||||||
|
|
||||||
const client = await getRedisClient();
|
const client = await getRedisClient();
|
||||||
|
|
||||||
export const ValidQueryParamAuth: RequestHandler = async (req, res, next) => {
|
export const HandleAuthMode: RequestHandler = async (req, res, next) => {
|
||||||
if (req.query['key'] !== Bun.env.DOOR_FIXED_PIN) {
|
const authModes = getAuthModes(req.params.id);
|
||||||
res.status(401).json({ msg: "Unauthorized" });
|
|
||||||
|
if (authModes.length === 0) {
|
||||||
|
res.status(404).json({ msg: `Unknown door ${req.params.id}` });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isAuthorized = authModes.some((mode) => {
|
||||||
|
switch(mode) {
|
||||||
|
case IAuthMode.FIXED_PIN: return handleFixedPinAuth(req)
|
||||||
|
default: return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!isAuthorized) {
|
||||||
|
res.status(401).json({ msg: 'Unauthorized' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
}
|
||||||
|
|
||||||
|
const handleFixedPinAuth = (req: Request): boolean => {
|
||||||
|
const fixedPin = getDoorSettingString(req.params.id, IDoorConfig.FIXED_PIN);
|
||||||
|
return fixedPin !== undefined && req.query['key'] === fixedPin;
|
||||||
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { getRedisClient } from "../clients/db/RedisDbProvider";
|
import { getRedisClient } from "../clients/db/RedisDbProvider";
|
||||||
import { doorStatusKey } from "../types/RedisKeys";
|
import { doorStatusKey } from "../types/RedisKeys";
|
||||||
import { ValidQueryParamAuth } from "../middlewares/DoorAuthModes";
|
import { HandleAuthMode } from "../middlewares/DoorAuthModes";
|
||||||
|
import { getDoorSettingNumber, getDoorSettingString } from "../util/EnvConfigUtil";
|
||||||
|
import { IDoorConfig } from "../types/IDoorConfig";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const client = await getRedisClient();
|
const client = await getRedisClient();
|
||||||
@ -12,6 +14,10 @@ router.get('/:id/status', async(req, res) => {
|
|||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
const fingerprint = JSON.parse(isOpen);
|
const fingerprint = JSON.parse(isOpen);
|
||||||
res.status(200).json({ status: 'open', fingerprint });
|
res.status(200).json({ status: 'open', fingerprint });
|
||||||
|
|
||||||
|
if (getDoorSettingString(req.params.id, IDoorConfig.CLOSE_AFTER_POLL)) {
|
||||||
|
await client.remove(doorStatusKey(req.params.id));
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,14 +29,16 @@ router.delete('/:id/status', async(req, res) => {
|
|||||||
res.status(200).json({ msg: `Closed the door ${req.params.id}` });
|
res.status(200).json({ msg: `Closed the door ${req.params.id}` });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.all('/:id/auth', ValidQueryParamAuth, async(req, res) => {
|
router.all('/:id/auth', HandleAuthMode, async(req, res) => {
|
||||||
const statusKey = doorStatusKey(req.params.id);
|
const statusKey = doorStatusKey(req.params.id);
|
||||||
|
|
||||||
const fingerprint = (req as any).fingerprint;
|
const fingerprint = (req as any).fingerprint;
|
||||||
|
|
||||||
|
const timeout = getDoorSettingNumber(req.params.id, IDoorConfig.OPEN_TIMEOUT) || 60;
|
||||||
|
|
||||||
await client.put(statusKey, JSON.stringify(fingerprint));
|
await client.put(statusKey, JSON.stringify(fingerprint));
|
||||||
await client.getClient().expire(statusKey, Bun.env.DOOR_OPEN_TIMEOUT);
|
await client.getClient().expire(statusKey, timeout);
|
||||||
res.status(200).json({ msg: `Opened the door "${req.params.id}" for ${Bun.env.DOOR_OPEN_TIMEOUT}s` });
|
res.status(200).json({ msg: `Opened the door "${req.params.id}" for ${timeout}s` });
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
3
packages/server/src/types/IAuthMode.ts
Normal file
3
packages/server/src/types/IAuthMode.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export enum IAuthMode {
|
||||||
|
FIXED_PIN = "FIXED_PIN",
|
||||||
|
}
|
||||||
6
packages/server/src/types/IDoorConfig.ts
Normal file
6
packages/server/src/types/IDoorConfig.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export enum IDoorConfig {
|
||||||
|
AUTH_MODES = "AUTH_MODES",
|
||||||
|
OPEN_TIMEOUT = "OPEN_TIMEOUT",
|
||||||
|
FIXED_PIN = "FIXED_PIN",
|
||||||
|
CLOSE_AFTER_POLL="CLOSE_AFTER_POLL",
|
||||||
|
}
|
||||||
24
packages/server/src/util/EnvConfigUtil.ts
Normal file
24
packages/server/src/util/EnvConfigUtil.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { IAuthMode } from "../types/IAuthMode";
|
||||||
|
import { IDoorConfig } from "../types/IDoorConfig";
|
||||||
|
|
||||||
|
const doorToEnv = (door: string): string => {
|
||||||
|
return door.toUpperCase().replaceAll(' ', '_').replaceAll('-', '_');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAuthModes = (door: string): IAuthMode[] => {
|
||||||
|
const config = getDoorSettingString(door, IDoorConfig.AUTH_MODES);
|
||||||
|
|
||||||
|
if (config) {
|
||||||
|
return JSON.parse(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDoorSettingString = (door: string, setting: IDoorConfig): string | undefined => {
|
||||||
|
return Bun.env[`${setting}_${doorToEnv(door)}`];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDoorSettingNumber = (door: string, setting: IDoorConfig): number => {
|
||||||
|
return parseInt(getDoorSettingString(door, setting) || "0");
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user