2017-01-31 23:26:29 +00:00
|
|
|
angular.module('portainer.services')
|
2017-03-12 16:24:15 +00:00
|
|
|
.factory('Authentication', ['$q', 'Auth', 'jwtHelper', 'LocalStorage', 'StateManager', 'EndpointProvider', function AuthenticationFactory($q, Auth, jwtHelper, LocalStorage, StateManager, EndpointProvider) {
|
2017-01-31 23:26:29 +00:00
|
|
|
'use strict';
|
2017-02-01 09:13:48 +00:00
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
var user = {};
|
2017-01-31 23:26:29 +00:00
|
|
|
return {
|
|
|
|
init: function() {
|
|
|
|
var jwt = LocalStorage.getJWT();
|
|
|
|
if (jwt) {
|
|
|
|
var tokenPayload = jwtHelper.decodeToken(jwt);
|
2017-03-12 16:24:15 +00:00
|
|
|
user.username = tokenPayload.username;
|
|
|
|
user.ID = tokenPayload.id;
|
|
|
|
user.role = tokenPayload.role;
|
2017-01-31 23:26:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
login: function(username, password) {
|
|
|
|
return $q(function (resolve, reject) {
|
|
|
|
Auth.login({username: username, password: password}).$promise
|
|
|
|
.then(function(data) {
|
|
|
|
LocalStorage.storeJWT(data.jwt);
|
2017-03-12 16:24:15 +00:00
|
|
|
var tokenPayload = jwtHelper.decodeToken(data.jwt);
|
|
|
|
user.username = username;
|
|
|
|
user.ID = tokenPayload.id;
|
|
|
|
user.role = tokenPayload.role;
|
2017-01-31 23:26:29 +00:00
|
|
|
resolve();
|
|
|
|
}, function() {
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
logout: function() {
|
|
|
|
StateManager.clean();
|
2017-03-12 16:24:15 +00:00
|
|
|
EndpointProvider.clean();
|
2017-01-31 23:26:29 +00:00
|
|
|
LocalStorage.clean();
|
|
|
|
},
|
|
|
|
isAuthenticated: function() {
|
|
|
|
var jwt = LocalStorage.getJWT();
|
|
|
|
return jwt && !jwtHelper.isTokenExpired(jwt);
|
2017-02-01 09:13:48 +00:00
|
|
|
},
|
2017-03-12 16:24:15 +00:00
|
|
|
getUserDetails: function() {
|
|
|
|
return user;
|
2017-01-31 23:26:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|