From 74e1ef0823736946d83caf040c3efec403ebeae8 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Wed, 12 Nov 2014 13:21:59 +0000 Subject: [PATCH] Add auth awareness to comms channel --- public/red/comms.js | 4 ++++ red/comms.js | 55 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/public/red/comms.js b/public/red/comms.js index 649124762..4d352a2a7 100644 --- a/public/red/comms.js +++ b/public/red/comms.js @@ -29,6 +29,10 @@ RED.comms = (function() { errornotification.close(); errornotification = null; } + var auth_tokens = RED.settings.get("auth-tokens"); + if (auth_tokens) { + ws.send(JSON.stringify({auth:auth_tokens.access_token})); + } for (var t in subscriptions) { if (subscriptions.hasOwnProperty(t)) { ws.send(JSON.stringify({subscribe:t})); diff --git a/red/comms.js b/red/comms.js index 822b66943..f02263d0f 100644 --- a/red/comms.js +++ b/red/comms.js @@ -14,6 +14,8 @@ * limitations under the License. **/ +var tokens = require("./api/auth/tokens"); + var ws = require("ws"); var log = require("./log"); @@ -21,6 +23,7 @@ var server; var settings; var wsServer; +var pendingConnections = []; var activeConnections = []; var retained = {}; @@ -43,13 +46,17 @@ function start() { wsServer = new ws.Server({server:server,path:path}); wsServer.on('connection',function(ws) { - activeConnections.push(ws); + var pendingAuth = (settings.httpAdminAuth != null); + if (!pendingAuth) { + activeConnections.push(ws); + } else { + pendingConnections.push(ws); + } ws.on('close',function() { - for (var i=0;i