-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
426 lines (375 loc) · 12.3 KB
/
app.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'use strict';
const rootPrefix = '.';
const express = require('express'),
path = require('path'),
createNamespace = require('continuation-local-storage').createNamespace,
morgan = require('morgan'),
bodyParser = require('body-parser'),
helmet = require('helmet'),
customUrlParser = require('url');
const jwtAuth = require(rootPrefix + '/lib/jwt/jwtAuth'),
createErrorLogsEntry = require(rootPrefix + '/lib/errorLogs/createEntry'),
ErrorLogsConstants = require(rootPrefix + '/lib/globalConstant/errorLogs'),
responseHelper = require(rootPrefix + '/lib/formatter/response'),
v2Routes = require(rootPrefix + '/routes/v2/index'),
internalRoutes = require(rootPrefix + '/routes/internal/index'),
elbHealthCheckerRoute = require(rootPrefix + '/routes/internal/elb_health_checker'),
ValidateApiSignature = require(rootPrefix + '/lib/validateApiSignature/Factory'),
logger = require(rootPrefix + '/lib/logger/customConsoleLogger'),
customMiddleware = require(rootPrefix + '/helpers/customMiddleware'),
SystemServiceStatusesCacheKlass = require(rootPrefix + '/lib/cacheManagement/shared/SystemServiceStatus'),
apiVersions = require(rootPrefix + '/lib/globalConstant/apiVersions'),
environmentInfo = require(rootPrefix + '/lib/globalConstant/environmentInfo'),
basicHelper = require(rootPrefix + '/helpers/basic'),
errorConfig = basicHelper.fetchErrorConfig(apiVersions.internal),
apiName = require(rootPrefix + '/lib/globalConstant/apiName'),
webhookRoutes = require(rootPrefix + '/routes/webhooks/index'),
AuthenticateApiByWebhookKeySecret = require(rootPrefix + '/lib/validateApiSignature/ByWebhookKeySecret'),
apiSignature = require(rootPrefix + '/lib/globalConstant/apiSignature'),
sanitizer = require(rootPrefix + '/helpers/sanitizer');
const requestSharedNameSpace = createNamespace('saasApiNameSpace'),
systemServiceStatusesCache = new SystemServiceStatusesCacheKlass({});
morgan.token('id', function getId(req) {
return req.id;
});
morgan.token('endTime', function getendTime(req) {
let hrTime = process.hrtime();
return hrTime[0] * 1000 + hrTime[1] / 1000000;
});
morgan.token('endDateTime', function getEndDateTime(req) {
return basicHelper.logDateFormat();
});
morgan.token('memoryUsage', function getMemoryUsage(req) {
const heapDetails = process.memoryUsage();
const used = heapDetails.heapUsed / 1024 / 1024;
return Math.round(used * 100) / 100;
});
const startRequestLogLine = function(req, res, next) {
const used = process.memoryUsage().heapUsed / 1024 / 1024;
let message =
"Started '" +
customUrlParser.parse(req.originalUrl).pathname +
"' '" +
req.method +
"' at '" +
basicHelper.logDateFormat() +
"' with memory '" +
Math.round(used * 100) / 100 +
" MB'";
logger.info(message);
next();
};
/**
* Assign params
*
* @param req
* @param res
* @param next
*/
const assignParams = function(req, res, next) {
// IMPORTANT NOTE: Don't assign parameters before sanitization
// Also override any request params, related to signatures
// And finally assign it to req.decodedParams
req.decodedParams = Object.assign(getRequestParams(req), req.decodedParams);
next();
};
/**
* Get request params
*
* @param req
* @return {*}
*/
const getRequestParams = function(req) {
// IMPORTANT NOTE: Don't assign parameters before sanitization
if (req.method === 'POST') {
return req.body;
} else if (req.method === 'GET' || req.method === 'DELETE') {
return req.query;
} else {
return {};
}
};
/**
* Validate API signature
*
* @param req
* @param res
* @param next
* @return {Promise|*|{$ref}|PromiseLike<T>|Promise<T>}
*/
const validateApiSignature = function(req, res, next) {
let inputParams = getRequestParams(req);
const handleParamValidationResult = function(result) {
if (result.isSuccess()) {
if (!req.decodedParams) {
req.decodedParams = {};
}
// NOTE: MAKE SURE ALL SANITIZED VALUES ARE ASSIGNED HERE
req.decodedParams['client_id'] = result.data['clientId'];
req.decodedParams['token_id'] = result.data['tokenId'];
req.decodedParams['user_data'] = result.data['userData'];
req.decodedParams['app_validated_api_name'] = result.data['appValidatedApiName'];
req.decodedParams['api_signature_kind'] = result.data['apiSignatureKind'];
req.decodedParams['token_shard_details'] = result.data['tokenShardDetails'];
next();
} else {
return result.renderResponse(res, errorConfig);
}
};
// Following line always gives resolution. In case this assumption changes, please add catch here.
return new ValidateApiSignature({
inputParams: inputParams,
requestPath: customUrlParser.parse(req.originalUrl).pathname,
requestMethod: req.method
})
.perform()
.then(handleParamValidationResult);
};
const validateWebhookSignature = function(req, res, next) {
console.log('I am in app.js validateWebhookSignature');
let inputParams = getRequestParams(req);
//Object.assign(inputParams, req.headers);
console.log('---------inputParams---', inputParams);
console.log('---------req.headers---', req.headers);
// Following line always gives resolution. In case this assumption changes, please add catch here.
return new AuthenticateApiByWebhookKeySecret({
inputParams: inputParams,
requestHeaders: req.headers
})
.perform()
.then(function(resp) {
if (resp.isSuccess()) {
console.log('----------validateWebhookSignature-------resp--------------', resp);
next();
} else {
return resp.renderResponse(res, errorConfig);
}
});
};
// before action for verifying the jwt token and setting the decoded info in req obj
const decodeJwt = function(req, res, next) {
let token;
if (req.method === 'POST' || req.method === 'DELETE') {
token = req.body.token || '';
} else if (req.method === 'GET') {
token = req.query.token || '';
}
// Set the decoded params in the re and call the next in control flow.
const jwtOnResolve = function(reqParams) {
req.decodedParams = sanitizer.sanitizeParams(reqParams.data);
req.decodedParams['app_validated_api_name'] = apiName.allInternalRoutes;
// Validation passed.
return next();
};
// send error, if token is invalid
const jwtOnReject = function(err) {
return responseHelper
.error({
internal_error_identifier: 'a_1',
api_error_identifier: 'invalid_or_expired_jwt_token',
debug_options: {}
})
.renderResponse(res, errorConfig);
};
// Verify token
Promise.resolve(jwtAuth.verifyToken(token, 'saasApi').then(jwtOnResolve, jwtOnReject)).catch(function(err) {
const errorObject = responseHelper.error({
internal_error_identifier: 'jwt_decide_failed:a_2',
api_error_identifier: 'jwt_decide_failed',
debug_options: { token: token }
});
createErrorLogsEntry.perform(errorObject, ErrorLogsConstants.lowSeverity);
logger.error('a_2', 'JWT Decide Failed', { token: token });
return responseHelper
.error({
internal_error_identifier: 'a_2',
api_error_identifier: 'something_went_wrong',
debug_options: {}
})
.renderResponse(res, errorConfig);
});
};
// Set request debugging/logging details to shared namespace
const appendRequestDebugInfo = function(req, res, next) {
requestSharedNameSpace.run(function() {
requestSharedNameSpace.set('reqId', req.id);
requestSharedNameSpace.set('startTime', req.startTime);
next();
});
};
// In order to put Saas into maintenance, set systemServiceStatusesCache with saas_api_available = 0
const checkSystemServiceStatuses = async function(req, res, next) {
const statusRsp = await systemServiceStatusesCache.fetch();
if (statusRsp.isSuccess && statusRsp.data && statusRsp.data['saas_api_available'] != 1) {
return responseHelper
.error({
internal_error_identifier: 'a_4',
api_error_identifier: 'api_under_maintenance',
debug_options: {}
})
.renderResponse(res, errorConfig);
}
logger.debug('completed checking checkSystemServiceStatuses');
next();
};
/**
* Handle deprecated routes
* @param req
* @param res
* @param next
*/
const handleDepricatedRoutes = function(req, res, next) {
return responseHelper
.error({
internal_error_identifier: 'a_8',
api_error_identifier: 'unsupported_routes',
debug_options: {}
})
.renderResponse(res, errorConfig);
};
/**
* Append internal version
*
* @param req
* @param res
* @param next
*/
const appendInternalVersion = function(req, res, next) {
req.decodedParams.apiVersion = apiVersions.internal;
next();
};
/**
* Append V2 version
*
* @param req
* @param res
* @param next
*/
const appendV2Version = function(req, res, next) {
req.decodedParams.apiVersion = apiVersions.v2;
next();
};
/**
* Add default signature kind in request params
*
* @param req
* @param res
* @param next
*/
const addDefaultSignatureKind = function(req, res, next) {
req.query = req.query || {};
req.query.api_signature_kind = req.query.api_signature_kind || apiSignature.noAuthKind;
next();
};
// Set worker process title
process.title = 'Company Restful API node worker';
// Create express application instance
const app = express();
// Add id and startTime to request
app.use(customMiddleware());
// Load Morgan
app.use(
morgan(
'[:id][:endTime] Completed with ":status" in :response-time ms with :memoryUsage MB at :endDateTime - ":res[content-length] bytes" - ":remote-addr" ":remote-user" - "HTTP/:http-version :method :url" - ":referrer" - ":user-agent"'
)
);
app.use(function(req, res, next) {
var data = '';
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.rawBody = data;
});
next();
});
// Helmet helps secure Express apps by setting various HTTP headers.
app.use(helmet());
// Node.js body parsing middleware.
app.use(bodyParser.json());
// Parsing the URL-encoded data with the qs library (extended: true)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// Mark older routes as UNSUPPORTED_VERSION
app.use('/transaction-types', handleDepricatedRoutes);
app.use('/users', handleDepricatedRoutes);
app.use('/v1', handleDepricatedRoutes);
app.use('/v1.1', handleDepricatedRoutes);
// Following are the routes
app.use('/health-checker', elbHealthCheckerRoute);
app.use(
'/' + environmentInfo.urlPrefix + '/test_webhook',
startRequestLogLine,
appendRequestDebugInfo,
validateWebhookSignature,
sanitizer.sanitizeBodyAndQuery,
assignParams,
internalRoutes
);
/**
* NOTE: SLACK webhooks
*/
app.use('/' + environmentInfo.urlPrefix + '/webhooks', webhookRoutes);
/*
The sanitizer piece of code should always be before routes for jwt and after validateApiSignature for sdk.
Docs: https://www.npmjs.com/package/sanitize-html
*/
app.use(
'/' + environmentInfo.urlPrefix + '/internal',
startRequestLogLine,
checkSystemServiceStatuses,
appendRequestDebugInfo,
sanitizer.sanitizeBodyAndQuery,
decodeJwt,
appendInternalVersion,
internalRoutes
);
app.use(addDefaultSignatureKind);
app.use(
'/' + environmentInfo.urlPrefix + '/v2',
startRequestLogLine,
checkSystemServiceStatuses,
appendRequestDebugInfo,
validateApiSignature,
sanitizer.sanitizeBodyAndQuery,
assignParams,
appendV2Version,
v2Routes
);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
let message =
"Started '" +
customUrlParser.parse(req.originalUrl).pathname +
"' '" +
req.method +
"' at " +
basicHelper.logDateFormat();
logger.info(message);
return responseHelper
.error({
internal_error_identifier: 'a_5',
api_error_identifier: 'resource_not_found',
debug_options: {}
})
.renderResponse(res, errorConfig);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
const errorObject = responseHelper.error({
internal_error_identifier: `a_6`,
api_error_identifier: 'something_went_wrong',
debug_options: { err: err }
});
createErrorLogsEntry.perform(errorObject, ErrorLogsConstants.lowSeverity);
logger.error('a_6', 'Something went wrong', err);
return responseHelper
.error({
internal_error_identifier: 'a_6',
api_error_identifier: 'something_went_wrong',
debug_options: {}
})
.renderResponse(res, errorConfig);
});
module.exports = app;