allow loopback to use providers that are not oauth3.org

This commit is contained in:
tigerbot 2017-06-27 10:39:59 -06:00
parent f37730c97d
commit a4aad3184a
2 changed files with 29 additions and 25 deletions

View File

@ -1,6 +1,6 @@
'use strict';
module.exports.create = function () {
module.exports.create = function (deps) {
var PromiseA = require('bluebird');
var request = PromiseA.promisify(require('request'));
var pending = {};
@ -8,7 +8,7 @@ module.exports.create = function () {
function checkPublicAddr(host) {
return request({
method: 'GET'
, url: 'https://'+host+'/api/org.oauth3.tunnel/checkip'
, url: host+'/api/org.oauth3.tunnel/checkip'
, json: true
}).then(function (result) {
if (!result.body) {
@ -18,7 +18,6 @@ module.exports.create = function () {
var err = new Error(result.body.error.message);
return PromiseA.reject(Object.assign(err, result.body.error));
}
console.log(result.body, result.body.address);
return result.body.address;
});
}
@ -39,7 +38,7 @@ module.exports.create = function () {
return request({
method: 'POST'
, url: 'https://'+host+'/api/org.oauth3.tunnel/loopback'
, url: host+'/api/org.oauth3.tunnel/loopback'
, json: opts
})
.then(function (result) {
@ -60,21 +59,16 @@ module.exports.create = function () {
});
}
function loopback(session) {
var host;
if (!session) {
host = 'api.oauth3.org';
} else {
host = 'api.' + ((session.token || {}).aud || 'oauth3.org');
}
return checkPublicAddr(host).then(function (address) {
function loopback(provider) {
return deps.OAUTH3.discover(provider).then(function (directives) {
return checkPublicAddr(directives.api).then(function (address) {
console.log('checking to see if', address, 'gets back to us');
var ports = require('./servers').listeners.tcp.list();
return PromiseA.all(ports.map(function (port) {
return checkSinglePort(host, address, port);
return checkSinglePort(directives.api, address, port);
}))
.then(function (values) {
console.log(pending);
var result = {error: null, address: address};
ports.forEach(function (port, ind) {
result[port] = values[ind];
@ -82,6 +76,7 @@ module.exports.create = function () {
return result;
});
});
});
}
loopback.server = require('http').createServer(function (req, res) {

View File

@ -313,9 +313,18 @@ module.exports.create = function (deps, conf) {
return;
}
isAuthorized(req, res, function () {
var prom;
var query = require('querystring').parse(require('url').parse(req.url).query);
if (query.provider) {
prom = deps.loopback(query.provider);
} else {
prom = deps.storage.owners.get(req.userId).then(function (session) {
return deps.loopback(session.token.aud);
});
}
res.setHeader('Content-Type', 'application/json');
deps.loopback()
.then(function (result) {
prom.then(function (result) {
res.end(JSON.stringify(result));
}, function (err) {
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));