X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FUI.git;a=blobdiff_plain;f=skyquake%2Fframework%2Fcore%2Fapi_utils%2Futils.js;h=3a509646a69b771f2fc83aa54b1670ae2670ecb4;hp=163769a981b7038bec09dad021e4e39b8c944a1f;hb=5b780234242860acc1979bbdece817bcbfa918c8;hpb=8d06f92bda77338e7772a912ef3cde474f2a06ed diff --git a/skyquake/framework/core/api_utils/utils.js b/skyquake/framework/core/api_utils/utils.js index 163769a98..3a509646a 100644 --- a/skyquake/framework/core/api_utils/utils.js +++ b/skyquake/framework/core/api_utils/utils.js @@ -1,5 +1,5 @@ /* - * + * * Copyright 2016 RIFT.IO Inc * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -49,6 +49,40 @@ 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\/)(.*)/, '$1project/' + projectId + '/$2'); + } + return url; +} + +var addProjectContextToRPCPayload = function(req, url, inputPayload) { + //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) { + if (url.indexOf('/api/operations/')) { + inputPayload['project-name'] = projectId; + } + } + return inputPayload; +} + var validateResponse = function(callerName, error, response, body, resolve, reject) { var res = {}; @@ -61,12 +95,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 +115,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 +129,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 +153,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 +174,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 +231,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,6 +251,15 @@ var passThroughConstructor = function(app) { }); } +var getPortForProtocol = function(protocol) { + switch (protocol) { + case 'http': + return 8000; + case 'https': + return 8443; + } +} + module.exports = { /** * Ensure confd port is on api_server variable. @@ -233,5 +276,11 @@ module.exports = { sendSuccessResponse: sendSuccessResponse, - passThroughConstructor: passThroughConstructor + passThroughConstructor: passThroughConstructor, + + getPortForProtocol: getPortForProtocol, + + projectContextUrl: projectContextUrl, + + addProjectContextToRPCPayload: addProjectContextToRPCPayload };