apxtrib/api/middlewares/checkHeaders.js

97 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-05-16 08:31:27 +00:00
const conf = require(`${process.env.dirtown}/conf.json`);
2023-05-12 05:59:32 +00:00
/**
2023-11-19 15:34:37 +00:00
* @api{get}/CheckHeaders
* @apiGroup Middlewares
* @apiName CheckHeaders
* @apiDescription a list of header is mandatory to access apxtrib see tribes/townconf.json.exposedHeaders
2023-05-12 05:59:32 +00:00
*
2023-11-19 15:34:37 +00:00
* @apiHeader {string} xalias anonymous or unique alias
* @apiHeader {string} xapp name of the webapp store in tribe/tribeid/www/xapp
* @apiHeader {string} xlang the 2 letter request langage (if does not exist then return en = english).
* @apiHeader {string} xtribe unique tribe name ere xapp exist
* @apiHeader {string} xdays a timestamp 0 or generate during the authentifyme process
* @apiHeader {string} xhash anonymous or signature of message: xalias_xdays created by alias private key during authentifyme process
* @apiHeader {array[]} xprofils list of string profil apply into xtribe for xapp
* @apiHeader {string} xuuid a unique number c reated the fisrt time a domain is visited
* @apiHeader {integer} xtrkversion a version number link to tracking system
2023-05-12 05:59:32 +00:00
*
* @apiError missingexposedHeaders it miss an exposedHeaders
* @apiErrorExample {json} Error-Response:
* HTTP/1/1 400 Not Found
* {
* status:400,
2023-11-05 11:03:25 +00:00
* ref:"middlewares"
2023-05-12 05:59:32 +00:00
* msg:"missingheaders",
2023-11-05 11:03:25 +00:00
* data: ["headermissing1"]
2023-05-12 05:59:32 +00:00
* }
*@apiErrorExample {json} Error-Response:
* HTTP/1/1 404 Not Found
* {
* status:404,
2023-11-05 11:03:25 +00:00
* ref:"middlewares"
2023-05-12 05:59:32 +00:00
* msg:"tribeiddoesnotexist",
2023-11-05 11:03:25 +00:00
* data: {xalias}
2023-05-12 05:59:32 +00:00
* }
* @apiHeaderExample {json} Header-Exemple:
* {
* xtribe:"apache",
* xalias:"toto",
* xhash:"",
* xdays:"123"
* xlang:"en",
* xapp:"popular"
* }
*/
2023-11-19 15:34:37 +00:00
const checkHeaders = (req, res, next) => {
2023-05-12 05:59:32 +00:00
req.session = {};
const header = {};
if (!req.header("xlang") && req.header("Content-Language"))
req.params.xlang = req.header("Content-Language");
let missingheader = [];
2023-06-28 13:23:17 +00:00
//console.log("req.headers", req.headers);
2023-05-12 05:59:32 +00:00
for (const h of conf.api.exposedHeaders) {
//console.log( h, req.header( h ) )
if (req.params[h]) {
header[h] = req.params[h];
} else if (req.header(h)) {
header[h] = req.header(h);
} else {
missingheader.push(h);
}
}
2023-11-05 11:03:25 +00:00
// console.log( 'pass header', header )
2023-05-12 05:59:32 +00:00
// store in session the header information
req.session.header = header;
// Each header have to be declared
if (missingheader != "") {
// bad request
return res.status(400).json({
2023-11-05 11:03:25 +00:00
ref: "middlewares",
2023-05-12 05:59:32 +00:00
msg: "missingheader",
data: missingheader,
});
}
//console.log( req.app.locals.tribeids )
// xtribe == "town" is used during the setup process
2023-11-05 11:03:25 +00:00
// xtribe == "adminapi" is used to access /adminapi
2023-05-12 05:59:32 +00:00
if (
!(
2023-11-05 11:03:25 +00:00
["town","adminapi"].includes(header.xtribe) || req.app.locals.tribeids.includes(header.xtribe)
2023-05-12 05:59:32 +00:00
)
) {
return res.status(404).json({
2023-11-05 11:03:25 +00:00
ref: "middlewares",
2023-05-12 05:59:32 +00:00
msg: "tribeiddoesnotexist",
data: { xtribe: header.xtribe },
});
}
if (!conf.api.languages.includes(header.xlang)) {
console.log("warning language requested does not exist force to english");
header.xlang = "en";
}
2023-11-05 11:03:25 +00:00
//set anonymous profil
req.session.header.xprofils=["anonymous"]
2023-05-12 05:59:32 +00:00
next();
2023-04-13 05:46:35 +00:00
};
module.exports = checkHeaders;