WIP loads API if allowed

This commit is contained in:
AJ ONeal 2017-05-20 00:09:48 +00:00
parent c1735d5c03
commit 032ebe0302
2 changed files with 21 additions and 14 deletions

View File

@ -12,7 +12,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
xconfx.appApiGrantsPath = path.join(__dirname, '..', '..', 'packages', 'client-api-grants'); xconfx.appApiGrantsPath = path.join(__dirname, '..', '..', 'packages', 'client-api-grants');
function notConfigured(req, res) { function notConfigured(req, res) {
res.send({ error: { message: "api '" + req.pkgId + "' not configured for domain '" + req.experienceId + "'" } }); res.send({ error: { message: "api package '" + req.pkgId + "' not configured for client uri '" + req.experienceId + "'" } });
} }
/* /*
@ -23,23 +23,28 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
function isThisClientAllowedToUseThisPkg(myConf, clientUrih, pkgId) { function isThisClientAllowedToUseThisPkg(myConf, clientUrih, pkgId) {
var appApiGrantsPath = path.join(myConf.appApiGrantsPath, clientUrih); var appApiGrantsPath = path.join(myConf.appApiGrantsPath, clientUrih);
console.log('sanity exists?', appApiGrantsPath);;
return fs.readFileAsync(appApiGrantsPath, 'utf8').then(function (text) { return fs.readFileAsync(appApiGrantsPath, 'utf8').then(function (text) {
console.log('sanity', text);
return text.trim().split(/\n/); return text.trim().split(/\n/);
}, function (/*err*/) { }, function (rer) {
console.error(rer);
return []; return [];
}).then(function (apis) { }).then(function (apis) {
if (!apis.some(function (api) { if (apis.some(function (api) {
if (api === pkgId) { if (api === pkgId) {
console.log(api, pkgId, api === pkgId);
return true; return true;
} }
})) { })) {
return true;
}
if (clientUrih === ('api.' + xconfx.setupDomain) && 'org.oauth3.consumer' === pkgId) { if (clientUrih === ('api.' + xconfx.setupDomain) && 'org.oauth3.consumer' === pkgId) {
// fallthrough // fallthrough
return true; return true;
} else { } else {
return null; return null;
} }
}
}); });
} }
@ -122,8 +127,9 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
return function (req, res, next) { return function (req, res, next) {
cors(req, res, function () { cors(req, res, function () {
var clientUrih = req.hostname + req.url.replace(/\/api\/.*/, '/').replace(/\/+/g, '#').replace(/#$/, ''); console.log('[sanity check]', req.url);
var pkgId = req.url.replace(/.*\/api\//, '').replace(/\/.*/, ''); var clientUrih = req.hostname.replace(/^api\./, '') + req.url.replace(/\/api\/.*/, '/').replace(/\/+/g, '#').replace(/#$/, '');
var pkgId = req.url.replace(/.*\/api\//, '').replace(/^\//, '').replace(/\/$/, '');
var now = Date.now(); var now = Date.now();
var hasBeenHandled = false; var hasBeenHandled = false;
@ -134,7 +140,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
, writable: false , writable: false
, value: clientUrih , value: clientUrih
}); });
Object.defineProperty(req, 'pkgId', { Object.defineProperty(req, 'apiId', {
enumerable: true enumerable: true
, configurable: false , configurable: false
, writable: false , writable: false
@ -168,11 +174,11 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
if (localCache.rests[pkgId]) { if (localCache.rests[pkgId]) {
localCache.rests[pkgId].handler(req, res, next); localCache.rests[pkgId].handler(req, res, next);
hasBeenHandled = true; hasBeenHandled = true;
}
if (now - localCache.rests[pkgId].createdAt > staleAfter) { if (now - localCache.rests[pkgId].createdAt > staleAfter) {
localCache.rests[pkgId] = null; localCache.rests[pkgId] = null;
} }
}
if (!localCache.rests[pkgId]) { if (!localCache.rests[pkgId]) {
//return doesThisPkgExist //return doesThisPkgExist
@ -183,9 +189,9 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) {
return; return;
} }
localCache.rests[pkgId] = { handler: myHandler.handle, createdAt: now }; localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now };
if (!hasBeenHandled) { if (!hasBeenHandled) {
myHandler.handle(req, res, next); myHandler.handler(req, res, next);
} }
}); });
} }

View File

@ -265,9 +265,10 @@ module.exports.create = function (app, xconfx, apiFactories, apiDeps, errorIfApi
// TODO handle assets.example.com/sub/assets/com.example.xyz/ // TODO handle assets.example.com/sub/assets/com.example.xyz/
app.use('/api', function (req, res, next) { app.use('/', function (req, res, next) {
// If this doesn't look like an API we can move along // If this doesn't look like an API we can move along
if (!/^api\./.test(req.hostname) && !/\/api(\/|$)/.test(req.url)) { if (!/\/api(\/|$)/.test(req.url)) {
// /^api\./.test(req.hostname) &&
next(); next();
return; return;
} }