X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=skyquake%2Fframework%2Fcore%2Fapi_utils%2Futils.js;h=06a4e726309a2a32375d45bff7d0219449f55d99;hb=56e55f5efc98e32a1afb3d99d07e9e5b0847a258;hp=bd99fe1a9a8924f37b77d330068b9e4af5a29213;hpb=b78fbe20fd7d976b3e15abb2387b4e329a5ea918;p=osm%2FUI.git diff --git a/skyquake/framework/core/api_utils/utils.js b/skyquake/framework/core/api_utils/utils.js index bd99fe1a9..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, }) @@ -217,32 +234,13 @@ var passThroughConstructor = function(app) { }); } - -var testAuthentication = function(app) { - app.get('/check-auth', function(req, res) { - console.log('testing auth') - var api_server = req.query["api_server"]; - var uri = confdPort(api_server) + '/api/config/'; - new Promise(function(resolve, reject) { - request({ - uri: uri, - method: 'GET', - headers: _.extend({}, CONSTANTS.HTTP_HEADERS.accept[type], { - 'Authorization': req.get('Authorization'), - forever: CONSTANTS.FOREVER_ON, - rejectUnauthorized: false, - }) - }, function(error, response, body) { - if (validateResponse('Passthrough: ' + url, error, response, body, resolve, reject)) { - resolve(JSON.parse(response.body)) - }; - }); - }).then(function(data) { - res.send(data); - }, function(error) { - res.send({'error': error, uri: uri}) - });; - }) +var getPortForProtocol = function(protocol) { + switch (protocol) { + case 'http': + return 8000; + case 'https': + return 8443; + } } module.exports = { @@ -263,5 +261,7 @@ module.exports = { passThroughConstructor: passThroughConstructor, - testAuthentication: testAuthentication + getPortForProtocol: getPortForProtocol, + + projectContextUrl: projectContextUrl };