fix(auth): prevent double transition to logout (#4266)

* fix(auth): prevent double transition to logout

* fix(app): revert

* feat(state-manager): reinitalize on login
pull/4273/head
Chaim Lev-Ari 4 years ago committed by GitHub
parent 6fa450a981
commit 0ebf0ab199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,7 +7,7 @@ async function initAuthentication(authManager, Authentication, $rootScope, $stat
// authManager.redirectWhenUnauthenticated() + unauthenticatedRedirector
// to have more controls on which URL should trigger the unauthenticated state.
$rootScope.$on('unauthenticated', function (event, data) {
if (!_.includes(data.config.url, '/v2/') && !_.includes(data.config.url, '/api/v4/')) {
if (!_.includes(data.config.url, '/v2/') && !_.includes(data.config.url, '/api/v4/') && isTransitionRequiresAuthentication($state.transition)) {
$state.go('portainer.logout', { error: 'Your session has expired' });
}
});
@ -32,9 +32,8 @@ angular.module('portainer.app', ['portainer.oauth']).config([
try {
const loggedIn = await initAuthentication(authManager, Authentication, $rootScope, $state);
await StateManager.initialize();
const nextTransition = $state.transition.to();
if (!loggedIn && !['portainer.logout', 'portainer.auth', 'portainer.init'].some((route) => nextTransition.name.startsWith(route))) {
$state.go('portainer.auth');
if (!loggedIn && isTransitionRequiresAuthentication($state.transition)) {
$state.go('portainer.logout');
return Promise.reject('Unauthenticated');
}
} catch (err) {
@ -425,3 +424,13 @@ angular.module('portainer.app', ['portainer.oauth']).config([
$stateRegistryProvider.register(team);
},
]);
function isTransitionRequiresAuthentication(transition) {
const UNAUTHENTICATED_ROUTES = ['portainer.logout', 'portainer.auth'];
if (!transition) {
return true;
}
const nextTransition = transition && transition.to();
const nextTransitionName = nextTransition ? nextTransition.name : '';
return !UNAUTHENTICATED_ROUTES.some((route) => nextTransitionName.startsWith(route));
}

@ -59,6 +59,7 @@ angular.module('portainer.app').factory('StateManager', [
manager.clean = function () {
state.endpoint = {};
state.application = {};
};
manager.updateLogo = function (logoURL) {

@ -147,6 +147,7 @@ class AuthenticationController {
}
async postLoginSteps() {
await this.StateManager.initialize();
await this.checkForEndpointsAsync();
await this.checkForLatestVersionAsync();
}

@ -32,9 +32,11 @@ angular.module('portainer.app').controller('InitAdminController', [
return Authentication.login(username, password);
})
.then(function success() {
StateManager.updateEnableTelemetry($scope.formValues.enableTelemetry);
return SettingsService.update({ enableTelemetry: $scope.formValues.enableTelemetry });
})
.then(() => {
return StateManager.initialize();
})
.then(function () {
return EndpointService.endpoints(0, 100);
})

Loading…
Cancel
Save