@szegedsw/lib-node
Version:
A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.
44 lines • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.systemAuditLog = void 0;
const config_1 = require("../config/config");
const request_1 = require("../express/request");
const try_1 = require("../functions/try");
exports.systemAuditLog = (auditLogCb, systemLogCb) => async (req, res, next) => {
await try_1._try(res, async () => {
const start = process.hrtime();
let url = req.originalUrl.split("?")[0];
const method = req.method;
res.on("finish", async () => {
const requestBody = request_1.requestParams(req);
const type = requestBody.type;
await try_1._try(res, async () => {
if (req.params && Object.keys(req.params).includes("_id")) {
url = url.split("/").reverse().splice(1).reverse().join("/");
}
if (config_1.env.auditLogMethods.includes(method)) {
Object.keys(requestBody).forEach((key) => {
if ((config_1.env.maskPassKeys || ["PASSWORD"]).some((pass) => key.toUpperCase().includes(pass.toUpperCase()))) {
requestBody[key] = "********";
}
});
// tslint:disable-next-line: max-line-length tslint:disable-next-line: no-string-literal
await auditLogCb({
req,
sessionId: req.sessionID || "",
url,
method,
requestBody,
status: res.statusCode,
responseBody: res.statusMessage,
});
}
// tslint:disable-next-line: no-magic-numbers
const msec = process.hrtime(start)[1] / 1000000;
await systemLogCb({ req, sessionId: req.sessionID || "", url, method, type, msec });
});
});
next();
});
};
//# sourceMappingURL=system.audit.log.js.map