forked from CloudBoost/cloudboost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.js
196 lines (175 loc) · 5.1 KB
/
middlewares.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const q = require('q');
const uuid = require('uuid');
const winston = require('winston');
const apiTracker = require('./database-connect/apiTracker');
const utilHelper = require('./helpers/util');
const sessionHelper = require('./helpers/session');
const appService = require('./services/app');
function ignoreUrl(requestUrl) {
try {
const ignoreUrls = [ // for the routes to check whether the particular service is active/not
'/api/userService',
'/api/customService',
'/api/roleService',
'/api/status',
'/file',
'/api/createIndex',
'/pages',
'/status',
];
for (let i = 0; i < ignoreUrls.length; i++) {
if (requestUrl.indexOf(ignoreUrls[i]) >= 0) {
return true;
}
}
return false;
} catch (err) {
winston.log('error', {
error: String(err),
stack: new Error().stack,
});
throw err;
}
}
function _setSession(req, res) {
try {
if (!req.session) {
req.session = {};
req.session.id = uuid.v1();
}
res.header('sessionID', req.session.id);
const obj = {
id: req.session.id,
userId: null,
loggedIn: false,
appId: null,
email: null,
roles: null,
};
req.session = obj;
const expireDays = 30; // Default
sessionHelper.saveSession(obj, expireDays);
} catch (err) {
winston.log('error', {
error: String(err),
stack: new Error().stack,
});
}
}
module.exports = (app) => {
app.use((req, res, next) => {
if (req.is('text/*')) {
req.text = '';
req.setEncoding('utf8');
req.on('data', (chunk) => {
req.text += chunk;
});
req.on('end', next);
} else {
next();
}
});
// This middleware converts text to JSON.
app.use((req, res, next) => {
try {
if (req.text && utilHelper._isJSON(req.text)) {
req.body = JSON.parse(req.text);
}
if (req.body && typeof (req.body) === 'string' && utilHelper._isJSON(req.body)) {
req.body = JSON.parse(req.body);
}
// INVALIDATE CACHE FOR API
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); // HTTP 1.1.
res.setHeader('Pragma', 'no-cache'); // HTTP 1.0.
res.setHeader('Expires', '0'); // Proxies.
next();
} catch (e) {
winston.log('error', {
error: String(e),
stack: new Error().stack,
});
// cannot convert to JSON.
next();
}
});
app.use([
'/file/:appId',
'/data/:appId',
'/app/:appId/:tableName',
'/user/:appId',
], (req, res, next) => {
// This is the Middleware for authenticating the app access using appID and key
// check if all the services are loaded first.
try {
if (req.text && utilHelper._isJSON(req.text)) {
req.body = JSON.parse(req.text);
}
if (req.body && typeof (req.body) === 'string' && utilHelper._isJSON(req.body)) {
req.body = JSON.parse(req.body);
}
const requestRecvd = req.originalUrl; // this is the relative url.
if (ignoreUrl(requestRecvd)) {
next();
} else {
const appKey = req.body.key || req.params.key; // key is extracted from body/url parameters
const { appId } = req.params;
if (!appKey) {
res.status(401).send({
status: 'error',
// eslint-disable-next-line max-len
message: "Key not found. You need to have your Client Key or Master Key in the body or url parameter 'key' when you make this request",
});
}
// check if app is in the plan.
const promises = [];
promises.push(apiTracker.isInPlanLimit(appId));
promises.push(appService.isKeyValid(appId, appKey));
q.all(promises).then((result) => {
const isAppKeyValid = result[1];
const isInPlan = result[0];
if (!isInPlan) {
// check if the appIsReleased.
apiTracker.log(appId, 'isReleased/isReleased', '', 'JavaScript', true);
res.status(402).send('Reached Plan Limit. Upgrade Plan.');
} else if (!isAppKeyValid) {
res.status(401).send('App ID or App Key is invalid.');
} else {
next();
}
}, err => res.status(500).send(err.message));
}
} catch (err) {
winston.log('error', {
error: String(err),
stack: new Error().stack,
});
}
});
app.use((req, res, next) => {
try {
// Middleware for retrieving sessions
res.header('Access-Control-Expose-Headers', 'sessionID');
if (req.headers.sessionid) {
res.header('sessionID', req.headers.sessionid);
sessionHelper.getSession(req.headers.sessionid, (err, session) => {
if (!err) {
req.session = session;
next();
} else {
req.session = {};
req.session.id = req.header.sessionid;
next();
}
});
} else {
_setSession(req, res);
next();
}
} catch (err) {
winston.log('error', {
error: String(err),
stack: new Error().stack,
});
}
});
};