WIP tunnel
This commit is contained in:
parent
ac71f96ba1
commit
fb5407c29e
|
@ -1,3 +1,5 @@
|
|||
*session*
|
||||
*secret*
|
||||
var/*
|
||||
|
||||
# Logs
|
||||
|
|
|
@ -1,6 +1,50 @@
|
|||
'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) {
|
||||
var scmp = require('scmp');
|
||||
var crypto = require('crypto');
|
||||
|
@ -10,6 +54,8 @@ module.exports.create = function (deps) {
|
|||
inflate: true, limit: '100kb', reviver: null, strict: true /* type, verify */
|
||||
});
|
||||
|
||||
var api = module.exports.api;
|
||||
|
||||
/*
|
||||
var owners;
|
||||
deps.storage.owners.on('set', function (_owners) {
|
||||
|
@ -40,6 +86,7 @@ module.exports.create = function (deps) {
|
|||
return;
|
||||
}
|
||||
|
||||
req.userId = id;
|
||||
fn();
|
||||
});
|
||||
}
|
||||
|
@ -62,6 +109,12 @@ module.exports.create = function (deps) {
|
|||
var id = crypto.createHash('sha256').update(auth.sub).digest('hex');
|
||||
var tid = crypto.createHash('sha256').update(token.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);
|
||||
|
||||
|
@ -83,7 +136,7 @@ module.exports.create = function (deps) {
|
|||
return deps.PromiseA.reject(err);
|
||||
}
|
||||
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');
|
||||
|
||||
|
@ -101,7 +154,7 @@ module.exports.create = function (deps) {
|
|||
return scmp(tid, token.id);
|
||||
})) {
|
||||
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 () {
|
||||
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) {
|
||||
isAuthorized(req, res, function () {
|
||||
if ('POST' !== req.method) {
|
||||
|
@ -159,5 +225,6 @@ module.exports.create = function (deps) {
|
|||
});
|
||||
});
|
||||
}
|
||||
, _api: api
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue