Skip to content

bring master up to date #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions app-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,5 @@ const Joi = require('joi')

Joi.optionalId = () => Joi.string()
Joi.id = () => Joi.optionalId().required()
Joi.page = () =>
Joi.number()
.integer()
.min(1)
.default(1)
Joi.perPage = () =>
Joi.number()
.integer()
.min(1)
.default(20)
Joi.page = () => Joi.number().integer().min(1).default(1)
Joi.perPage = () => Joi.number().integer().min(1).default(20)
41 changes: 9 additions & 32 deletions app-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const authenticator = require('tc-core-library-js').middleware.jwtAuthenticator
* Configure all routes for express app
* @param app the express app
*/
module.exports = app => {
module.exports = (app) => {
// Load all routes
_.each(routes, (verbs, path) => {
_.each(verbs, (def, verb) => {
const controllerPath = `./src/controllers/${def.controller}`
const method = require(controllerPath)[def.method]; // eslint-disable-line
const method = require(controllerPath)[def.method] // eslint-disable-line
if (!method) {
throw new Error(`${def.method} is undefined`)
}
Expand All @@ -32,25 +32,14 @@ module.exports = app => {
// add Authenticator check if route has auth
if (def.auth) {
actions.push((req, res, next) => {
authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(
req,
res,
next
)
authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(req, res, next)
})

actions.push((req, res, next) => {
if (req.authUser.isMachine) {
// M2M
if (
!req.authUser.scopes ||
!helper.checkIfExists(def.scopes, req.authUser.scopes)
) {
next(
new errors.ForbiddenError(
'You are not allowed to perform this action!'
)
)
if (!req.authUser.scopes || !helper.checkIfExists(def.scopes, req.authUser.scopes)) {
next(new errors.ForbiddenError('You are not allowed to perform this action!'))
} else {
next()
}
Expand All @@ -59,20 +48,12 @@ module.exports = app => {
// User
if (req.authUser.roles) {
if (!helper.checkIfExists(def.access, req.authUser.roles)) {
next(
new errors.ForbiddenError(
'You are not allowed to perform this action!'
)
)
next(new errors.ForbiddenError('You are not allowed to perform this action!'))
} else {
next()
}
} else {
next(
new errors.ForbiddenError(
'You are not authorized to perform this action'
)
)
next(new errors.ForbiddenError('You are not authorized to perform this action'))
}
}
})
Expand All @@ -87,13 +68,9 @@ module.exports = app => {
app.use('*', (req, res) => {
const route = routes[req.baseUrl]
if (route) {
res
.status(HttpStatus.METHOD_NOT_ALLOWED)
.json({ message: 'The requested HTTP method is not supported.' })
res.status(HttpStatus.METHOD_NOT_ALLOWED).json({ message: 'The requested HTTP method is not supported.' })
} else {
res
.status(HttpStatus.NOT_FOUND)
.json({ message: 'The requested resource cannot be found.' })
res.status(HttpStatus.NOT_FOUND).json({ message: 'The requested resource cannot be found.' })
}
})
}
6 changes: 2 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ require('./app-routes')(app)
app.use((err, req, res, next) => {
logger.logFullError(err, req.signature || `${req.method} ${req.url}`)
const errorResponse = {}
const status = err.isJoi
? HttpStatus.BAD_REQUEST
: err.httpStatus || HttpStatus.INTERNAL_SERVER_ERROR
const status = err.isJoi ? HttpStatus.BAD_REQUEST : err.httpStatus || HttpStatus.INTERNAL_SERVER_ERROR

if (_.isArray(err.details)) {
if (err.isJoi) {
_.map(err.details, e => {
_.map(err.details, (e) => {
if (e.message) {
if (_.isUndefined(errorResponse.message)) {
errorResponse.message = e.message
Expand Down
Loading