apxtrib/api/middlewares/checkHeaders.js

103 lines
3.8 KiB
JavaScript
Raw Permalink 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-12-05 06:42:35 +00:00
* @api {get} http://header/CheckHeaders - CheckHeaders
2023-11-19 15:34:37 +00:00
* @apiGroup Middlewares
* @apiName CheckHeaders
2023-12-05 06:42:35 +00:00
* @apiDescription a list of headers are mandatory to access apxtrib see in your space town /conf.json.exposedHeaders
2023-05-12 05:59:32 +00:00
*
2023-12-05 06:42:35 +00:00
* @apiHeader {string} xalias 'anonymous' or unique alias
* @apiHeader {string} xapp name of the webapp store in tribe/tribeid/www/{xapp}
2023-11-19 15:34:37 +00:00
* @apiHeader {string} xlang the 2 letter request langage (if does not exist then return en = english).
2023-12-05 06:42:35 +00:00
* @apiHeader {string} xtribe unique tribe name where xapp exist
2023-11-19 15:34:37 +00:00
* @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
2023-12-05 06:42:35 +00:00
* @apiHeader {string} xuuid a unique number uuid.v4 created the fisrt time a domain is visited on a device
2023-11-19 15:34:37 +00:00
* @apiHeader {integer} xtrkversion a version number link to tracking system
2023-05-12 05:59:32 +00:00
*
2023-12-05 06:42:35 +00:00
* @apiHeaderExample {json} Header-Example:
* {
* Cache-Control: "no-cache",
* Expires: 0, Pragma:"no-cache",
* xalias:"jojo",
* xapp:"presentation",
* xdays:1700733068298
* xhash:"LS0tLS1CRUdJTiBQR1AgU0lHTkVEIE1FU1NBR0UtLS0tLQpIYXNoOiBTSEE1MTIKCmpvam9fMTcwMDczMzA2ODI5OAotLS0tLUJFR0lOIFBHUCBTSUdOQVRVUkUtLS0tLQoKd25VRUFSWUtBQ2NGZ21WZklJd0prTmFVQ0daRHVUYnBGaUVFTjZlc1ZMSWdURmtPRGFVaDFwUUlaa081Ck51a0FBR09MQVA5OS96c21YeEd0b0VuYnpnekppZDJMcDA3YlBNZ1gwNUdhOUFVWjlCQm91Z0VBOVlYVworYjZIM2JHWHVhbEVOc3BrdUk1alNlTFNUWGNkSStjTExTZk5OQTg9Cj1uVjhNCi0tLS0tRU5EIFBHUCBTSUdOQVRVUkUtLS0tLQo=",
* xlang:"fr",
* xprofils:["anonymous", "pagans"],
* xtribe:"smatchit",
* xtrkversion:1,
* xuuid:"ea1cf73f-27f5-4c69-ab53-197a0feab9b2"
* }
* @apiErrorExample {json} Error-Response:
* HTTP/1/1 400 Not Found
* {
* status:400,
* ref:"middlewares",
* msg:"missingheaders",
* data:["headermissing1"]
* }
* @apiErrorExample {json} Error-Response:
* HTTP/1/1 404 Not Found
* {
* status:404,
* ref:"middlewares"
* msg:"tribeiddoesnotexist",
* data: {xalias}
* }
*/
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-12-05 06:42:35 +00:00
status:400,
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-12-05 06:42:35 +00:00
status:404,
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;