-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathvalidateResourcePath.js
95 lines (74 loc) · 3.46 KB
/
validateResourcePath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
var url = require('url');
var debug_ = require('debug');
const DOUBLE_STAR_PLACEHOLDER = '@@@@';
const SUPPORTED_SINGLE_FORWARD_SLASH_PATTERN = "/";
module.exports = function (config, req, res, decodedToken, productOnly, logger, componentName) {
if (config === undefined || !config) return (undefined);
if (req === undefined || !req) return (undefined);
if (res === undefined || !res) return (undefined);
const proxy = res.proxy;
let urlPath = req.reqUrl.path;
var debug = debug_('plugin:' + componentName);
var parsedUrl = url.parse(urlPath);
debug('product only: ' + productOnly);
if (!decodedToken.api_product_list) {
debug('no api product list');
return false;
}
return decodedToken.api_product_list.some(function (product) {
const validProxyNames = config.product_to_proxy[product];
if (!productOnly) {
if (!validProxyNames) {
debug('no proxies found for product');
return false;
}
}
const resourcePaths = config.product_to_api_resource[product];
var matchesProxyRules = false;
if (resourcePaths && resourcePaths.length) {
resourcePaths.forEach(function (productResourcePath) {
if (matchesProxyRules) {
//found one
debug('found matching proxy rule');
return;
}
if (productResourcePath === SUPPORTED_SINGLE_FORWARD_SLASH_PATTERN) {
matchesProxyRules = true;
} else {
urlPath = parsedUrl.pathname;
let apiproxy = productResourcePath.includes(proxy.base_path) ?
productResourcePath :
proxy.base_path + (productResourcePath.startsWith("/") ? "" : "/") + productResourcePath;
if (!apiproxy.endsWith("/") && urlPath.endsWith("/") && (productResourcePath.lastIndexOf("/") === 0)) {
apiproxy = apiproxy + "/";
}
let placeholder = DOUBLE_STAR_PLACEHOLDER;
while (apiproxy.indexOf(placeholder) !== -1) {
placeholder = '_' + DOUBLE_STAR_PLACEHOLDER + '_' + placeholder + '_';
}
let regExPatternStr = apiproxy.replace(/\*\*/g, placeholder);
regExPatternStr = regExPatternStr.replace(/\*/g, '\\w+');
placeholder = new RegExp(placeholder, "g");
regExPatternStr = regExPatternStr.replace(placeholder, '.*');
try {
var proxyRegEx = new RegExp(`^${regExPatternStr}$`, 'ig');
matchesProxyRules = urlPath.match(proxyRegEx);
} catch (e) {
debug('Exception in generating regex for the pattern :', proxyRegEx);
logger.eventLog({ level: 'warn', req: req, res: res, err: e, component: componentName }, 'Exception in generating regex for the pattern');
}
}
})
} else {
matchesProxyRules = true;
}
debug("matches proxy rules: " + matchesProxyRules);
//add pattern matching here
if (!productOnly) {
return matchesProxyRules && validProxyNames.indexOf(proxy.name) >= 0;
} else {
return matchesProxyRules;
}
});
}