🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

express-json-refiner

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-json-refiner - npm Package Compare versions

Comparing version

to
0.0.3

4

package.json
{
"name": "express-json-refiner",
"version": "0.0.2",
"version": "0.0.3",
"description": "Express JSON refiner middleware by context's scope of api (admin, member...)",

@@ -28,3 +28,3 @@ "keywords": [

"scripts": {
"test": "mocha test/test.spec.js"
"test": "grunt simplemocha"
},

@@ -31,0 +31,0 @@ "dependencies": {

@@ -38,4 +38,5 @@ express-json-refiner [![NPM version](https://badge.fury.io/js/express-json-refiner.png)](http://badge.fury.io/js/express-json-refiner) [![Build Status](https://travis-ci.org/darul75/express-json-refiner.png?branch=master)](https://travis-ci.org/darul75/express-json-refiner) [![Total views](https://sourcegraph.com/api/repos/github.com/darul75/express-json-refiner/counters/views.png)](https://sourcegraph.com/github.com/darul75/express-json-refiner)

* Case1 : no routes defined
```
var o = {
var userAccessRule = {

@@ -47,4 +48,5 @@ model: {field1: '', field2: '', field3: '', field4: ''},

public: ['field1', 'field3'],
member: ['field1', 'field3'],
admin: ['field1', 'field2', 'field3', 'field4']
member: ['field2', 'field3'],
admin: ['field1', 'field2', 'field3', 'field4']
}

@@ -55,2 +57,21 @@

* Case2 : routes defined
```
var userAccessRule = {
model: {field1: '', field2: '', field3: '', field4: ''},
routes: ['/api1/admin/user/*', '/api1/member/user/*'],
fields: {
public: ['field1', 'field3'],
member: ['field2', 'field3'],
admin: ['field1', 'field2', 'field3', 'field4']
}
};
```
## How to use

@@ -79,6 +100,6 @@

load('test/model').then('test/access').into(app);
load('model').then('access').into(app);
// where 'test/model' contains Mongoose model (schema) or arbitrary model
// where 'test/access' contains your json refiner rules, which attributes to keep for each context.
// where 'model' contains Mongoose model (schema) or arbitrary model
// where 'access' contains your json refiner rules, which attributes to keep for each context.

@@ -104,3 +125,8 @@ // result is injection of `model` namespace into `app` variable.

// NOTE before rendering json output, just give api context for refiner to apply.
req.api = {model: 'api1', scope:'admin'};
// CASE 1 : routes not definied => explicitly give rule model, here 'api1"
req.api = {model: 'api1', scope:'public'};
// CASE 2 : routes definied => just scope is needeed
req.api = {scope:'public'};

@@ -114,5 +140,84 @@ // 'api1' is a reference for 'access/api1.js' rule file

// {'field1': '1'}
// {'field1': '1', 'field3': 3}
});
```
`scope` can be set globaly for instance by this way, example made with `passport-http-bearer`
```javascript
var checkAdminAccess = function (req, res, next) {
if (!req.user || req.user.role !== "admin") {
var err = new Error('not allowed!');
err.status = 403;
next(err);
return;
}
req.api = {scope:'admin'};
next();
};
app.all('/api/v1/admin/*', passport.authenticate('bearer', { session: false }), checkAdminAccess);
```
Do not hesitate look at test folder hierarchy files.
Installation
------------
Using npm:
```
npm install express-json-refiner
```
### Build
You can run the tests by running
```
npm install
```
or
```
npm test
```
assuming you already have `grunt` installed, otherwise you also need to do:
```
npm install -g grunt-cli
```
## License
The MIT License (MIT)
Copyright (c) 2014 Julien Valéry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
var _ = require('underscore');
module.exports = function (json, req, options) {
module.exports = function (json, api, options) {
var model = req.api.model; // ex: {model: 'api1', scope:'admin'};
var scope = req.api.scope;
var fields = null;
var model = api.model; // ex: {model: 'api1', scope:'admin'};
var scope = api.scope;
if (!model) { // search it
model = matchingModel(req.route.path, options.rules);
var fields = options.rules[model].fields[scope];
/*var model = options.rules[model].model;*/
if (!model)
return;
fields = model.fields[scope];
}
else {
fields = options.rules[model].fields[scope];
}
if (Array.isArray(json)) {

@@ -32,2 +41,17 @@ json = json.map(function(elt) {

return json;
};
var matchingModel = function(path, rules) {
var find = _.find(rules, function (rule) {
if (rule.routes) {
for (var i=0;i<rule.routes.length;i++) {
var route = rule.routes[i];
if (route.match(path).length > 0)
return true;
}
}
});
return find;
};

@@ -41,4 +41,4 @@ var processor = require('./processor');

if (options.rules && req.api && req.api.model && req.api.scope) {
obj = processor(obj, req.api, options);
if (options.rules && req.api && req.api.scope) {
obj = processor(obj, req, options);
}

@@ -45,0 +45,0 @@

@@ -7,2 +7,4 @@ module.exports = function (app) {

routes: ['/api1/admin/user/*'],
fields: {

@@ -9,0 +11,0 @@ public: ['field1', 'field3'],

@@ -18,3 +18,3 @@ var expect = require('expect.js'),

load('test/model').then('test/access').into(app);
load('test/model', {verbose:true}).then('test/access').into(app);

@@ -31,3 +31,3 @@ var opts = {

req.api = {model: 'api1', scope:'admin'};
req.api = {scope:'admin'};

@@ -34,0 +34,0 @@ res.json(o);

Sorry, the diff of this file is not supported yet