# express-swagger-rbac

> Express Swagger RBAC

Latest version **2.2.0** (published 2019-02-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-swagger-rbac
pnpm add express-swagger-rbac
yarn add express-swagger-rbac
bun add express-swagger-rbac
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2019-02-05 |
| First published | 2019-02-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10.6.0 |
| Dependencies | 1 |
| Unpacked size | 9.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | daniele@xibitdigital.com |
| Maintainers | xibitdigital |
| Keywords | express, swagger, rbac |

## Links

- npm: https://www.npmjs.com/package/express-swagger-rbac
- Repository: https://github.com/xibitdigital/express-swagger-rbac
- Homepage: https://github.com/xibitdigital/express-swagger-rbac#readme
- Issues: https://github.com/xibitdigital/express-swagger-rbac/issues
- npm.io page: https://npm.io/package/express-swagger-rbac

## Dependencies (1)

- [ramda](https://npm.io/package/ramda.md) ^0.26.1

## Recent versions

- 2.2.0 (latest) — 2019-02-05
- 2.1.0 — 2019-02-05
- 2.0.0 — 2019-02-04
- 1.0.2 — 2019-02-04
- 1.0.1 — 2019-02-04
- 1.0.0 — 2019-02-02

## README

# Swagger RBAC middleware

[![Build Status](https://travis-ci.com/xibitdigital/express-swagger-rbac.svg?branch=master)](https://travis-ci.com/xibitdigital/express-swagger-rbac)[![Known Vulnerabilities](https://snyk.io/test/github/xibitdigital/express-swagger-rbac/badge.svg?targetFile=package.json)](https://snyk.io/test/github/xibitdigital/express-swagger-rbac?targetFile=package.json)

Simple middleware with RBAC on JSON swagger document

```javascript
{
  swagger: "2.0",
  produces: ["application/json"],
  host: "localhost:3001",
  basePath: "/v1",
  paths: {
    "/testfoo/:type/:id": {
      get: {
        tags: ["/foo"],
        rbac: ["group1"]
      }
    },
    "/testfoo/:foo/:foo/:foo": {
      get: {
        "x-swagger-router-controller": "foo",
        operationId: "fooControllerWithPage",
        tags: ["/foo"],
        rbac: ["group1"]
      }
    },
    "/testfoobar/:foo": {
      get: {
        "x-swagger-router-controller": "foobar",
        operationId: "foobarController",
        tags: ["/foobar"]
      }
    }
  }
};
```

If the RABC role is defined in the route the middleware will match it with what's defined in `req.groups`.

Example:

```javascript
const swaggerDoc = {
  swagger: "2.0",
  produces: ["application/json"],
  host: "localhost:3001",
  basePath: "/v1",
  paths: {
    "/testfoo/:type/:id": {
      get: {
        tags: ["/foo"],
        rbac: ["group1"]
      }
    },
    "/testfoo/:foo/:foo/:foo": {
      get: {
        "x-swagger-router-controller": "foo",
        operationId: "fooControllerWithPage",
        tags: ["/foo"],
        rbac: ["group1"]
      }
    },
    "/testfoobar/:foo": {
      get: {
        "x-swagger-router-controller": "foobar",
        operationId: "foobarController",
        tags: ["/foobar"]
      }
    }
  }
};

const config = swaggerDocToConf(swaggerDoc);

app.use((req, res, next) => {
  req.groups = ["group1"]; // adding groups to req
  next();
});

// setting middleware
app.use(permitMiddleware(config));

app.get("/v1/testfoo/:foo/:foo", (req, res, next) => {
  res.sendStatus(200);
  next();
});

const response = await request(app)
  .get("/v1/testfoo/1/1")
  .expect(200)
  .end((err, res) => {
    if (err) throw err;
  });
```

For all the endpoints that have no rbac defined the middleware will make the request got through.
Please check the tests for more examples.

---
_Source: https://npm.io/package/express-swagger-rbac · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
