From 032ebe0302a89278cb978fd96ed62bdf0cdefa6a Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 20 May 2017 00:09:48 +0000 Subject: [PATCH] WIP loads API if allowed --- lib/apis.js | 30 ++++++++++++++++++------------ lib/main.js | 5 +++-- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index ec28efe..b88419c 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -12,7 +12,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { xconfx.appApiGrantsPath = path.join(__dirname, '..', '..', 'packages', 'client-api-grants'); 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) { var appApiGrantsPath = path.join(myConf.appApiGrantsPath, clientUrih); + console.log('sanity exists?', appApiGrantsPath);; return fs.readFileAsync(appApiGrantsPath, 'utf8').then(function (text) { + console.log('sanity', text); return text.trim().split(/\n/); - }, function (/*err*/) { + }, function (rer) { +console.error(rer); return []; }).then(function (apis) { - if (!apis.some(function (api) { + if (apis.some(function (api) { if (api === pkgId) { + console.log(api, pkgId, api === pkgId); return true; } })) { + return true; + } if (clientUrih === ('api.' + xconfx.setupDomain) && 'org.oauth3.consumer' === pkgId) { // fallthrough return true; } else { return null; } - } }); } @@ -122,8 +127,9 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { return function (req, res, next) { cors(req, res, function () { - var clientUrih = req.hostname + req.url.replace(/\/api\/.*/, '/').replace(/\/+/g, '#').replace(/#$/, ''); - var pkgId = req.url.replace(/.*\/api\//, '').replace(/\/.*/, ''); + console.log('[sanity check]', req.url); + 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 hasBeenHandled = false; @@ -134,7 +140,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { , writable: false , value: clientUrih }); - Object.defineProperty(req, 'pkgId', { + Object.defineProperty(req, 'apiId', { enumerable: true , configurable: false , writable: false @@ -168,10 +174,10 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { if (localCache.rests[pkgId]) { localCache.rests[pkgId].handler(req, res, next); hasBeenHandled = true; - } - if (now - localCache.rests[pkgId].createdAt > staleAfter) { - localCache.rests[pkgId] = null; + if (now - localCache.rests[pkgId].createdAt > staleAfter) { + localCache.rests[pkgId] = null; + } } if (!localCache.rests[pkgId]) { @@ -183,9 +189,9 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { return; } - localCache.rests[pkgId] = { handler: myHandler.handle, createdAt: now }; + localCache.rests[pkgId] = { handler: myHandler.handler, createdAt: now }; if (!hasBeenHandled) { - myHandler.handle(req, res, next); + myHandler.handler(req, res, next); } }); } diff --git a/lib/main.js b/lib/main.js index 540db0d..338123f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -265,9 +265,10 @@ module.exports.create = function (app, xconfx, apiFactories, apiDeps, errorIfApi // 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 (!/^api\./.test(req.hostname) && !/\/api(\/|$)/.test(req.url)) { + if (!/\/api(\/|$)/.test(req.url)) { + // /^api\./.test(req.hostname) && next(); return; }