@@ -13,12 +13,12 @@ const authenticator = require('tc-core-library-js').middleware.jwtAuthenticator
13
13
* Configure all routes for express app
14
14
* @param app the express app
15
15
*/
16
- module . exports = app => {
16
+ module . exports = ( app ) => {
17
17
// Load all routes
18
18
_ . each ( routes , ( verbs , path ) => {
19
19
_ . each ( verbs , ( def , verb ) => {
20
20
const controllerPath = `./src/controllers/${ def . controller } `
21
- const method = require ( controllerPath ) [ def . method ] ; // eslint-disable-line
21
+ const method = require ( controllerPath ) [ def . method ] // eslint-disable-line
22
22
if ( ! method ) {
23
23
throw new Error ( `${ def . method } is undefined` )
24
24
}
@@ -32,25 +32,14 @@ module.exports = app => {
32
32
// add Authenticator check if route has auth
33
33
if ( def . auth ) {
34
34
actions . push ( ( req , res , next ) => {
35
- authenticator ( _ . pick ( config , [ 'AUTH_SECRET' , 'VALID_ISSUERS' ] ) ) (
36
- req ,
37
- res ,
38
- next
39
- )
35
+ authenticator ( _ . pick ( config , [ 'AUTH_SECRET' , 'VALID_ISSUERS' ] ) ) ( req , res , next )
40
36
} )
41
37
42
38
actions . push ( ( req , res , next ) => {
43
39
if ( req . authUser . isMachine ) {
44
40
// M2M
45
- if (
46
- ! req . authUser . scopes ||
47
- ! helper . checkIfExists ( def . scopes , req . authUser . scopes )
48
- ) {
49
- next (
50
- new errors . ForbiddenError (
51
- 'You are not allowed to perform this action!'
52
- )
53
- )
41
+ if ( ! req . authUser . scopes || ! helper . checkIfExists ( def . scopes , req . authUser . scopes ) ) {
42
+ next ( new errors . ForbiddenError ( 'You are not allowed to perform this action!' ) )
54
43
} else {
55
44
next ( )
56
45
}
@@ -59,20 +48,12 @@ module.exports = app => {
59
48
// User
60
49
if ( req . authUser . roles ) {
61
50
if ( ! helper . checkIfExists ( def . access , req . authUser . roles ) ) {
62
- next (
63
- new errors . ForbiddenError (
64
- 'You are not allowed to perform this action!'
65
- )
66
- )
51
+ next ( new errors . ForbiddenError ( 'You are not allowed to perform this action!' ) )
67
52
} else {
68
53
next ( )
69
54
}
70
55
} else {
71
- next (
72
- new errors . ForbiddenError (
73
- 'You are not authorized to perform this action'
74
- )
75
- )
56
+ next ( new errors . ForbiddenError ( 'You are not authorized to perform this action' ) )
76
57
}
77
58
}
78
59
} )
@@ -87,13 +68,9 @@ module.exports = app => {
87
68
app . use ( '*' , ( req , res ) => {
88
69
const route = routes [ req . baseUrl ]
89
70
if ( route ) {
90
- res
91
- . status ( HttpStatus . METHOD_NOT_ALLOWED )
92
- . json ( { message : 'The requested HTTP method is not supported.' } )
71
+ res . status ( HttpStatus . METHOD_NOT_ALLOWED ) . json ( { message : 'The requested HTTP method is not supported.' } )
93
72
} else {
94
- res
95
- . status ( HttpStatus . NOT_FOUND )
96
- . json ( { message : 'The requested resource cannot be found.' } )
73
+ res . status ( HttpStatus . NOT_FOUND ) . json ( { message : 'The requested resource cannot be found.' } )
97
74
}
98
75
} )
99
76
}
0 commit comments