35 lines
1.0 KiB
TypeScript
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;
|
|
} |