-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from kozzztya/deny-not-allowed
Deny not allowed routes
- Loading branch information
Showing
10 changed files
with
67 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const _ = require('lodash'); | ||
|
||
module.exports = () => (req, res, next) => { | ||
if (!_.has(req, 'acl.allowed')) return res.status(403).send('Route is not allowed.'); | ||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
const _ = require('lodash'); | ||
const ruleChecker = require('./rule-checker'); | ||
const jwtDecode = require('./jwt-decode'); | ||
const denyNotAllowed = require('./deny-not-allowed'); | ||
|
||
module.exports = (configs, { customRules, mongooseConnection, jwt } = {}) => function () { | ||
module.exports = (configs, options = {}) => function () { | ||
const app = this; | ||
if (jwt) app.use(jwtDecode(jwt)); | ||
|
||
const check = ruleChecker({ customRules, mongooseConnection, jwt }); | ||
const check = ruleChecker(options); | ||
if (options.jwt) app.use(jwtDecode(options.jwt)); | ||
|
||
_.forEach(configs, ({ url, method, allow }) => { | ||
|
||
app.all(url, (req, res, next) => { | ||
if (method !== req.method) return next(); | ||
req.acl = { allowed: true }; | ||
|
||
check(req.payload, allow, req) | ||
.then(() => next()) | ||
.catch(err => res.status(err.status || 500).send(err.message)); | ||
}); | ||
|
||
}); | ||
|
||
if (options.denyNotAllowed) app.use(denyNotAllowed()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { test, App } = require('../env'); | ||
|
||
const config = [ | ||
{ | ||
url: '/posts', method: 'GET', | ||
allow: { roles: ['client'] } | ||
} | ||
]; | ||
|
||
test('should be allowed to read for client', async (t) => { | ||
const app = App(config, {}, { roles: ['client'] }); | ||
const { error } = await app.get('/posts'); | ||
t.falsy(error); | ||
}); | ||
|
||
test('should be allowed to delete all if no option set', async (t) => { | ||
const app = App(config, { denyNotAllowed: false }, { roles: ['client'] }); | ||
const { error } = await app.delete('/posts'); | ||
t.falsy(error); | ||
}); | ||
|
||
test('should be denied to delete all for client', async (t) => { | ||
const app = App(config, {}, { roles: ['client'] }); | ||
const { error } = await app.delete('/posts'); | ||
t.is(error.status, 403); | ||
t.is(error.text, 'Route is not allowed.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { test, App } = require('../env'); | ||
|
||
const config = [{ | ||
url: '/posts', method: 'GET', | ||
allow: { roles: ['client'] } | ||
}]; | ||
|
||
test('should work without options', async (t) => { | ||
const app = App(config, undefined, { roles: ['client'] }); | ||
const { error } = await app.get('/posts'); | ||
t.falsy(error); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.