0.7.4 • Published 2 years ago

fastify-autosecurity v0.7.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

fastify-autosecurity

Logo

JavaScript   TypeScript

NPM version NPM downloads Known Vulnerabilities GitHub license

CI Coverage Status

:star: Thanks to everyone who has starred the project, it means a lot!

plugin to handle securities in fastify automatically based on file name.

:newspaper: Full Documentation

fastify-autosecurity

:rocket: Install

npm install --save fastify-autosecurity

:blue_book: Usage

Register plugin

const fastify = require('fastify')
const server = fastify()

server.register(require('fastify-autosecurity'), {
  dir: './<autosecurity-directory>', // relative to your cwd
})

Create file in autosecurity directory

//file: `<autosecurity-directory>/some/route.js`
//url:  `http://your-host/some/route`

export default (fastifyInstance) => ({
  security: {
    type: 'basic,
  },
  handle: async (token) => {
    return { user: 'my user id from token', whatever: 'something else i got from my application', scopes: ['user-basic', 'admin'] }
  },
  validScopes: ['user-basic', 'manager', 'consultant', 'admin'],
  scopes: async (myUser, scopesOfRote) => {
    return true // your logic to check scopes of user against scopes required in route
  }
})

Using typescript support for module

//file: `<autosecurity-directory>/some/route.ts`
//url:  `http://your-host/some/route`

import { FastifyInstance } from 'fastify'


interface MyUser {
  user: string
  whatever: string
  scopes: string[]
}

export default (fastify: FastifyInstance): StrictBasicAuthSecurity<MyUser> => ({
  security: {
    type: 'basic,
  },
  handle: async (token: string) => {
    return { user: 'my user id from token', whatever: 'something else i got from my application', scopes: ['user-basic', 'admin'] }
  },
  validScopes: ['user-basic', 'manager', 'consultant', 'admin'],
  scopes: async (myUser: MyUser, scopesOfRote: string[]) => {
    return true // your logic to check scopes of user against scopes required in route
  }
})

:page_facing_up: License

Licensed under MIT

:sparkles: Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification.

Contributions of any kind welcome!

0.7.2

2 years ago

0.6.3

2 years ago

0.7.4

2 years ago

0.7.3

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.5

3 years ago

0.1.1

3 years ago

0.0.1

3 years ago