diff options
| author | root <root@kabir-PC> | 2016-03-02 15:11:56 +0100 |
|---|---|---|
| committer | root <root@kabir-PC> | 2016-03-02 15:11:56 +0100 |
| commit | dac5c28a6795984a32687fe21c41783de2e85d8b (patch) | |
| tree | bb5b719f923b33e31d5a0880d1d50dbe49543e45 /client/js/services/Identity.js | |
| parent | 70fcf3553abb4a25672ee198dafb89e430e2bd79 (diff) | |
| parent | b6d7d2c30efe5e9758072bb82ea3a947bda7fd1d (diff) | |
Merge branch 'develop' of https://github.com/manzerbredes/istic-openstack into othmane
Diffstat (limited to 'client/js/services/Identity.js')
| -rw-r--r-- | client/js/services/Identity.js | 92 |
1 files changed, 70 insertions, 22 deletions
diff --git a/client/js/services/Identity.js b/client/js/services/Identity.js index 8ee664c..da85ecd 100644 --- a/client/js/services/Identity.js +++ b/client/js/services/Identity.js @@ -7,37 +7,50 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ var profile={}; profile.username=null; profile.projectname=null; - profile.token=null; - - - /** - * Save profile in cookies - */ - var saveCookieForSession=function(){ - $cookies.putObject('profile', 5); - }; - + 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'); - console.log(profileInCookie); + var tokenPart_0InCookie=$cookies.getObject('token.part_0'); + var tokenPart_1InCookie=$cookies.getObject('token.part_1'); + - if(typeof profileInCookie !== 'undefined'){ + // 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; } + + /* * Destroy profile cookies */ var logout=function(){ $cookies.remove('profile'); + $cookies.remove('token.part_0'); + $cookies.remove('token.part_1'); + token=null; + profile.username=null; + profile.projectname=null; } @@ -49,15 +62,32 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ */ var parseLoginAnswer=function(response, failedToSendRequest){ - + // Defined return object var requestParserResult={}; requestParserResult.status=1; requestParserResult.failReason=null; if (typeof response.data.token !== 'undefined') { + // Set status code requestParserResult.status=0; - profile.token=response.data.token; - saveCookieForSession(); + + // 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, {'expires': expireDate}); + // Save first part of token + $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"; @@ -69,7 +99,6 @@ mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ return requestParserResult; }; - /** * Function to connect to OpenStack @@ -80,31 +109,50 @@ 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; 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){ - callback(parseLoginAnswer(response), false); + callback(parseLoginAnswer(response, false)); },function(response){ - callback(parseLoginAnswer(response), true) + callback(parseLoginAnswer(response, true)); }); }; + + + /* + * Get the profile + */ + var getProfile=function(){ + return profile; + } + + /* + * Get the token + */ + var getToken=function(){ + return token; + } + + + // Return services objects return { login: login, - profile: profile, + getProfile: getProfile, isAlreadyLogin: isAlreadyLogin, - logout:logout + logout:logout, + getToken:getToken }; |
