WIP loads API if allowed
This commit is contained in:
parent
c1735d5c03
commit
032ebe0302
30
lib/apis.js
30
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue