istic-openstack/client/js/services/Identity.js

132 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-02-17 14:01:13 +01:00
mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){
2016-02-10 21:45:52 +01:00
/* Create profile structure to store informations
* about current session
*/
2016-02-10 17:55:13 +01:00
var profile={};
2016-02-10 21:45:52 +01:00
profile.username=null;
profile.projectname=null;
2016-02-24 14:39:47 +01:00
var token={};
token.part_0=null;
token.part_1=null;
2016-02-17 14:01:13 +01:00
2016-02-22 17:14:11 +01:00
/**
* Save profile in cookies
*/
2016-02-17 14:01:13 +01:00
var saveCookieForSession=function(){
2016-02-24 14:39:47 +01:00
$cookies.putObject('profile', profile);
$cookies.putObject('token.part_0', token.part_0);
$cookies.putObject('token.part_1', token.part_1);
2016-02-17 14:01:13 +01:00
};
2016-02-22 17:14:11 +01:00
/*
* @returns {boolean} Return true if a cookie is found (and load it in profile) false else
*/
2016-02-17 14:01:13 +01:00
var isAlreadyLogin=function(){
var profileInCookie=$cookies.getObject('profile');
2016-02-24 14:39:47 +01:00
var tokenPart_0InCookie=$cookies.getObject('token.part_0');
var tokenPart_1InCookie=$cookies.getObject('token.part_0');
2016-02-17 14:01:13 +01:00
2016-02-24 14:39:47 +01:00
if(typeof profileInCookie !== 'undefined'
&& typeof tokenPart_0InCookie !== 'undefined'
&& typeof tokenPart_1InCookie !== 'undefined'
){
2016-02-17 14:01:13 +01:00
angular.extend(profile, profileInCookie);
2016-02-24 14:39:47 +01:00
token.part_0=tokenPart_0InCookie;
token.part_1=tokenPart_1InCookie;
2016-02-17 14:01:13 +01:00
return true;
}
return false;
}
2016-02-22 17:14:11 +01:00
/*
* Destroy profile cookies
*/
2016-02-17 14:01:13 +01:00
var logout=function(){
$cookies.remove('profile');
2016-02-24 14:39:47 +01:00
$cookies.remove('token.part_0');
$cookies.remove('token.part_1');
2016-02-17 14:01:13 +01:00
}
2016-02-22 17:14:11 +01:00
2016-02-17 14:01:13 +01:00
2016-02-10 21:45:52 +01:00
/**
*
* @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){
2016-02-17 14:01:13 +01:00
2016-02-10 21:45:52 +01:00
var requestParserResult={};
requestParserResult.status=1;
requestParserResult.failReason=null;
if (typeof response.data.token !== 'undefined') {
requestParserResult.status=0;
2016-02-24 14:39:47 +01:00
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);
2016-02-17 14:01:13 +01:00
saveCookieForSession();
2016-02-10 21:45:52 +01:00
}
else if(failedToSendRequest){
requestParserResult.failReason="Failed to send request";
}
else{
requestParserResult.failReason="Please check your username, password and project name !";
}
return requestParserResult;
};
2016-02-24 14:39:47 +01:00
var getToken=function(){
return token.part_0+token.part_1;
}
2016-02-10 17:55:13 +01:00
/**
2016-02-10 17:55:13 +01:00
* 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
2016-02-10 21:45:52 +01:00
* @param {function} function to call when data is avalaible
*/
2016-02-10 21:45:52 +01:00
var login=function(username, password,projectname, callback){
2016-02-17 14:01:13 +01:00
2016-02-10 21:45:52 +01:00
// Set profile information (early)
2016-02-10 17:55:13 +01:00
profile.username=username;
profile.projectname=projectname;
2016-02-10 21:45:52 +01:00
var result=$http.post('../server/index.php',
$.param({"task" : "Authenticate", "user" : username, "password" : password, "project" : projectname}));
2016-02-10 21:45:52 +01:00
// Wait and handle the response
result.then(function (response){
callback(parseLoginAnswer(response), false);
},function(response){
callback(parseLoginAnswer(response), true)
});
};
2016-02-22 17:14:11 +01:00
2016-02-10 17:55:13 +01:00
// Return services objects
return {
login: login,
2016-02-17 14:01:13 +01:00
profile: profile,
isAlreadyLogin: isAlreadyLogin,
2016-02-24 14:39:47 +01:00
logout:logout,
getToken:getToken
};
}]);