2023-05-16 10:31:27 +02:00
|
|
|
const conf = require(`${process.env.dirtown}/conf.json`);
|
2023-05-12 07:59:32 +02:00
|
|
|
/**
|
2023-12-05 07:42:35 +01:00
|
|
|
* @api {get} http://header/CheckHeaders - CheckHeaders
|
2023-11-19 16:34:37 +01:00
|
|
|
* @apiGroup Middlewares
|
|
|
|
* @apiName CheckHeaders
|
2023-12-05 07:42:35 +01:00
|
|
|
* @apiDescription a list of headers are mandatory to access apxtrib see in your space town /conf.json.exposedHeaders
|
2023-05-12 07:59:32 +02:00
|
|
|
*
|
2023-12-05 07:42:35 +01:00
|
|
|
* @apiHeader {string} xalias 'anonymous' or unique alias
|
|
|
|
* @apiHeader {string} xapp name of the webapp store in tribe/tribeid/www/{xapp}
|
2023-11-19 16:34:37 +01:00
|
|
|
* @apiHeader {string} xlang the 2 letter request langage (if does not exist then return en = english).
|
2023-12-05 07:42:35 +01:00
|
|
|
* @apiHeader {string} xtribe unique tribe name where xapp exist
|
2023-11-19 16:34:37 +01: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 07:42:35 +01:00
|
|
|
* @apiHeader {string} xuuid a unique number uuid.v4 created the fisrt time a domain is visited on a device
|
2023-11-19 16:34:37 +01:00
|
|
|
* @apiHeader {integer} xtrkversion a version number link to tracking system
|
2023-05-12 07:59:32 +02:00
|
|
|
*
|
2023-12-05 07:42:35 +01: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 16:34:37 +01:00
|
|
|
const checkHeaders = (req, res, next) => {
|
2023-05-12 07:59:32 +02: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 15:23:17 +02:00
|
|
|
//console.log("req.headers", req.headers);
|
2023-05-12 07:59:32 +02: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 12:03:25 +01:00
|
|
|
// console.log( 'pass header', header )
|
2023-05-12 07:59:32 +02: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 07:42:35 +01:00
|
|
|
status:400,
|
2023-11-05 12:03:25 +01:00
|
|
|
ref: "middlewares",
|
2023-05-12 07:59:32 +02:00
|
|
|
msg: "missingheader",
|
|
|
|
data: missingheader,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//console.log( req.app.locals.tribeids )
|
|
|
|
// xtribe == "town" is used during the setup process
|
2023-11-05 12:03:25 +01:00
|
|
|
// xtribe == "adminapi" is used to access /adminapi
|
2023-05-12 07:59:32 +02:00
|
|
|
if (
|
|
|
|
!(
|
2023-11-05 12:03:25 +01:00
|
|
|
["town","adminapi"].includes(header.xtribe) || req.app.locals.tribeids.includes(header.xtribe)
|
2023-05-12 07:59:32 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return res.status(404).json({
|
2023-12-05 07:42:35 +01:00
|
|
|
status:404,
|
2023-11-05 12:03:25 +01:00
|
|
|
ref: "middlewares",
|
2023-05-12 07:59:32 +02: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 12:03:25 +01:00
|
|
|
//set anonymous profil
|
|
|
|
req.session.header.xprofils=["anonymous"]
|
2023-05-12 07:59:32 +02:00
|
|
|
next();
|
2023-04-13 07:46:35 +02:00
|
|
|
};
|
|
|
|
module.exports = checkHeaders;
|