WIP tunnel

This commit is contained in:
AJ ONeal 2017-04-05 12:49:48 -06:00
parent ac71f96ba1
commit fb5407c29e
4 changed files with 96 additions and 4 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*session*
*secret*
var/* var/*
# Logs # Logs

View File

@ -1,6 +1,50 @@
'use strict'; 'use strict';
module.exports.dependencies = [ 'storage.owners' ]; module.exports.dependencies = [ 'OAUTH3', 'storage.owners', 'options.device' ];
module.exports.api = {
tunnel: function (deps, session) {
var OAUTH3 = deps.OAUTH3;
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
});
//var crypto = require('crypto');
//var id = crypto.createHash('sha256').update(session.token.sub).digest('hex');
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 !== deps.options.device.hostname) {
return;
}
domainsMap[d.name] = true;
});
console.log('domains matching hostname', Object.keys(domainsMap));
console.log('device', deps.options.device);
return oauth3.api('tunnel.token', {
data: {
// filter to all domains that are on this device
domains: Object.keys(domainsMap)
, device: {
hostname: deps.options.device.hostname
, id: deps.options.device.uid || deps.options.device.id
}
}
}).then(function (result) {
console.log(result);
});
});
});
//, { token: token, refresh: refresh });
}
};
module.exports.create = function (deps) { module.exports.create = function (deps) {
var scmp = require('scmp'); var scmp = require('scmp');
var crypto = require('crypto'); var crypto = require('crypto');
@ -10,6 +54,8 @@ module.exports.create = function (deps) {
inflate: true, limit: '100kb', reviver: null, strict: true /* type, verify */ inflate: true, limit: '100kb', reviver: null, strict: true /* type, verify */
}); });
var api = module.exports.api;
/* /*
var owners; var owners;
deps.storage.owners.on('set', function (_owners) { deps.storage.owners.on('set', function (_owners) {
@ -40,6 +86,7 @@ module.exports.create = function (deps) {
return; return;
} }
req.userId = id;
fn(); fn();
}); });
} }
@ -62,6 +109,12 @@ module.exports.create = function (deps) {
var id = crypto.createHash('sha256').update(auth.sub).digest('hex'); var id = crypto.createHash('sha256').update(auth.sub).digest('hex');
var tid = crypto.createHash('sha256').update(token.sub).digest('hex'); var tid = crypto.createHash('sha256').update(token.sub).digest('hex');
var rid = crypto.createHash('sha256').update(refresh.sub).digest('hex'); var rid = crypto.createHash('sha256').update(refresh.sub).digest('hex');
var session = {
access_token: req.body.access_token
, token: token
, refresh_token: req.body.refresh_token
, refresh: refresh
};
console.log('ids', id, tid, rid); console.log('ids', id, tid, rid);
@ -83,7 +136,7 @@ module.exports.create = function (deps) {
return deps.PromiseA.reject(err); return deps.PromiseA.reject(err);
} }
console.log('no owner, creating'); console.log('no owner, creating');
return deps.storage.owners.set(id, { token: token, refresh: refresh }); return deps.storage.owners.set(id, session);
} }
console.log('has results'); console.log('has results');
@ -101,7 +154,7 @@ module.exports.create = function (deps) {
return scmp(tid, token.id); return scmp(tid, token.id);
})) { })) {
console.log('adds new owner with existing owner'); console.log('adds new owner with existing owner');
return deps.storage.owners.set(id, { token: token, refresh: refresh }); return deps.storage.owners.set(id, session);
} }
}).then(function () { }).then(function () {
res.setHeader('Content-Type', 'application/json;'); res.setHeader('Content-Type', 'application/json;');
@ -114,6 +167,19 @@ module.exports.create = function (deps) {
}); });
} }
, tunnel: function (req, res) {
isAuthorized(req, res, function () {
jsonParser(req, res, function () {
console.log('req.body', req.body);
return deps.storage.owners.get(req.userId).then(function (session) {
session.token.id = req.userId;
return api.tunnel(deps, session);
});
});
});
}
, config: function (req, res) { , config: function (req, res) {
isAuthorized(req, res, function () { isAuthorized(req, res, function () {
if ('POST' !== req.method) { if ('POST' !== req.method) {
@ -159,5 +225,6 @@ module.exports.create = function (deps) {
}); });
}); });
} }
, _api: api
}; };
}; };

View File

@ -0,0 +1,23 @@
'use strict';
var api = require('./index.js').api;
var OAUTH3 = require('../../assets/org.oauth3/');
// these all auto-register
require('../../assets/org.oauth3/oauth3.domains.js');
require('../../assets/org.oauth3/oauth3.dns.js');
require('../../assets/org.oauth3/oauth3.tunnel.js');
OAUTH3._hooks = require('../../assets/org.oauth3/oauth3.node.storage.js');
api.tunnel(
{
OAUTH3: OAUTH3
, options: {
device: {
hostname: 'test.local'
, id: ''
}
}
}
// OAUTH3.hooks.session.get('oauth3.org').then(function (result) { console.log(result) });
, require('./test.session.json')
);

@ -1 +1 @@
Subproject commit 5ed05f03cf87e8354e7561c0d92485dc9716f8df Subproject commit 8f773c9de4ee9fdb893026c1045740635308922b