Martin Dimitrov 4c6399f0c3
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
change to env var config
2024-02-27 21:31:32 -08:00

35 lines
1.0 KiB
TypeScript

import { Request, RequestHandler } from "express";
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();
export const HandleAuthMode: RequestHandler = async (req, res, next) => {
const authModes = getAuthModes(req.params.id);
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;
}
next();
}
const handleFixedPinAuth = (req: Request): boolean => {
const fixedPin = getDoorSettingString(req.params.id, IDoorConfig.FIXED_PIN);
return fixedPin !== undefined && req.query['key'] === fixedPin;
}