From 3ac0f3077e0179c83bc57df89891eba8b3a422d6 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Fri, 23 Jun 2017 17:22:45 -0600 Subject: [PATCH] fixed bug not being able to discover azp --- lib/app.js | 48 -------------------- lib/tunnel-client-manager.js | 45 +++++++++++++++++- lib/tunnel-server-manager.js | 14 +++--- packages/apis/com.daplie.goldilocks/index.js | 2 +- 4 files changed, 52 insertions(+), 57 deletions(-) diff --git a/lib/app.js b/lib/app.js index 0cbaab5..235ca02 100644 --- a/lib/app.js +++ b/lib/app.js @@ -114,54 +114,6 @@ module.exports = function (myDeps, conf, overrideHttp) { return result; }); } - , tunnel: function (deps, session) { - // TODO save session to config and turn tunnel on - var OAUTH3 = deps.OAUTH3; - var owner = session.id; - var url = require('url'); - var providerUri = session.token.aud; - var urlObj = url.parse(OAUTH3.url.normalize(session.token.azp)); - var oauth3 = OAUTH3.create(urlObj, { - providerUri: providerUri - , session: session - }); - - return oauth3.setProvider(providerUri).then(function () { - /* - return oauth3.api('domains.list').then(function (domains) { - var domainsMap = {}; - domains.forEach(function (d) { - if (!d.device) { - return; - } - if (d.device !== conf.device.hostname) { - return; - } - domainsMap[d.name] = true; - }); - */ - - //console.log('domains matching hostname', Object.keys(domainsMap)); - //console.log('device', conf.device); - return oauth3.api('tunnel.token', { - data: { - // filter to all domains that are on this device - //domains: Object.keys(domainsMap) - device: { - hostname: conf.device.hostname - , id: conf.device.uid || conf.device.id - } - } - }).then(function (result) { - console.log('got a token from the tunnel server?'); - result.owner = owner; - return deps.tunnelClients.add(result); - }); - /* - }); - */ - }); - } }; return require('../packages/apis/com.daplie.goldilocks').create(myDeps, conf); diff --git a/lib/tunnel-client-manager.js b/lib/tunnel-client-manager.js index abb8c51..cd5f390 100644 --- a/lib/tunnel-client-manager.js +++ b/lib/tunnel-client-manager.js @@ -66,6 +66,42 @@ module.exports.create = function (deps, config) { } }; + function acquireToken(session) { + var OAUTH3 = deps.OAUTH3; + // session seems to be changed by the API call for some reason, so save the + // owner before that happens. + var owner = session.id; + + // The OAUTH3 library stores some things on the root session object that we usually + // just leave inside the token, but we need to pull those out before we use it here + session.provider_uri = session.provider_uri || session.token.provider_uri || session.token.iss; + session.client_uri = session.client_uri || session.token.azp; + session.scope = session.scope || session.token.scp; + + console.log('asking for tunnel token from', session.token.aud); + return OAUTH3.discover(session.token.aud).then(function (directives) { + var opts = { + api: 'tunnel.token' + , session: session + , data: { + // filter to all domains that are on this device + //domains: Object.keys(domainsMap) + device: { + hostname: config.device.hostname + , id: config.device.uid || config.device.id + } + } + }; + + console.log(directives); + return OAUTH3.api(directives.api, opts).then(function (result) { + console.log('got a token from the tunnel server?'); + result.owner = owner; + return result; + }); + }); + } + function addToken(data) { if (!data.tunnelUrl) { var decoded; @@ -147,7 +183,14 @@ module.exports.create = function (deps, config) { }); return { - add: function (data) { + start: function (session) { + return acquireToken(session).then(function (token) { + return addToken(token).then(function () { + return storage.save(token); + }); + }); + } + , add: function (data) { return addToken(data).then(function () { return storage.save(data); }); diff --git a/lib/tunnel-server-manager.js b/lib/tunnel-server-manager.js index c920d3d..cdada29 100644 --- a/lib/tunnel-server-manager.js +++ b/lib/tunnel-server-manager.js @@ -3,8 +3,8 @@ module.exports.create = function (deps, config) { if (!config.tunnelServer || !Array.isArray(config.tunnelServer.servernames) || !config.tunnelServer.secret) { return { - isAdminDomain: function () { return false; }, - isClientDomain: function () { return false; }, + isAdminDomain: function () { return false; } + , isClientDomain: function () { return false; } }; } @@ -50,12 +50,12 @@ module.exports.create = function (deps, config) { return { isAdminDomain: function (domain) { return config.tunnelServer.servernames.indexOf(domain) !== -1; - }, - handleAdminConn: function (conn) { + } + , handleAdminConn: function (conn) { httpServer.emit('connection', conn); - }, + } - isClientDomain: tunnelServer.isClientDomain, - handleClientConn: tunnelServer.tcp + , isClientDomain: tunnelServer.isClientDomain + , handleClientConn: tunnelServer.tcp }; }; diff --git a/packages/apis/com.daplie.goldilocks/index.js b/packages/apis/com.daplie.goldilocks/index.js index 41a918e..2659578 100644 --- a/packages/apis/com.daplie.goldilocks/index.js +++ b/packages/apis/com.daplie.goldilocks/index.js @@ -169,7 +169,7 @@ module.exports.create = function (deps, conf) { } return deps.storage.owners.get(req.userId).then(function (session) { - return api.tunnel(deps, session).then(function () { + return deps.tunnelClients.start(session).then(function () { res.setHeader('Content-Type', 'application/json;'); res.end(JSON.stringify({ success: true })); }, function (err) {