From 021a44d47ae9f6c0e8d160bf6936a9ce68fb533a Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 6 Jun 2018 01:01:56 -0600 Subject: [PATCH] old auth works again --- lib/remote.js | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/remote.js b/lib/remote.js index 3769984..d3ea166 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -21,12 +21,14 @@ function _connect(state) { // being established initialy and allows the caller to use `.append` for the first token so // they can get a promise that will provide feedback about invalid tokens. var tokens = []; + var auth; if (state.token) { tokens.push(state.token); } var wstunneler; var authenticated = false; + var authsent = false; var localclients = {}; var pausedClients = []; @@ -171,6 +173,7 @@ function _connect(state) { function sendCommand(name) { var id = Math.ceil(1e9 * Math.random()); var cmd = [id, name].concat(Array.prototype.slice.call(arguments, 1)); + if (state.debug) { console.log('[DEBUG] command sending', cmd); } wsHandlers.sendMessage(Packer.pack(null, cmd, 'control')); setTimeout(function () { @@ -196,7 +199,13 @@ function _connect(state) { } function sendAllTokens() { + if (auth) { + authsent = true; + sendCommand('auth', auth).catch(function (err) { console.error('1', err); }); + } tokens.forEach(function (jwtoken) { + if (state.debug) { console.log('[DEBUG] send token'); } + authsent = true; sendCommand('add_token', jwtoken) .catch(function (err) { console.error('failed re-adding token', jwtoken, 'after reconnect', err); @@ -267,8 +276,7 @@ function _connect(state) { } if (cmd[1] === 'hello') { - // We only get the 'hello' event after the token has been validated - authenticated = true; + if (state.debug) { console.log('[DEBUG] hello received'); } sendAllTokens(); if (connCallback) { connCallback(); @@ -276,14 +284,12 @@ function _connect(state) { // TODO: handle the versions and commands provided by 'hello' - isn't super important // yet since there is only one version and set of commands. err = null; - } - else { - err = { message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' }; - } - - if (cmd[1] === 'grant') { + } else if (cmd[1] === 'grant') { + authenticated = true; displayGrants(cmd[2]); return; + } else { + err = { message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' }; } wsHandlers.sendMessage(Packer.pack(null, [-cmd[0], err], 'control')); @@ -478,24 +484,25 @@ function _connect(state) { }; function connect() { - var auth; if (wstunneler) { console.warn('attempted to connect with connection already active'); return; } - if (tokens.length) { - auth = 'access_token=' + tokens[0]; - } else if (state.config.email) { - auth = 'subject=' + state.config.email; - auth += '&subject_scheme=mailto'; - // TODO create domains list earlier - auth += '&scope=' + Object.keys(state.config.servernames || {}).join(','); + if (!tokens.length) { + if (state.config.email) { + auth = { + subject: state.config.email + , subject_scheme: 'mailto' + // TODO create domains list earlier + , scope: Object.keys(state.config.servernames || {}).join(',') + }; + } } timeoutId = null; var machine = Packer.create(packerHandlers); console.info("[connect] '" + state.relay + "'"); - var tunnelUrl = state.relay.replace(/\/$/, '') + '/?' + auth; + var tunnelUrl = state.relay.replace(/\/$/, '') + '/'; // + auth; wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: !state.insecure }); wstunneler.on('open', wsHandlers.onOpen); wstunneler.on('close', wsHandlers.onClose);