summaryrefslogtreecommitdiff
path: root/client/js/services/Identity.js
blob: 2042efdf8aa385079af7109f80a5b038a2f754a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

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=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_1');

		
		if(typeof profileInCookie !== 'undefined'
		   && typeof tokenPart_0InCookie !== 'undefined'
		   && typeof tokenPart_1InCookie !== 'undefined'
		  ){
			angular.extend(profile, profileInCookie);
			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
	 */
	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') {
			// Set status code
			requestParserResult.status=0;

			// Find the middle of the token to split it
			var middle=parseInt(response.data.token.length/2);

			// 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";
		}
		else{
			requestParserResult.failReason="Please check your username, password and project name !";
		}
		
		return requestParserResult;
	};
	
	/**
	 * 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,
		getProfile: getProfile,
		isAlreadyLogin: isAlreadyLogin,
		logout:logout,
		getToken:getToken
	};

	
}]);