old auth works again

This commit is contained in:
AJ ONeal 2018-06-06 01:01:56 -06:00
parent a94f3d26eb
commit 5725d9f1e1
1 changed files with 24 additions and 17 deletions

View File

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