const conf = require(`${process.env.dirtown}/conf.json`); /** * @api{get}/CheckHeaders * @apiGroup Middlewares * @apiName CheckHeaders * @apiDescription a list of header is mandatory to access apxtrib see tribes/townconf.json.exposedHeaders * * @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 * * @apiError missingexposedHeaders it miss an exposedHeaders * @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} * } * @apiHeaderExample {json} Header-Exemple: * { * xtribe:"apache", * xalias:"toto", * xhash:"", * xdays:"123" * xlang:"en", * xapp:"popular" * } */ const checkHeaders = (req, res, next) => { req.session = {}; const header = {}; if (!req.header("xlang") && req.header("Content-Language")) req.params.xlang = req.header("Content-Language"); let missingheader = []; //console.log("req.headers", req.headers); 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); } } // console.log( 'pass header', header ) // 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({ ref: "middlewares", msg: "missingheader", data: missingheader, }); } //console.log( req.app.locals.tribeids ) // xtribe == "town" is used during the setup process // xtribe == "adminapi" is used to access /adminapi if ( !( ["town","adminapi"].includes(header.xtribe) || req.app.locals.tribeids.includes(header.xtribe) ) ) { return res.status(404).json({ ref: "middlewares", 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"; } //set anonymous profil req.session.header.xprofils=["anonymous"] next(); }; module.exports = checkHeaders;