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

172 lines
4 KiB
JavaScript
Raw Normal View History

2016-03-21 06:47:01 +01:00
mainApp.factory('Identity',[ '$http', '$cookies', '$rootScope', function($http, $cookies, $rootScope){
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 15:02:14 +01:00
var token=null;
2016-02-17 14:01:13 +01:00
2016-02-29 08:54:04 +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(){
2016-02-29 08:54:04 +01:00
// Load cookies
2016-02-17 14:01:13 +01:00
var profileInCookie=$cookies.getObject('profile');
2016-02-24 14:39:47 +01:00
var tokenPart_0InCookie=$cookies.getObject('token.part_0');
2016-02-24 15:02:14 +01:00
var tokenPart_1InCookie=$cookies.getObject('token.part_1');
2016-02-24 14:39:47 +01:00
2016-02-17 14:01:13 +01:00
2016-02-29 08:54:04 +01:00
// Check if cookie is defined
2016-02-24 14:39:47 +01:00
if(typeof profileInCookie !== 'undefined'
&& typeof tokenPart_0InCookie !== 'undefined'
&& typeof tokenPart_1InCookie !== 'undefined'
){
2016-02-29 08:54:04 +01:00
2016-03-21 06:47:01 +01:00
//if(token!==null){
2016-03-20 11:23:06 +01:00
// If yes, put it into variables
angular.extend(profile, profileInCookie);
token=tokenPart_0InCookie+tokenPart_1InCookie;
2016-03-21 06:47:01 +01:00
//}
2016-02-29 08:54:04 +01:00
// Return I'm Login
2016-02-17 14:01:13 +01:00
return true;
}
2016-03-21 06:47:01 +01:00
// Show the login overlay
$rootScope.$broadcast("logoutEvent");
2016-02-29 08:54:04 +01:00
// Return I'm not Login
2016-02-17 14:01:13 +01:00
return false;
}
2016-02-24 15:02:14 +01:00
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-03-01 20:20:05 +01:00
token=null;
profile.username=null;
profile.projectname=null;
2016-03-02 17:41:04 +01:00
// Reload Page
2016-03-21 06:47:01 +01:00
//location.reload();
$rootScope.$broadcast("logoutEvent");
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-29 08:54:04 +01:00
// Defined return object
2016-02-10 21:45:52 +01:00
var requestParserResult={};
requestParserResult.status=1;
requestParserResult.failReason=null;
if (typeof response.data.token !== 'undefined') {
2016-02-24 15:02:14 +01:00
// Set status code
2016-02-10 21:45:52 +01:00
requestParserResult.status=0;
2016-02-24 14:39:47 +01:00
2016-02-24 15:02:14 +01:00
// Find the middle of the token to split it
2016-02-24 14:39:47 +01:00
var middle=parseInt(response.data.token.length/2);
2016-02-24 15:02:14 +01:00
// Create expire date (cookie expire in 55 mins)
var expireDate=new Date();
expireDate.setMinutes(expireDate.getMinutes()+55);
2016-02-24 15:02:14 +01:00
// Save profile
$cookies.putObject('profile', profile, {'expires': expireDate});
2016-02-24 15:02:14 +01:00
// Save first part of token
$cookies.putObject('token.part_0', response.data.token.substring(0, middle), {'expires': expireDate});
2016-02-24 15:02:14 +01:00
// Save second part of token
$cookies.putObject('token.part_1', response.data.token.substring(middle, response.data.token.length), {'expires': expireDate});
2016-03-01 20:20:05 +01:00
// Put token in var
token=response.data.token;
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 !";
}
2016-03-02 17:41:04 +01:00
2016-02-10 21:45:52 +01:00
return requestParserResult;
};
2016-02-29 08:54:04 +01:00
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-03-01 20:20:05 +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',
2016-02-29 08:54:04 +01:00
$.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){
2016-02-29 08:32:48 +01:00
callback(parseLoginAnswer(response, false));
2016-02-10 21:45:52 +01:00
},function(response){
2016-02-29 08:32:48 +01:00
callback(parseLoginAnswer(response, true));
2016-02-10 21:45:52 +01:00
});
};
2016-02-22 17:14:11 +01:00
2016-03-01 20:20:05 +01:00
2016-02-29 08:54:04 +01:00
/*
* Get the profile
*/
var getProfile=function(){
return profile;
}
/*
* Get the token
*/
var getToken=function(){
return token;
}
2016-03-01 20:20:05 +01:00
2016-02-29 08:54:04 +01:00
2016-02-22 17:14:11 +01:00
2016-02-10 17:55:13 +01:00
// Return services objects
return {
login: login,
2016-02-24 15:02:14 +01:00
getProfile: getProfile,
2016-02-17 14:01:13 +01:00
isAlreadyLogin: isAlreadyLogin,
2016-02-24 14:39:47 +01:00
logout:logout,
getToken:getToken
};
}]);