|
2 | 2 | * Configure all routes for express app
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -const _ = require('lodash') |
6 |
| -const config = require('config') |
7 |
| -const HttpStatus = require('http-status-codes') |
8 |
| -const helper = require('./src/common/helper') |
9 |
| -const errors = require('./src/common/errors') |
10 |
| -const routes = require('./src/routes') |
11 |
| -const authenticator = require('tc-core-library-js').middleware.jwtAuthenticator |
12 |
| - |
13 |
| -const AWSXRay = require('aws-xray-sdk') |
| 5 | +const _ = require("lodash"); |
| 6 | +const config = require("config"); |
| 7 | +const HttpStatus = require("http-status-codes"); |
| 8 | +const helper = require("./src/common/helper"); |
| 9 | +const errors = require("./src/common/errors"); |
| 10 | +const routes = require("./src/routes"); |
| 11 | +const authenticator = require("tc-core-library-js").middleware.jwtAuthenticator; |
14 | 12 |
|
15 | 13 | /**
|
16 | 14 | * Configure all routes for express app
|
17 | 15 | * @param app the express app
|
18 | 16 | */
|
19 | 17 | module.exports = (app) => {
|
20 | 18 | // Load all routes
|
| 19 | + _.each(routes, (verbs, path) => { |
| 20 | + _.each(verbs, (def, verb) => { |
| 21 | + const controllerPath = `./src/controllers/${def.controller}`; |
| 22 | + const method = require(controllerPath)[def.method]; // eslint-disable-line |
| 23 | + if (!method) { |
| 24 | + throw new Error(`${def.method} is undefined`); |
| 25 | + } |
21 | 26 |
|
22 |
| - const ns = AWSXRay.getNamespace() |
23 |
| - |
24 |
| - ns.run(() => { |
25 |
| - const rootSegment = new AWSXRay.Segment('v5-challenge-api-custom-segment') |
26 |
| - AWSXRay.setSegment(rootSegment) |
| 27 | + const actions = []; |
| 28 | + actions.push((req, res, next) => { |
| 29 | + req.signature = `${def.controller}#${def.method}`; |
| 30 | + next(); |
| 31 | + }); |
27 | 32 |
|
28 |
| - _.each(routes, (verbs, path) => { |
29 |
| - _.each(verbs, (def, verb) => { |
30 |
| - const controllerPath = `./src/controllers/${def.controller}` |
31 |
| - const method = require(controllerPath)[def.method]; // eslint-disable-line |
32 |
| - if (!method) { |
33 |
| - throw new Error(`${def.method} is undefined`) |
| 33 | + actions.push((req, res, next) => { |
| 34 | + if (_.get(req, "query.token")) { |
| 35 | + _.set(req, "headers.authorization", `Bearer ${_.trim(req.query.token)}`); |
34 | 36 | }
|
35 |
| - |
36 |
| - const actions = [] |
| 37 | + next(); |
| 38 | + }); |
| 39 | + |
| 40 | + if (def.auth) { |
| 41 | + // add Authenticator/Authorization check if route has auth |
37 | 42 | actions.push((req, res, next) => {
|
38 |
| - req.signature = `${def.controller}#${def.method}` |
39 |
| - next() |
40 |
| - }) |
41 |
| - |
| 43 | + authenticator(_.pick(config, ["AUTH_SECRET", "VALID_ISSUERS"]))(req, res, next); |
| 44 | + }); |
| 45 | + |
42 | 46 | actions.push((req, res, next) => {
|
43 |
| - if (_.get(req, 'query.token')) { |
44 |
| - _.set(req, 'headers.authorization', `Bearer ${_.trim(req.query.token)}`) |
45 |
| - } |
46 |
| - next() |
47 |
| - }) |
48 |
| - |
49 |
| - if (def.auth) { |
50 |
| - // add Authenticator/Authorization check if route has auth |
51 |
| - actions.push((req, res, next) => { |
52 |
| - authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(req, res, next) |
53 |
| - }) |
54 |
| - |
55 |
| - actions.push((req, res, next) => { |
56 |
| - if (req.authUser.isMachine) { |
57 |
| - // M2M |
58 |
| - if (!req.authUser.scopes || !helper.checkIfExists(def.scopes, req.authUser.scopes)) { |
59 |
| - next(new errors.ForbiddenError('You are not allowed to perform this action!')) |
60 |
| - } else { |
61 |
| - req.authUser.handle = config.M2M_AUDIT_HANDLE |
62 |
| - req.userToken = req.headers.authorization.split(' ')[1] |
63 |
| - next() |
64 |
| - } |
| 47 | + if (req.authUser.isMachine) { |
| 48 | + // M2M |
| 49 | + if (!req.authUser.scopes || !helper.checkIfExists(def.scopes, req.authUser.scopes)) { |
| 50 | + next(new errors.ForbiddenError("You are not allowed to perform this action!")); |
65 | 51 | } else {
|
66 |
| - req.authUser.userId = String(req.authUser.userId) |
67 |
| - // User roles authorization |
68 |
| - if (req.authUser.roles) { |
69 |
| - if (def.access && !helper.checkIfExists(_.map(def.access, a => a.toLowerCase()), _.map(req.authUser.roles, r => r.toLowerCase()))) { |
70 |
| - next(new errors.ForbiddenError('You are not allowed to perform this action!')) |
71 |
| - } else { |
72 |
| - // user token is used in create/update challenge to ensure user can create/update challenge under specific project |
73 |
| - req.userToken = req.headers.authorization.split(' ')[1] |
74 |
| - next() |
75 |
| - } |
76 |
| - } else { |
77 |
| - next(new errors.ForbiddenError('You are not authorized to perform this action')) |
78 |
| - } |
| 52 | + req.authUser.handle = config.M2M_AUDIT_HANDLE; |
| 53 | + req.userToken = req.headers.authorization.split(" ")[1]; |
| 54 | + next(); |
79 | 55 | }
|
80 |
| - }) |
81 |
| - } else { |
82 |
| - // public API, but still try to authenticate token if provided, but allow missing/invalid token |
83 |
| - actions.push((req, res, next) => { |
84 |
| - const interceptRes = {} |
85 |
| - interceptRes.status = () => interceptRes |
86 |
| - interceptRes.json = () => interceptRes |
87 |
| - interceptRes.send = () => next() |
88 |
| - authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(req, interceptRes, next) |
89 |
| - }) |
90 |
| - |
91 |
| - actions.push((req, res, next) => { |
92 |
| - if (!req.authUser) { |
93 |
| - next() |
94 |
| - } else if (req.authUser.isMachine) { |
95 |
| - if (!def.scopes || !req.authUser.scopes || !helper.checkIfExists(def.scopes, req.authUser.scopes)) { |
96 |
| - req.authUser = undefined |
| 56 | + } else { |
| 57 | + req.authUser.userId = String(req.authUser.userId); |
| 58 | + // User roles authorization |
| 59 | + if (req.authUser.roles) { |
| 60 | + if ( |
| 61 | + def.access && |
| 62 | + !helper.checkIfExists( |
| 63 | + _.map(def.access, (a) => a.toLowerCase()), |
| 64 | + _.map(req.authUser.roles, (r) => r.toLowerCase()) |
| 65 | + ) |
| 66 | + ) { |
| 67 | + next(new errors.ForbiddenError("You are not allowed to perform this action!")); |
| 68 | + } else { |
| 69 | + // user token is used in create/update challenge to ensure user can create/update challenge under specific project |
| 70 | + req.userToken = req.headers.authorization.split(" ")[1]; |
| 71 | + next(); |
97 | 72 | }
|
98 |
| - next() |
99 | 73 | } else {
|
100 |
| - req.authUser.userId = String(req.authUser.userId) |
101 |
| - next() |
| 74 | + next(new errors.ForbiddenError("You are not authorized to perform this action")); |
102 | 75 | }
|
103 |
| - }) |
104 |
| - } |
105 |
| - |
106 |
| - actions.push(method) |
107 |
| - app[verb](`/${config.API_VERSION}${path}`, helper.autoWrapExpress(actions)) |
108 |
| - }) |
109 |
| - }) |
110 |
| - |
111 |
| - // Check if the route is not found or HTTP method is not supported |
112 |
| - app.use('*', (req, res) => { |
113 |
| - if (routes[req.baseUrl]) { |
114 |
| - res.status(HttpStatus.METHOD_NOT_ALLOWED).json({ |
115 |
| - message: 'The requested HTTP method is not supported.' |
116 |
| - }) |
| 76 | + } |
| 77 | + }); |
117 | 78 | } else {
|
118 |
| - res.status(HttpStatus.NOT_FOUND).json({ |
119 |
| - message: 'The requested resource cannot be found.' |
120 |
| - }) |
| 79 | + // public API, but still try to authenticate token if provided, but allow missing/invalid token |
| 80 | + actions.push((req, res, next) => { |
| 81 | + const interceptRes = {}; |
| 82 | + interceptRes.status = () => interceptRes; |
| 83 | + interceptRes.json = () => interceptRes; |
| 84 | + interceptRes.send = () => next(); |
| 85 | + authenticator(_.pick(config, ["AUTH_SECRET", "VALID_ISSUERS"]))(req, interceptRes, next); |
| 86 | + }); |
| 87 | + |
| 88 | + actions.push((req, res, next) => { |
| 89 | + if (!req.authUser) { |
| 90 | + next(); |
| 91 | + } else if (req.authUser.isMachine) { |
| 92 | + if ( |
| 93 | + !def.scopes || |
| 94 | + !req.authUser.scopes || |
| 95 | + !helper.checkIfExists(def.scopes, req.authUser.scopes) |
| 96 | + ) { |
| 97 | + req.authUser = undefined; |
| 98 | + } |
| 99 | + next(); |
| 100 | + } else { |
| 101 | + req.authUser.userId = String(req.authUser.userId); |
| 102 | + next(); |
| 103 | + } |
| 104 | + }); |
121 | 105 | }
|
122 |
| - }) |
123 | 106 |
|
124 |
| - rootSegment.close() |
125 |
| - }) |
126 |
| -} |
| 107 | + actions.push(method); |
| 108 | + app[verb](`/${config.API_VERSION}${path}`, helper.autoWrapExpress(actions)); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + // Check if the route is not found or HTTP method is not supported |
| 113 | + app.use("*", (req, res) => { |
| 114 | + if (routes[req.baseUrl]) { |
| 115 | + res.status(HttpStatus.METHOD_NOT_ALLOWED).json({ |
| 116 | + message: "The requested HTTP method is not supported.", |
| 117 | + }); |
| 118 | + } else { |
| 119 | + res.status(HttpStatus.NOT_FOUND).json({ |
| 120 | + message: "The requested resource cannot be found.", |
| 121 | + }); |
| 122 | + } |
| 123 | + }); |
| 124 | +}; |
0 commit comments