Skip to content

Commit c75b99b

Browse files
committed
An attempt to fix 401 Not Authorized after self password change in Management UI
1 parent a07a30f commit c75b99b

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

deps/rabbitmq_management/priv/www/js/dispatcher.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,17 @@ dispatcher_add(function(sammy) {
239239
return false;
240240
});
241241
sammy.put('#/users-modify', function() {
242-
if (sync_put(this, '/users/:username'))
243-
go_to('#/users');
242+
var is_own_password_change =
243+
this.params['password'] != undefined &&
244+
this.params['password'] != '' &&
245+
this.params['username'] == user.name;
246+
247+
if (is_own_password_change) {
248+
change_own_password(this);
249+
} else {
250+
if (sync_put(this, '/users/:username'))
251+
go_to('#/users');
252+
}
244253
return false;
245254
});
246255
sammy.del('#/users', function() {

deps/rabbitmq_management/priv/www/js/main.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,8 +1392,10 @@ function update_status(status) {
13921392

13931393

13941394
function with_req(method, path, body, fun, on404fun) {
1395+
if (password_change_in_progress) {
1396+
return;
1397+
}
13951398
if(!has_auth_credentials()) {
1396-
// Clear any lingering auth settings in local storage and navigate to the login form.
13971399
clear_auth();
13981400
location.reload();
13991401
return;
@@ -1518,6 +1520,9 @@ function check_bad_response(req, full_page_404, on404fun) {
15181520
if ((req.status >= 200 && req.status < 300) || req.status == 1223) {
15191521
return true;
15201522
}
1523+
else if (password_change_in_progress) {
1524+
return false;
1525+
}
15211526
else if (req.status == 404 && full_page_404) {
15221527
var html = format('404', {});
15231528
replace_content('main', html);
@@ -1942,3 +1947,52 @@ function check_version() {
19421947
location.reload()
19431948
}
19441949
}
1950+
1951+
var password_change_in_progress = false;
1952+
1953+
function change_own_password(sammy) {
1954+
var params = params_magic(sammy.params);
1955+
var path = 'api' + fill_path_template('/users/:username', params);
1956+
var new_password = sammy.params['password'];
1957+
var username = sammy.params['username'];
1958+
var done = false;
1959+
1960+
password_change_in_progress = true;
1961+
pause_auto_refresh();
1962+
for (var i in outstanding_reqs) {
1963+
outstanding_reqs[i].abort();
1964+
}
1965+
outstanding_reqs = [];
1966+
1967+
function finish() {
1968+
if (done) return;
1969+
done = true;
1970+
password_change_in_progress = false;
1971+
resume_auto_refresh();
1972+
}
1973+
1974+
var timeout = setTimeout(function() {
1975+
finish();
1976+
show_popup('warn',
1977+
'Password change request timed out. Please try logging in again.');
1978+
}, 30000);
1979+
1980+
var req = xmlHttpRequest();
1981+
req.open('PUT', path, true);
1982+
req.setRequestHeader('content-type', 'application/json');
1983+
req.setRequestHeader('authorization', authorization_header());
1984+
req.onreadystatechange = function() {
1985+
if (req.readyState == 4 && !done) {
1986+
clearTimeout(timeout);
1987+
if (req.status >= 200 && req.status < 300) {
1988+
set_basic_auth(username, new_password);
1989+
finish();
1990+
go_to('#/users');
1991+
} else {
1992+
finish();
1993+
check_bad_response(req, false);
1994+
}
1995+
}
1996+
};
1997+
req.send(JSON.stringify(params));
1998+
}

0 commit comments

Comments
 (0)