From c511010ec41e57d99d1756780ee968269fa2b8fb Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 24 Feb 2016 14:39:47 +0100 Subject: Manager session cookie OK ! --- client/js/services/Identity.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'client/js/services/Identity.js') diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index 8ee664c..0dfba47 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -7,14 +7,17 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ var profile={}; profile.username=null; profile.projectname=null; - profile.token=null; - + var token={}; + token.part_0=null; + token.part_1=null; /** * Save profile in cookies */ var saveCookieForSession=function(){ - $cookies.putObject('profile', 5); + $cookies.putObject('profile', profile); + $cookies.putObject('token.part_0', token.part_0); + $cookies.putObject('token.part_1', token.part_1); }; @@ -23,10 +26,17 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ */ var isAlreadyLogin=function(){ var profileInCookie=$cookies.getObject('profile'); - console.log(profileInCookie); + var tokenPart_0InCookie=$cookies.getObject('token.part_0'); + var tokenPart_1InCookie=$cookies.getObject('token.part_0'); + - if(typeof profileInCookie !== 'undefined'){ + if(typeof profileInCookie !== 'undefined' + && typeof tokenPart_0InCookie !== 'undefined' + && typeof tokenPart_1InCookie !== 'undefined' + ){ angular.extend(profile, profileInCookie); + token.part_0=tokenPart_0InCookie; + token.part_1=tokenPart_1InCookie; return true; } @@ -38,6 +48,8 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ */ var logout=function(){ $cookies.remove('profile'); + $cookies.remove('token.part_0'); + $cookies.remove('token.part_1'); } @@ -56,7 +68,11 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ if (typeof response.data.token !== 'undefined') { requestParserResult.status=0; - profile.token=response.data.token; + + var middle=parseInt(response.data.token.length/2); + token.part_0=response.data.token.substring(0, middle); + token.part_1=response.data.token.substring(middle, response.data.token.length); + saveCookieForSession(); } else if(failedToSendRequest){ @@ -70,6 +86,9 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ }; + var getToken=function(){ + return token.part_0+token.part_1; + } /** * Function to connect to OpenStack @@ -104,7 +123,8 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ login: login, profile: profile, isAlreadyLogin: isAlreadyLogin, - logout:logout + logout:logout, + getToken:getToken }; -- cgit v1.2.3 From 211ebb0e432b5fee2031dd6e7338e659146867bb Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 24 Feb 2016 15:02:14 +0100 Subject: Simplify Identity --- client/js/controllers/login.js | 7 ++++++- client/js/controllers/status.js | 2 +- client/js/services/Identity.js | 46 +++++++++++++++++++++++++++-------------- 3 files changed, 37 insertions(+), 18 deletions(-) (limited to 'client/js/services/Identity.js') diff --git a/client/js/controllers/login.js b/client/js/controllers/login.js index a37591c..66d1793 100644 --- a/client/js/controllers/login.js +++ b/client/js/controllers/login.js @@ -13,13 +13,18 @@ mainApp.controller('loginCtrl', ['$scope','$sce','Identity', function ($scope,$s if(!Identity.isAlreadyLogin()){ $('#loginModal').modal({backdrop: 'static', keyboard: false}); } + + // Manager logout event $scope.$on('logoutEvent', function(){ $('#loginModal').modal({backdrop: 'static', keyboard: false}); }); - + + // Hide loading button and message alert $('#loadingLoginButton').hide(); $('#failedToLoginAlert').hide(); + + // Defined function for login $scope.loginAction=function(){ // Begin login state for template diff --git a/client/js/controllers/status.js b/client/js/controllers/status.js index e01df34..5ec6fa8 100644 --- a/client/js/controllers/status.js +++ b/client/js/controllers/status.js @@ -10,7 +10,7 @@ mainApp.controller('statusCtrl', ['$scope','Identity', '$rootScope', function ($ { // Give profile to model - $scope.profile=Identity.profile; + $scope.profile=Identity.getProfile(); // Function to logout $scope.logout=function(){ diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index 0dfba47..2042efd 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -7,10 +7,9 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ var profile={}; profile.username=null; profile.projectname=null; - var token={}; - token.part_0=null; - token.part_1=null; + var token=null; + /** * Save profile in cookies */ @@ -27,7 +26,7 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ var isAlreadyLogin=function(){ var profileInCookie=$cookies.getObject('profile'); var tokenPart_0InCookie=$cookies.getObject('token.part_0'); - var tokenPart_1InCookie=$cookies.getObject('token.part_0'); + var tokenPart_1InCookie=$cookies.getObject('token.part_1'); if(typeof profileInCookie !== 'undefined' @@ -35,14 +34,29 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ && typeof tokenPart_1InCookie !== 'undefined' ){ angular.extend(profile, profileInCookie); - token.part_0=tokenPart_0InCookie; - token.part_1=tokenPart_1InCookie; + token=tokenPart_0InCookie+tokenPart_1InCookie; + return true; } return false; } + + /* + * Get the profile + */ + var getProfile=function(){ + return profile; + } + + /* + * Get the token + */ + var getToken=function(){ + return token; + } + /* * Destroy profile cookies */ @@ -67,13 +81,18 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ requestParserResult.failReason=null; if (typeof response.data.token !== 'undefined') { + // Set status code requestParserResult.status=0; + // Find the middle of the token to split it var middle=parseInt(response.data.token.length/2); - token.part_0=response.data.token.substring(0, middle); - token.part_1=response.data.token.substring(middle, response.data.token.length); - - saveCookieForSession(); + + // Save profile + $cookies.putObject('profile', profile); + // Save first part of token + $cookies.putObject('token.part_0', response.data.token.substring(0, middle)); + // Save second part of token + $cookies.putObject('token.part_1', response.data.token.substring(middle, response.data.token.length)); } else if(failedToSendRequest){ requestParserResult.failReason="Failed to send request"; @@ -84,11 +103,6 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ return requestParserResult; }; - - - var getToken=function(){ - return token.part_0+token.part_1; - } /** * Function to connect to OpenStack @@ -121,7 +135,7 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ // Return services objects return { login: login, - profile: profile, + getProfile: getProfile, isAlreadyLogin: isAlreadyLogin, logout:logout, getToken:getToken -- cgit v1.2.3 From fa77d200c525d0c53118d5d4d60758880e9ca11c Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Wed, 24 Feb 2016 16:16:19 +0100 Subject: Add expire time and remove useless function --- client/js/services/Identity.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'client/js/services/Identity.js') diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index 2042efd..7604230 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -9,17 +9,6 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ profile.projectname=null; var token=null; - - /** - * Save profile in cookies - */ - var saveCookieForSession=function(){ - $cookies.putObject('profile', profile); - $cookies.putObject('token.part_0', token.part_0); - $cookies.putObject('token.part_1', token.part_1); - }; - - /* * @returns {boolean} Return true if a cookie is found (and load it in profile) false else */ @@ -87,12 +76,16 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ // Find the middle of the token to split it var middle=parseInt(response.data.token.length/2); + // Create expire date (cookie expire in 55 mins) + var expireDate=new Date(); + expireDate.setMinutes(expireDate.getMinutes()+55); + // Save profile - $cookies.putObject('profile', profile); + $cookies.putObject('profile', profile, {'expires': expireDate}); // Save first part of token - $cookies.putObject('token.part_0', response.data.token.substring(0, middle)); + $cookies.putObject('token.part_0', response.data.token.substring(0, middle), {'expires': expireDate}); // Save second part of token - $cookies.putObject('token.part_1', response.data.token.substring(middle, response.data.token.length)); + $cookies.putObject('token.part_1', response.data.token.substring(middle, response.data.token.length), {'expires': expireDate}); } else if(failedToSendRequest){ requestParserResult.failReason="Failed to send request"; -- cgit v1.2.3 From 6a67f6ba9dce19b046a5fc86e7437fbaa873c1e8 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Mon, 29 Feb 2016 08:32:48 +0100 Subject: Test --- client/js/services/Compute.js | 2 +- client/js/services/Identity.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'client/js/services/Identity.js') diff --git a/client/js/services/Compute.js b/client/js/services/Compute.js index 59f6291..9f404c4 100644 --- a/client/js/services/Compute.js +++ b/client/js/services/Compute.js @@ -17,7 +17,7 @@ mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){ var getMachines=function(callback){ var result=$http.post('../server/index.php', - $.param({"token" : Identity.getToken(), "task" : "compute", "action":"listServers"})); + $.param({"token" : Identity.getToken(), "task" : "compute", "action":"getServer", serverId:"69d5bcc4-2fab-4634-b0d2-f455fee5b7bd"})); // Wait and handle the response result.then(function (response){ diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index 7604230..a13c457 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -117,9 +117,9 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ // Wait and handle the response result.then(function (response){ - callback(parseLoginAnswer(response), false); + callback(parseLoginAnswer(response, false)); },function(response){ - callback(parseLoginAnswer(response), true) + callback(parseLoginAnswer(response, true)); }); }; -- cgit v1.2.3 From 3656654077f505dd782678451760eba45b59707b Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Mon, 29 Feb 2016 08:54:04 +0100 Subject: Clean code --- client/js/services/Compute.js | 14 +++++++++++--- client/js/services/Identity.js | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 19 deletions(-) (limited to 'client/js/services/Identity.js') diff --git a/client/js/services/Compute.js b/client/js/services/Compute.js index 9f404c4..8148948 100644 --- a/client/js/services/Compute.js +++ b/client/js/services/Compute.js @@ -2,11 +2,12 @@ mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){ - + // Create data var data={}; data.machines={}; + // Parser var parseGetMachinesAnswer=function(response, failedToSendRequest){ @@ -15,9 +16,16 @@ mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){ // Get Machine var getMachines=function(callback){ + + var params={ + "token" : Identity.getToken(), + "task" : "compute", + "action":"getServer", + "serverId":"69d5bcc4-2fab-4634-b0d2-f455fee5b7bd" + }; var result=$http.post('../server/index.php', - $.param({"token" : Identity.getToken(), "task" : "compute", "action":"getServer", serverId:"69d5bcc4-2fab-4634-b0d2-f455fee5b7bd"})); + $.param(params)); // Wait and handle the response result.then(function (response){ @@ -25,7 +33,7 @@ mainApp.factory('Compute',[ '$http', 'Identity', function($http, Identity){ callback(parseGetMachinesAnswer(response, false)); },function(response){ alert(response.status); - callback(parseGetMachinesAnswer(response, true)); + callback(parseGetMachinesAnswer(response, true)); }); diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index a13c457..b1d2a48 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -9,42 +9,37 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ profile.projectname=null; var token=null; + /* * @returns {boolean} Return true if a cookie is found (and load it in profile) false else */ var isAlreadyLogin=function(){ + + // Load cookies var profileInCookie=$cookies.getObject('profile'); var tokenPart_0InCookie=$cookies.getObject('token.part_0'); var tokenPart_1InCookie=$cookies.getObject('token.part_1'); + // Check if cookie is defined if(typeof profileInCookie !== 'undefined' && typeof tokenPart_0InCookie !== 'undefined' && typeof tokenPart_1InCookie !== 'undefined' ){ + + // If yes, put it into variables angular.extend(profile, profileInCookie); token=tokenPart_0InCookie+tokenPart_1InCookie; - + + // Return I'm Login return true; } + // Return I'm not Login return false; } - /* - * Get the profile - */ - var getProfile=function(){ - return profile; - } - - /* - * Get the token - */ - var getToken=function(){ - return token; - } /* * Destroy profile cookies @@ -64,7 +59,7 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ */ var parseLoginAnswer=function(response, failedToSendRequest){ - + // Defined return object var requestParserResult={}; requestParserResult.status=1; requestParserResult.failReason=null; @@ -96,6 +91,7 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ return requestParserResult; }; + /** * Function to connect to OpenStack @@ -113,7 +109,7 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ profile.projectname=projectname; var result=$http.post('../server/index.php', - $.param({"task" : "Authenticate", "user" : username, "password" : password, "project" : projectname})); + $.param({"task" : "Authenticate", "user" : username, "password" : password, "project" : projectname})); // Wait and handle the response result.then(function (response){ @@ -124,6 +120,20 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ }; + /* + * Get the profile + */ + var getProfile=function(){ + return profile; + } + + /* + * Get the token + */ + var getToken=function(){ + return token; + } + // Return services objects return { -- cgit v1.2.3 From f1628e280abaf47bfdcabc7d463ce6ad5d0f0752 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Tue, 1 Mar 2016 20:20:05 +0100 Subject: Clean Identity service --- client/js/services/Identity.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'client/js/services/Identity.js') diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index b1d2a48..da85ecd 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -48,6 +48,9 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ $cookies.remove('profile'); $cookies.remove('token.part_0'); $cookies.remove('token.part_1'); + token=null; + profile.username=null; + profile.projectname=null; } @@ -81,6 +84,10 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ $cookies.putObject('token.part_0', response.data.token.substring(0, middle), {'expires': expireDate}); // Save second part of token $cookies.putObject('token.part_1', response.data.token.substring(middle, response.data.token.length), {'expires': expireDate}); + + // Put token in var + token=response.data.token; + } else if(failedToSendRequest){ requestParserResult.failReason="Failed to send request"; @@ -102,7 +109,7 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ * @param {string} projectname The user project name * @param {function} function to call when data is avalaible */ - var login=function(username, password,projectname, callback){ + var login=function(username, password,projectname,callback){ // Set profile information (early) profile.username=username; @@ -120,6 +127,9 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ }; + + + /* * Get the profile */ @@ -133,6 +143,7 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ var getToken=function(){ return token; } + // Return services objects -- cgit v1.2.3