allow loopback to use providers that are not oauth3.org
This commit is contained in:
parent
f37730c97d
commit
a4aad3184a
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports.create = function () {
|
module.exports.create = function (deps) {
|
||||||
var PromiseA = require('bluebird');
|
var PromiseA = require('bluebird');
|
||||||
var request = PromiseA.promisify(require('request'));
|
var request = PromiseA.promisify(require('request'));
|
||||||
var pending = {};
|
var pending = {};
|
||||||
|
@ -8,7 +8,7 @@ module.exports.create = function () {
|
||||||
function checkPublicAddr(host) {
|
function checkPublicAddr(host) {
|
||||||
return request({
|
return request({
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
, url: 'https://'+host+'/api/org.oauth3.tunnel/checkip'
|
, url: host+'/api/org.oauth3.tunnel/checkip'
|
||||||
, json: true
|
, json: true
|
||||||
}).then(function (result) {
|
}).then(function (result) {
|
||||||
if (!result.body) {
|
if (!result.body) {
|
||||||
|
@ -18,7 +18,6 @@ module.exports.create = function () {
|
||||||
var err = new Error(result.body.error.message);
|
var err = new Error(result.body.error.message);
|
||||||
return PromiseA.reject(Object.assign(err, result.body.error));
|
return PromiseA.reject(Object.assign(err, result.body.error));
|
||||||
}
|
}
|
||||||
console.log(result.body, result.body.address);
|
|
||||||
return result.body.address;
|
return result.body.address;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -39,7 +38,7 @@ module.exports.create = function () {
|
||||||
|
|
||||||
return request({
|
return request({
|
||||||
method: 'POST'
|
method: 'POST'
|
||||||
, url: 'https://'+host+'/api/org.oauth3.tunnel/loopback'
|
, url: host+'/api/org.oauth3.tunnel/loopback'
|
||||||
, json: opts
|
, json: opts
|
||||||
})
|
})
|
||||||
.then(function (result) {
|
.then(function (result) {
|
||||||
|
@ -60,21 +59,16 @@ module.exports.create = function () {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loopback(session) {
|
function loopback(provider) {
|
||||||
var host;
|
return deps.OAUTH3.discover(provider).then(function (directives) {
|
||||||
if (!session) {
|
return checkPublicAddr(directives.api).then(function (address) {
|
||||||
host = 'api.oauth3.org';
|
|
||||||
} else {
|
|
||||||
host = 'api.' + ((session.token || {}).aud || 'oauth3.org');
|
|
||||||
}
|
|
||||||
|
|
||||||
return checkPublicAddr(host).then(function (address) {
|
|
||||||
console.log('checking to see if', address, 'gets back to us');
|
console.log('checking to see if', address, 'gets back to us');
|
||||||
var ports = require('./servers').listeners.tcp.list();
|
var ports = require('./servers').listeners.tcp.list();
|
||||||
return PromiseA.all(ports.map(function (port) {
|
return PromiseA.all(ports.map(function (port) {
|
||||||
return checkSinglePort(host, address, port);
|
return checkSinglePort(directives.api, address, port);
|
||||||
}))
|
}))
|
||||||
.then(function (values) {
|
.then(function (values) {
|
||||||
|
console.log(pending);
|
||||||
var result = {error: null, address: address};
|
var result = {error: null, address: address};
|
||||||
ports.forEach(function (port, ind) {
|
ports.forEach(function (port, ind) {
|
||||||
result[port] = values[ind];
|
result[port] = values[ind];
|
||||||
|
@ -82,6 +76,7 @@ module.exports.create = function () {
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loopback.server = require('http').createServer(function (req, res) {
|
loopback.server = require('http').createServer(function (req, res) {
|
||||||
|
|
|
@ -313,9 +313,18 @@ module.exports.create = function (deps, conf) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
isAuthorized(req, res, function () {
|
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');
|
res.setHeader('Content-Type', 'application/json');
|
||||||
deps.loopback()
|
prom.then(function (result) {
|
||||||
.then(function (result) {
|
|
||||||
res.end(JSON.stringify(result));
|
res.end(JSON.stringify(result));
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
|
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
|
||||||
|
|
Loading…
Reference in New Issue