1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Prompt for login if comms reconnect fails authentication

This commit is contained in:
Nick O'Leary 2016-04-10 18:49:39 +01:00
parent 0c227be02d
commit 6b0bef61a5
3 changed files with 22 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/**
* Copyright 2014, 2015 IBM Corp.
* Copyright 2014, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,8 +24,10 @@ RED.comms = (function() {
var ws;
var pendingAuth = false;
var reconnectAttempts = 0;
var active = false;
function connectWS() {
active = true;
var path = location.hostname;
var port = location.port;
if (port.length !== 0) {
@ -63,9 +65,17 @@ RED.comms = (function() {
}
ws.onmessage = function(event) {
var msg = JSON.parse(event.data);
if (pendingAuth && msg.auth == "ok") {
pendingAuth = false;
completeConnection();
if (pendingAuth && msg.auth) {
if (msg.auth === "ok") {
pendingAuth = false;
completeConnection();
} else if (msg.auth === "fail") {
// anything else is an error...
active = false;
RED.user.login({updateMenu:true},function() {
connectWS();
})
}
} else if (msg.topic) {
for (var t in subscriptions) {
if (subscriptions.hasOwnProperty(t)) {
@ -83,6 +93,9 @@ RED.comms = (function() {
}
};
ws.onclose = function() {
if (!active) {
return;
}
if (clearErrorTimer) {
clearTimeout(clearErrorTimer);
clearErrorTimer = null;

View File

@ -99,6 +99,9 @@ RED.user = (function() {
}).done(function(data,textStatus,xhr) {
RED.settings.set("auth-tokens",data);
$("#node-dialog-login").dialog('destroy').remove();
if (opts.updateMenu) {
updateUserMenu();
}
done();
}).fail(function(jqXHR,textStatus,errorThrown) {
RED.settings.remove("auth-tokens");
@ -133,7 +136,7 @@ RED.user = (function() {
}
function updateUserMenu() {
$("#usermenu-submenu li").remove();
$("#btn-usermenu-submenu li").remove();
if (RED.settings.user.anonymous) {
RED.menu.addItem("btn-usermenu",{
id:"usermenu-item-login",
@ -163,8 +166,6 @@ RED.user = (function() {
}
function init() {
if (RED.settings.user) {
if (!RED.settings.editorTheme || !RED.settings.editorTheme.hasOwnProperty("userMenu")) {

View File

@ -89,6 +89,7 @@ function start() {
} else {
var completeConnection = function(userScope,sendAck) {
if (!userScope || !Permissions.hasPermission(userScope,"status.read")) {
ws.send(JSON.stringify({auth:"fail"}));
ws.close();
} else {
pendingAuth = false;