X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=skyquake%2Fframework%2Fcore%2Fapi_utils%2Futils.js;h=06a4e726309a2a32375d45bff7d0219449f55d99;hb=56e55f5efc98e32a1afb3d99d07e9e5b0847a258;hp=5b17279d5af68b95f64ff237d24ebfe6972ce483;hpb=64b803dbd876e15a0a2d4ca46d8ce81ac18915f6;p=osm%2FUI.git diff --git a/skyquake/framework/core/api_utils/utils.js b/skyquake/framework/core/api_utils/utils.js index 5b17279d5..06a4e7263 100644 --- a/skyquake/framework/core/api_utils/utils.js +++ b/skyquake/framework/core/api_utils/utils.js @@ -49,6 +49,23 @@ var confdPort = function(api_server) { return api_server + ':' + CONFD_PORT; }; +var projectContextUrl = function(req, url) { + //NOTE: We need to go into the sessionStore because express-session + // does not reliably update the session. + // See https://github.com/expressjs/session/issues/450 + var projectId = (req.session && + req.sessionStore && + req.sessionStore.sessions && + req.sessionStore.sessions[req.session.id] && + JSON.parse(req.sessionStore.sessions[req.session.id])['projectId']) || + (null); + if (projectId) { + return url.replace(/(\/api\/operational\/|\/api\/config\/|\/api\/operations\/)(.*)/, '$1project/' + projectId + '/$2'); + + } + return url; +} + var validateResponse = function(callerName, error, response, body, resolve, reject) { var res = {}; @@ -61,12 +78,12 @@ var validateResponse = function(callerName, error, response, body, resolve, reje }; reject(res); return false; - } else if (response.statusCode >= 400) { + } else if (response.statusCode >= CONSTANTS.HTTP_RESPONSE_CODES.ERROR.BAD_REQUEST) { console.log('Problem with "', callerName, '": ', response.statusCode, ':', body); res.statusCode = response.statusCode; // auth specific - if (response.statusCode == 401) { + if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.ERROR.UNAUTHORIZED) { res.errorMessage = { error: 'Authentication needed' + body }; @@ -81,7 +98,7 @@ var validateResponse = function(callerName, error, response, body, resolve, reje reject(res); return false; - } else if (response.statusCode == 204) { + } else if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.SUCCESS.NO_CONTENT) { resolve({ statusCode: response.statusCode, data: {} @@ -95,7 +112,7 @@ var validateResponse = function(callerName, error, response, body, resolve, reje var checkAuthorizationHeader = function(req) { return new Promise(function(resolve, reject) { - if (req.get('Authorization') == null) { + if (req.session && req.session.authorization == null) { reject(); } else { resolve(); @@ -119,12 +136,12 @@ if (process.env.LOG_REQUESTS) { reject(res); fs.appendFileSync(logFile, 'Request API: ' + response.request.uri.href + ' ; ' + 'Error: ' + error); return false; - } else if (response.statusCode >= 400) { + } else if (response.statusCode >= CONSTANTS.HTTP_RESPONSE_CODES.ERROR.BAD_REQUEST) { console.log('Problem with "', callerName, '": ', response.statusCode, ':', body); res.statusCode = response.statusCode; // auth specific - if (response.statusCode == 401) { + if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.ERROR.UNAUTHORIZED) { res.errorMessage = { error: 'Authentication needed' + body }; @@ -140,7 +157,7 @@ if (process.env.LOG_REQUESTS) { reject(res); fs.appendFileSync(logFile, 'Request API: ' + response.request.uri.href + ' ; ' + 'Error Body: ' + body); return false; - } else if (response.statusCode == 204) { + } else if (response.statusCode == CONSTANTS.HTTP_RESPONSE_CODES.SUCCESS.NO_CONTENT) { resolve(); fs.appendFileSync(logFile, 'Request API: ' + response.request.uri.href + ' ; ' + 'Response Body: ' + body); return false; @@ -197,10 +214,10 @@ var passThroughConstructor = function(app) { } new Promise(function(resolve, reject) { request({ - uri: uri, + uri: projectContextUrl(req, uri), method: 'GET', headers: _.extend({}, CONSTANTS.HTTP_HEADERS.accept[type], { - 'Authorization': req.get('Authorization'), + 'Authorization': req.session && req.session.authorization, forever: CONSTANTS.FOREVER_ON, rejectUnauthorized: false, }) @@ -244,5 +261,7 @@ module.exports = { passThroughConstructor: passThroughConstructor, - getPortForProtocol: getPortForProtocol + getPortForProtocol: getPortForProtocol, + + projectContextUrl: projectContextUrl };