fastify-autosecurity

0.7.4 • Public • Published

fastify-autosecurity

Logo

JavaScript   TypeScript

NPM version NPM downloads Known Vulnerabilities GitHub license

CI Coverage Status

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

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

📰 Full Documentation

fastify-autosecurity

🚀 Install

npm install --save fastify-autosecurity

📘 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
  }
})

📄 License

Licensed under MIT

✨ Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification.

Contributions of any kind welcome!

Readme

Keywords

none

Package Sidebar

Install

npm i fastify-autosecurity

Weekly Downloads

17

Version

0.7.4

License

MIT

Unpacked Size

31.9 kB

Total Files

11

Last publish

Collaborators

  • giovanni_cardamone
  • giovannicardamone