Session manager modified to catch all requests. Login redirect fix
authorLaurence Maultsby <laurence.maultsby@riftio.com>
Thu, 27 Apr 2017 17:58:59 +0000 (13:58 -0400)
committerLaurence Maultsby <laurence.maultsby@riftio.com>
Thu, 27 Apr 2017 17:58:59 +0000 (13:58 -0400)
Signed-off-by: Laurence Maultsby <laurence.maultsby@riftio.com>
skyquake/framework/core/api_utils/utils.js
skyquake/framework/core/modules/api/sessions.js
skyquake/framework/core/modules/api/userManagementAPI.js
skyquake/framework/core/modules/routes/navigation.js
skyquake/framework/core/views/login.html

index 3a50964..667e7a9 100644 (file)
@@ -196,6 +196,9 @@ if (process.env.LOG_REQUESTS) {
  * @param {Function} res - a handle to the express response function
  */
 var sendErrorResponse = function(error, res) {
+       if (!error.statusCode) {
+               console.error('Status Code has not been set in error object: ', error);
+       }
        res.status(error.statusCode);
        res.send(error);
 }
index 9736f88..0c67091 100644 (file)
@@ -46,7 +46,7 @@ function logAndRedirectToLogin(mesg, res, req) {
     var api_server = req.query['api_server'] || (req.protocol + '://' + configurationAPI.globalConfiguration.get().api_server);
     var upload_server = req.protocol + '://' + (configurationAPI.globalConfiguration.get().upload_server || req.hostname);
     console.log(mesg);
-    res.redirect('login.html?api_server=' + api_server + '&upload_server=' + upload_server + '&referer=' + req.headers.referer);
+    res.redirect('login.html?api_server=' + api_server + '&upload_server=' + upload_server + '&referer=' + encodeURIComponent(req.headers.referer));
     res.end();
 }
 
@@ -124,6 +124,7 @@ sessionsAPI.create = function(req, res) {
                 username: username,
                 // project: req.session.projectId
             };
+            req.session.redirect = true;
             var successMsg = 'User => ' + username + ' successfully logged in.';
             successMsg += req.session.projectId ? 'Project => ' + req.session.projectId + ' set as default.' : '';
 
index b0b8ad5..b2f55c3 100644 (file)
@@ -70,21 +70,27 @@ UserManagement.getProfile = function(req) {
     var api_server = req.query['api_server'];
     return new Promise(function(resolve, reject) {
         var response = {};
-        var userId = req.session.userdata.username
-        response['data'] = {
-            userId: userId,
-            projectId: req.session.projectId
-        };
-        UserManagement.getUserInfo(req, userId).then(function(result) {
-            response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK;
-            response.data.data =result.data
-            resolve(response);
-        }, function(error) {
-            console.log('Error retrieving getUserInfo');
+        try {
+            var userId = req.session.userdata.username
+            response['data'] = {
+                userId: userId,
+                projectId: req.session.projectId
+            };
+            UserManagement.getUserInfo(req, userId).then(function(result) {
+                response.statusCode = constants.HTTP_RESPONSE_CODES.SUCCESS.OK;
+                response.data.data = result.data
+                resolve(response);
+            }, function(error) {
+                console.log('Error retrieving getUserInfo');
+                response.statusCode = constants.HTTP_RESPONSE_CODES.ERROR.INTERNAL_SERVER_ERROR;
+                reject(response);
+            })
+        } catch (e) {
+            response.data.data = e;
             response.statusCode = constants.HTTP_RESPONSE_CODES.ERROR.INTERNAL_SERVER_ERROR;
             reject(response);
-        })
-
+            reject()
+        }
     });
 };
 UserManagement.getUserInfo = function(req, userId, domain) {
index afffb20..3f460e6 100644 (file)
@@ -42,12 +42,27 @@ Router.get('/login.html', cors(), function(req, res) {
        res.end();
 });
 
-Router.get('/', cors(), function(req, res) {
+Router.use(/^\/(?!session).*/, function(req, res, next) {
        var api_server = req.query['api_server'] || (req.protocol + '://' + configurationAPI.globalConfiguration.get().api_server);
        if (req.session && req.session.loggedIn) {
-               console.log('Logged in. Redirect to launchpad');
-               if(req.params.referer) {
-                       res.redirect(req.params.referer);
+               next();
+       } else {
+               console.log('Redirect to login.html');
+               res.redirect('/login.html?api_server=' + api_server + '&upload_server=' + req.protocol + '://' + (configurationAPI.globalConfiguration.get().upload_server || req.hostname)  + '&referer=' + encodeURIComponent(req.headers.referer));
+       }
+});
+
+Router.use(function(req, res, next) {
+       var api_server = req.query['api_server'] || (req.protocol + '://' + configurationAPI.globalConfiguration.get().api_server);
+       if (req.session.redirect) {
+               req.session.redirect = false;
+        req.session.save(function(err) {
+            if (err) {
+                console.log('Error saving session to store', err);
+            }
+        });
+               if(req.query.referer && (req.query.referer != "undefined")) {
+                       res.redirect(decodeURIComponent(req.query.referer));
                }  else {
                        if(req.session.isLCM) {
                                res.redirect('/launchpad/?api_server=' + api_server + '&upload_server=' + req.protocol + '://' + (configurationAPI.globalConfiguration.get().upload_server || req.hostname));
@@ -56,8 +71,7 @@ Router.get('/', cors(), function(req, res) {
                        }
                }
        } else {
-               console.log('Redirect to login.html');
-               res.redirect('login.html?api_server=' + api_server + '&upload_server=' + req.protocol + '://' + (configurationAPI.globalConfiguration.get().upload_server || req.hostname)  + '&referer=' + req.headers.referer);
+               next();
        }
 });
 
index 404b21e..9089328 100644 (file)
@@ -75,6 +75,7 @@ $(document).ready(function() {
     var username;
     var pass;
     var api_server = getSearchParams(window.location).api_server;
+    var referer = getSearchParams(window.location).referer;
     $('#submit').click(function() {
         username=$('#username').val();
         pass=$('#password').val();
@@ -89,7 +90,7 @@ $(document).ready(function() {
                 password: pass
             },
             success: function(data) {
-                window.location.href='/?api_server=' + api_server;
+                window.location.href='/?api_server=' + api_server + '&referer=' + referer;
             }
         });
     });