Skip to content

Commit cd1a561

Browse files
author
James Cori
committed
Merge branch 'develop'
2 parents f578fae + 0858e51 commit cd1a561

19 files changed

+2179
-554
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,9 @@ The GroupContains relation contains these fields:
9696
- type: the relationship type, 'group' or 'user'
9797
- roles: the roles of the user in the group
9898
- createdAt: the created at date string
99-
- createdBy: the created by user id
99+
- createdBy: the created by user id
100+
101+
## Swagger UI
102+
103+
- the swagger UI may be browsed at `http://localhost:3000/groups/docs`
104+

Validation.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

app-routes.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,43 @@ module.exports = (app) => {
2929
next()
3030
})
3131

32+
let access = []
3233
// add Authenticator check if route has auth
3334
if (def.auth) {
35+
// default access roles
36+
access = def.access || []
3437
actions.push((req, res, next) => {
3538
authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(req, res, next)
3639
})
37-
3840
actions.push((req, res, next) => {
39-
if (req.authUser.isMachine) {
40-
// M2M
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!'))
41+
if (!req.authUser) {
42+
return next(new errors.UnauthorizedError('Action is not allowed for invalid token'))
43+
}
44+
req.authUser.userId = String(req.authUser.userId)
45+
req.auth = req.authUser
46+
req.auth.sub = req.auth.userId
47+
if (req.authUser.roles) {
48+
// all access are allowed
49+
if (_.isEmpty(access)) {
50+
next()
51+
} else if (!helper.checkIfExists(access, req.authUser.roles)) {
52+
res.forbidden = true
53+
next(new errors.ForbiddenError('You are not allowed to perform this action'))
4354
} else {
4455
next()
4556
}
46-
} else {
47-
req.authUser.userId = String(req.authUser.userId)
48-
// User
49-
if (req.authUser.roles) {
50-
if (!helper.checkIfExists(def.access, req.authUser.roles)) {
51-
next(new errors.ForbiddenError('You are not allowed to perform this action!'))
52-
} else {
53-
next()
54-
}
57+
} else if (req.authUser.scopes) {
58+
if (_.isNil(def.scopes) || _.isEmpty(def.scopes)) {
59+
next()
60+
} else if (!helper.checkIfExists(def.scopes, req.authUser.scopes)) {
61+
next(new errors.ForbiddenError('You are not allowed to perform this action!'))
5562
} else {
56-
next(new errors.ForbiddenError('You are not authorized to perform this action'))
63+
next()
5764
}
65+
} else if ((_.isArray(def.access) && def.access.length > 0) || (_.isArray(def.scopes) && def.scopes.length > 0)) {
66+
next(new errors.UnauthorizedError('You are not authorized to perform this action'))
67+
} else {
68+
next()
5869
}
5970
})
6071
}

0 commit comments

Comments
 (0)