Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit b289449

Browse files
ghillertBoykoAlex
authored andcommitted
gh-158 Fix: Dashboard refresh causes user session logout
* Store oauth-token in session-store * Restore Dashboard UI in-case a pre-existing oauth-token can be retrieved AND if the SecurityInfo REST endpoint indicates that the user is still logged in * Polish login page - Improve error handling - Disable login button as long as both form-fields (username / password) are not filled * Logout will clear the session-store
1 parent c34bd5b commit b289449

File tree

6 files changed

+116
-41
lines changed

6 files changed

+116
-41
lines changed

ui/app/scripts/auth/controllers/login.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,50 @@
2323

2424
define([], function () {
2525
'use strict';
26-
return ['$scope', '$state', 'userService', 'DataflowUtils', '$log', '$rootScope', '$http',
27-
function ($scope, $state, user, utils, $log, $rootScope, $http) {
28-
$scope.loginForm = {};
26+
return ['$scope', '$state', 'userService', 'DataflowUtils', '$log', '$rootScope', '$http', '$window',
27+
function ($scope, $state, userService, utils, $log, $rootScope, $http, $window) {
28+
$scope.loginModel = {
29+
username: '',
30+
password: ''
31+
};
32+
$scope.errorMessage = null;
33+
2934
$scope.login = function() {
30-
$log.info('Logging in user:', $scope.loginForm.username);
31-
var authenticationPromise = $http.post($rootScope.dataflowServerUrl + '/authenticate', $scope.loginForm);
35+
$log.info('Logging in user:', $scope);
36+
var authenticationPromise = $http.post($rootScope.dataflowServerUrl + '/authenticate', $scope.loginModel);
3237
utils.addBusyPromise(authenticationPromise);
3338
authenticationPromise.then(
3439
function(response) {
35-
$rootScope.user.username = $scope.loginForm.username;
36-
$rootScope.user.isAuthenticated = true;
37-
$rootScope.user.isFormLogin = true;
38-
$http.defaults.headers.common[$rootScope.xAuthTokenHeaderName] = response.data;
40+
var oauthToken = response.data;
41+
$http.defaults.headers.common[$rootScope.xAuthTokenHeaderName] = oauthToken;
3942

4043
var securityInfoUrl = '/security/info';
4144
var timeout = 20000;
4245
var promiseHttp = $http.get(securityInfoUrl, {timeout: timeout});
43-
utils.growl.success('User ' + $scope.loginForm.username + ' logged in.');
44-
$scope.loginForm = {};
4546

4647
promiseHttp.then(function(response) {
4748
console.log('Security info retrieved ...', response.data);
48-
$rootScope.user.roles = response.data.roles;
49-
$state.go('home.apps.tabs.appsList');
49+
userService.populateUser(response.data);
50+
$window.sessionStorage.setItem('xAuthToken', oauthToken);
51+
52+
if (response.data.authenticated) {
53+
utils.growl.success('User ' + response.data.username + ' logged in.');
54+
$scope.loginModel = {
55+
username: '',
56+
password: ''
57+
};
58+
$state.go('home.apps.tabs.appsList');
59+
}
60+
else {
61+
$scope.errorMessage = 'Login failed. Please retry.';
62+
}
5063
}, function(errorResponse) {
51-
var errorMessage = 'Error retrieving security info from ' + securityInfoUrl + ' (timeout: ' + timeout + 'ms)';
52-
console.log(errorMessage, errorResponse);
53-
$('.splash .container').html(errorMessage);
64+
console.log('Error getteng securityInfo', errorResponse);
65+
$scope.errorMessage = 'Error retrieving security info from ' + securityInfoUrl + ' (timeout: ' + timeout + 'ms)';
5466
});
5567
},
5668
function(response) {
69+
$scope.errorMessage = response.data[0].message ;
5770
utils.growl.error(response.data[0].message);
5871
}
5972
);

ui/app/scripts/auth/controllers/logout.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,24 @@
2121
*/
2222
define([], function () {
2323
'use strict';
24-
return ['$window', 'DataflowUtils', '$state', '$log', '$rootScope', '$http', 'userService', function ($window, DataflowUtils, $state, $log, $rootScope, $http, user) {
25-
$log.info('Logging out...');
24+
return ['$window', 'DataflowUtils', '$state', '$log', '$rootScope', '$http', 'userService', function ($window, DataflowUtils, $state, $log, $rootScope, $http, userService) {
2625

27-
if ($rootScope.user.isFormLogin) {
28-
$http.get($rootScope.dataflowServerUrl + '/dashboard/logout').then(function() {
29-
30-
$rootScope.user.username = '';
31-
$rootScope.user.isAuthenticated = false;
32-
$rootScope.user.isFormLogin = false;
33-
34-
user = {
35-
authenticationEnabled: true,
36-
isFormLogin: false,
37-
isAuthenticated: false,
38-
username: ''
39-
};
26+
$log.info('Logging out ...');
4027

28+
if (userService.isFormLogin) {
29+
console.log('Logging out user ' + userService.username + ' (FormLogin)');
30+
$http.get($rootScope.dataflowServerUrl + '/dashboard/logout').then(function() {
31+
userService.resetUser();
4132
delete $http.defaults.headers.common[$rootScope.xAuthTokenHeaderName];
33+
$window.sessionStorage.removeItem('xAuthToken-token');
4234
DataflowUtils.growl.success('Logged out.');
4335
$state.go('login');
4436
});
4537
}
46-
else{
38+
else {
39+
console.log('Logging out user ' + userService.username + ' (OAuth)');
40+
delete $http.defaults.headers.common[$rootScope.xAuthTokenHeaderName];
41+
$window.sessionStorage.removeItem('xAuthToken-token');
4742
var logoutUrl = '//' + $window.location.host + '/logout';
4843
console.log('Redirecting to ' + logoutUrl);
4944
$window.open(logoutUrl, '_self');

ui/app/scripts/auth/services.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,12 +24,13 @@ define(['angular'], function (angular) {
2424
'use strict';
2525

2626
return angular.module('dataflowAuth.services', [])
27-
.factory('userService', function(securityInfo) {
27+
.factory('userService', function(securityInfo, $rootScope, $window, $http) {
2828
var user = {
2929
authenticationEnabled: securityInfo.authenticationEnabled,
3030
isAuthenticated: securityInfo.authenticated,
3131
username: securityInfo.username,
3232
roles: securityInfo.roles,
33+
isFormLogin: securityInfo.formLogin,
3334
hasRole: function(role) {
3435
if (user.roles.indexOf(role) >= 0){
3536
return true;
@@ -38,7 +39,49 @@ define(['angular'], function (angular) {
3839
return false;
3940
}
4041
},
41-
isFormLogin: false
42+
resetUser: function() {
43+
user.authenticationEnabled = null;
44+
user.isAuthenticated = null;
45+
user.isFormLogin = null;
46+
user.roles = [];
47+
user.username = null;
48+
$rootScope.user.username = '';
49+
$rootScope.user.isAuthenticated = false;
50+
},
51+
populateUser: function(userInfo) {
52+
user.authenticationEnabled = userInfo.authenticationEnabled;
53+
user.isAuthenticated = userInfo.authenticated;
54+
user.isFormLogin = userInfo.formLogin;
55+
user.roles = userInfo.roles;
56+
user.username = userInfo.username;
57+
58+
$rootScope.user.username = user.username;
59+
$rootScope.user.isAuthenticated = user.isAuthenticated;
60+
},
61+
restoreUser: function (xAuthToken) {
62+
if (xAuthToken !== null) {
63+
$http.defaults.headers.common[$rootScope.xAuthTokenHeaderName] = xAuthToken;
64+
$window.sessionStorage.setItem('xAuthToken', xAuthToken);
65+
}
66+
67+
var securityInfoUrl = '/security/info';
68+
var timeout = 20000;
69+
var promiseHttp = $http.get(securityInfoUrl, {timeout: timeout});
70+
71+
promiseHttp.then(function(response) {
72+
console.log('Security info retrieved ...', response.data);
73+
74+
$rootScope.user = {
75+
username: response.data.username,
76+
isAuthenticated: response.data.authenticated,
77+
isFormLogin: true,
78+
roles: response.data.roles
79+
};
80+
81+
console.log($rootScope.user);
82+
});
83+
return promiseHttp;
84+
}
4285
};
4386
return user;
4487
})

ui/app/scripts/auth/views/login.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
<div class="platform--title" ng-if="user.isAuthenticated">Please sign out first</div>
1313
<div class="platform--text">
1414
<div class="container-fluid">
15+
<div class="row" ng-hide="errorMessage === null">
16+
<div class="col-md-4 col-md-offset-4">
17+
{{errorMessage}}
18+
</div>
19+
</div>
1520
<div class="row">
1621
<div class="col-md-4 col-md-offset-4">
17-
<form class="form-signin" role="form" ng-submit="login()" novalidate ng-if="!user.isAuthenticated">
18-
<input ng-model="loginForm.username" type="text" class="form-control" placeholder="Username" required autofocus>
22+
<form name="loginForm" class="form-signin" role="form" ng-submit="login()" novalidate ng-if="!user.isAuthenticated">
23+
<input name="username" ng-model="loginModel.username" type="text" class="form-control" placeholder="Username" required autofocus>
1924
<div class="help-block"></div>
20-
<input ng-model="loginForm.password" type="password" class="form-control" placeholder="Password" required>
25+
<input name="password" ng-model="loginModel.password" type="password" class="form-control" placeholder="Password" required>
2126
<div class="help-block"></div>
22-
<button class="btn btn-lg btn-default btn-block" type="submit">Sign in</button>
27+
<button class="btn btn-lg btn-default btn-block" type="submit" ng-disabled="loginForm.$invalid">Sign in</button>
2328
</form>
2429
<form class="form-signin" role="form" ng-submit="logout()" novalidate ng-if="user.isAuthenticated">
2530
<button class="btn btn-lg btn-default btn-block" type="submit">Sign out</button>

ui/app/scripts/main.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,26 @@ define([
135135

136136
var initInjector = angular.injector(['ng']);
137137
var $http = initInjector.get('$http');
138+
var $window = initInjector.get('$window');
139+
140+
console.log('Checking whether current session is active ...');
141+
var xAuthToken = $window.sessionStorage.getItem('xAuthToken');
142+
143+
if (xAuthToken !== null) {
144+
console.log('Current session found.');
145+
$http.defaults.headers.common['x-auth-token'] = xAuthToken;
146+
}
147+
else {
148+
console.log('Initiating new session.');
149+
}
150+
138151
var securityInfoUrl = '/security/info';
139152
var timeout = 20000;
140153
var promiseHttp = $http.get(securityInfoUrl, {timeout: timeout});
141154
var promiseFeature = $http.get('/features', {timeout: timeout});
142155

143156
promiseHttp.then(function(response) {
144-
console.log('Security info retrieved ...', response.data);
157+
console.log('Security info retrieved, user authenticated: ' + response.data.authenticated, response.data);
145158
app.constant('securityInfo', response.data);
146159

147160
promiseFeature.then(function(featuresResponse) {
@@ -155,6 +168,7 @@ define([
155168
console.log(errorMessage, errorResponse);
156169
$('.splash .container').html(errorMessage);
157170
});
171+
158172
function updateGrowl() {
159173
var bodyScrollTop = $(document).scrollTop();
160174
var navHeight = $('nav').outerHeight();

ui/app/scripts/routes.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ define(['./app'], function (dashboard) {
436436
}
437437
});
438438
});
439-
dashboard.run(function ($rootScope, $state, $stateParams, userService, featuresService, $log) {
439+
dashboard.run(function ($rootScope, $state, $stateParams, userService, featuresService, $log, $window, $http) {
440440

441441
$rootScope.$state = $state;
442442
$rootScope.$stateParams = $stateParams;
@@ -447,6 +447,11 @@ define(['./app'], function (dashboard) {
447447
$rootScope.pageRefreshTime = 5000;
448448
$rootScope.enableMessageRates = true;
449449

450+
var xAuthToken = $window.sessionStorage.getItem('xAuthToken');
451+
if (xAuthToken !== null && userService.isAuthenticated) {
452+
console.log('User ' + userService.username + ' is already authenticated, populating http header ' + $rootScope.xAuthTokenHeaderName);
453+
$http.defaults.headers.common[$rootScope.xAuthTokenHeaderName] = xAuthToken;
454+
}
450455
$rootScope.$on('$stateChangeStart', function(event, toState) {
451456
if (toState.data.feature && !$rootScope.features[toState.data.feature]) {
452457
$log.error('Feature disabled: ' + toState.data.feature);

0 commit comments

Comments
 (0)