Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-casbin

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-casbin - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

3

package.json
{
"name": "fastify-casbin",
"version": "1.0.0",
"version": "2.0.0",
"description": "Plugin for fastify to add generic support for Casbin",

@@ -9,2 +9,3 @@ "main": "plugin.js",

"test": "tap test/*.test.js && npm run typescript",
"test:ci": "tap --coverage-report=lcov test/*.test.js && npm run typescript",
"lint": "standard | snazzy",

@@ -11,0 +12,0 @@ "lint:fix": "standard --fix | snazzy",

/// <reference types="node" />
import { FastifyPluginAsync } from 'fastify'
import { Adapter, Enforcer, Watcher } from 'casbin'
import { Adapter, Enforcer, Watcher, Model } from 'casbin'

@@ -15,4 +15,4 @@ declare module 'fastify' {

export interface FastifyCasbinOptions {
modelPath: string
adapter: string | Adapter
model: string | Model
adapter?: string | Adapter
watcher?: Watcher

@@ -19,0 +19,0 @@ }

@@ -6,4 +6,5 @@ 'use strict'

async function fastifyCasbin (fastify, { modelPath, adapter, watcher }) {
const enforcer = await newEnforcer(modelPath, adapter)
async function fastifyCasbin (fastify, { model, adapter, watcher }) {
const enforcerParams = adapter ? [model, adapter] : [model]
const enforcer = await newEnforcer(...enforcerParams)

@@ -14,3 +15,5 @@ if (watcher) {

await enforcer.loadPolicy()
if (adapter) {
await enforcer.loadPolicy()
}

@@ -17,0 +20,0 @@ fastify.onClose(async () => {

# fastify-casbin
![Continuous Integration](https://github.com/nearform/fastify-casbin/workflows/ci/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/nearform/fastify-casbin/badge.svg?branch=)](https://coveralls.io/github/nearform/fastify-casbin?branch=)
[![codecov](https://codecov.io/gh/nearform/fastify-casbin/branch/master/graph/badge.svg?token=gfJ55XYZAV)](https://codecov.io/gh/nearform/fastify-casbin)
[![npm version](https://badge.fury.io/js/fastify-casbin.svg)](https://badge.fury.io/js/fastify-casbin)

@@ -35,3 +35,3 @@

fastify.register(require('fastify-casbin'), {
modelPath: 'basic_model.conf', // the model configuration
model: 'basic_model.conf', // the model configuration
adapter: 'basic_policy.csv' // the adapter

@@ -64,3 +64,3 @@ })

fastify.register(require('fastify-casbin'), {
modelPath: 'basic_model.conf', // the model configuration
model: 'basic_model.conf', // the model configuration
adapter: await newAdapter(pgOptions), // the adapter

@@ -84,4 +84,34 @@ watcher: await newWatcher(pgOptions) // the watcher

### Using programmatically assembled model
```typescript
import fastify from 'fastify'
import { join } from 'path'
import { Model, FileAdapter } from 'casbin'
const modelPath = join(__dirname, 'auth', 'basic_model.conf')
const policyPath = join(__dirname, 'auth', 'basic_policy.csv')
const app = fastify()
const preloadedModel = new Model()
preloadedModel.loadModel(modelPath)
const preloadedAdapter = new FileAdapter(policyPath)
fastify.register(plugin, {
model: preloadedModel,
adapter: preloadedAdapter
})
fastify.get('/protected', async () => {
if (!(await fastify.casbin.enforce('alice', 'data1', 'read'))) {
throw new Error('Forbidden')
}
return `You're in!`
})
```
## License
Licensed under [MIT License](./LICENSE)

@@ -9,2 +9,3 @@ 'use strict'

const sinon = require('sinon')
const { Model, FileAdapter } = require('casbin')

@@ -22,3 +23,3 @@ const plugin = require('../')

fastify.register(plugin, {
modelPath,
model: modelPath,
adapter: policyPath

@@ -35,2 +36,42 @@ })

test('preloaded model and adapter should be accepted', t => {
t.plan(2)
const fastify = Fastify()
const preloadedModel = new Model()
preloadedModel.loadModel(modelPath)
const preloadedAdapter = new FileAdapter(policyPath)
fastify.register(plugin, {
model: preloadedModel,
adapter: preloadedAdapter
})
fastify.ready(err => {
t.error(err)
t.ok(fastify.casbin)
fastify.close()
})
})
test('adapter can be omitted for in-memory storage', t => {
t.plan(2)
const fastify = Fastify()
const preloadedModel = new Model()
preloadedModel.loadModel(modelPath)
fastify.register(plugin, {
model: preloadedModel
})
fastify.ready(err => {
t.error(err)
t.ok(fastify.casbin)
fastify.close()
})
})
test('casbinJsGetPermissionForUser should exist', t => {

@@ -42,3 +83,3 @@ t.plan(2)

fastify.register(plugin, {
modelPath,
model: modelPath,
adapter: policyPath

@@ -73,3 +114,3 @@ })

fastify.register(plugin, {
modelPath,
model: modelPath,
adapter: policyPath

@@ -107,3 +148,3 @@ })

fastify.register(plugin, {
modelPath,
model: modelPath,
adapter: policyPath

@@ -145,3 +186,3 @@ })

fastify.register(plugin, {
modelPath,
model: modelPath,
adapter: policyPath,

@@ -187,3 +228,3 @@ watcher

fastify.register(plugin, {
modelPath,
model: modelPath,
adapter,

@@ -190,0 +231,0 @@ watcher

@@ -1,2 +0,2 @@

import { Enforcer, FileAdapter, Watcher } from 'casbin'
import { Enforcer, FileAdapter, Model, Watcher } from 'casbin'
import fastify from 'fastify'

@@ -25,9 +25,20 @@ import { expectAssignable, expectType } from 'tsd'

server.register(fastifyCasbin, {
modelPath: 'some file',
model: 'some file',
adapter: 'some adapter',
})
// in-memory adapter
server.register(fastifyCasbin, {
model: 'some file'
})
const model: Model = {} as Model
server.register(fastifyCasbin, {
model: model,
adapter: new FileAdapter('some file'),
})
// typed adapter
server.register(fastifyCasbin, {
modelPath: 'some file',
model: 'some file',
adapter: new FileAdapter('some file'),

@@ -38,3 +49,3 @@ })

server.register(fastifyCasbin, {
modelPath: 'some file',
model: 'some file',
adapter: 'some adapter',

@@ -41,0 +52,0 @@ watcher: new TestWatcher(),

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc