change to env var config
All checks were successful
Build and push image for doorman / docker (push) Successful in 55s
Build and push image for doorman / deploy-portainer (push) Successful in 32s

This commit is contained in:
Martin Dimitrov 2024-02-27 21:31:32 -08:00
parent 3510f1e031
commit 4c6399f0c3
5 changed files with 72 additions and 9 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -0,0 +1,3 @@
export enum IAuthMode {
FIXED_PIN = "FIXED_PIN",
}

View 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",
}

View 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");
};