1.0.14
This commit is contained in:
@@ -1,23 +1,32 @@
|
||||
import dotenv from "dotenv";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { resolve } from "path";
|
||||
import { logger } from "../server.js";
|
||||
|
||||
dotenv.config({
|
||||
path: resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
|
||||
});
|
||||
|
||||
export default function ValidateImsToken(req: Request, res: Response, next: NextFunction) {
|
||||
const jobid: string = (req.body.jobid || "").trim();
|
||||
try {
|
||||
const IMS_TOKEN: string = (process.env.IMS_TOKEN || "").trim();
|
||||
|
||||
const IMS_TOKEN: string = (process.env.IMS_TOKEN || "").trim();
|
||||
|
||||
if (IMS_TOKEN === "") {
|
||||
next();
|
||||
} else {
|
||||
if (req.headers.ims_token !== IMS_TOKEN) {
|
||||
res.sendStatus(401);
|
||||
} else {
|
||||
if (!IMS_TOKEN) {
|
||||
logger.debug("IMS_TOKEN not set, skipping token validation.");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
const token = req.headers.ims_token || req.headers["ims-token"] || req.headers["x-ims-token"];
|
||||
if (token !== IMS_TOKEN) {
|
||||
logger.warn("Invalid IMS token provided.", { provided: token });
|
||||
res.sendStatus(401);
|
||||
return;
|
||||
}
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
logger.error("Error validating IMS token.", { error: (error as Error).message });
|
||||
if (!res.headersSent) res.status(500).json({ error: "Error validating IMS token." });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user