mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){ /* Create profile structure to store informations * about current session */ var profile={}; profile.username=null; profile.projectname=null; var token={}; token.part_0=null; token.part_1=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 */ var isAlreadyLogin=function(){ var profileInCookie=$cookies.getObject('profile'); var tokenPart_0InCookie=$cookies.getObject('token.part_0'); var tokenPart_1InCookie=$cookies.getObject('token.part_0'); 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; } return false; } /* * Destroy profile cookies */ var logout=function(){ $cookies.remove('profile'); $cookies.remove('token.part_0'); $cookies.remove('token.part_1'); } /** * * @param {string} response The response to parse * @param {boolean} to check if the request is send or not * @returns {requestParserResult} Formated data */ var parseLoginAnswer=function(response, failedToSendRequest){ var requestParserResult={}; requestParserResult.status=1; requestParserResult.failReason=null; if (typeof response.data.token !== 'undefined') { requestParserResult.status=0; 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){ requestParserResult.failReason="Failed to send request"; } else{ requestParserResult.failReason="Please check your username, password and project name !"; } return requestParserResult; }; var getToken=function(){ return token.part_0+token.part_1; } /** * Function to connect to OpenStack * * @param {object} $http Angular $http service * @param {string} username The user name * @param {string} password The user password * @param {string} projectname The user project name * @param {function} function to call when data is avalaible */ 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})); // Wait and handle the response result.then(function (response){ callback(parseLoginAnswer(response), false); },function(response){ callback(parseLoginAnswer(response), true) }); }; // Return services objects return { login: login, profile: profile, isAlreadyLogin: isAlreadyLogin, logout:logout, getToken:getToken }; }]);