UNPKG

4.57 kBMarkdownView Raw
1# fastify-guard
2> A simple user role and scope check plugin to protect endpoints for [Fastify](https://github.com/fastify/fastify).
3
4[![Downloads](https://img.shields.io/npm/dm/fastify-guard.svg)](https://npmjs.com/fastify-guard)
5[![install size](https://packagephobia.com/badge?p=fastify-guard)](https://packagephobia.com/result?p=fastify-guard)
6
7`fastify-guard` is designed to protect API endpoints by checking authenticated user roles and/or scopes if they met. `guard` is the registered Fastify decorator and can be used in anywhere.
8
9Inspired by [express-jwt-permissions](https://github.com/MichielDeMey/express-jwt-permissions).
10
11**Note:** Fastify v4 support is shipped with v2.0.0.
12
13## Install
14```
15$ npm install fastify-guard
16```
17
18## Usage
19
20```js
21const fastify = require('fastify')()
22const fastifyGuard = require('fastify-guard')
23
24fastify.register(
25 fastifyGuard,
26 {
27 errorHandler: (result, req, reply) => {
28 return reply.send('you are not allowed to call this route')
29 }
30 }
31)
32
33// this route can only be called by users who has 'cto' and 'admin' roles
34fastify.get(
35 '/admin',
36 { preHandler: [fastify.guard.role(['cto', 'admin'])] },
37 (req, reply) => {
38 // 'user' should already be defined in req object
39 reply.send(req.user)
40 }
41)
42
43// this route can only be called by users who has 'admin' or 'editor' role
44fastify.get(
45 '/',
46 { preHandler: [fastify.guard.role('admin', 'editor')] },
47 (req, reply) => {
48 // 'user' should already be defined in req object
49 reply.send(req.user)
50 }
51)
52
53/*
54http://localhost:3000 -> will print out below result if the authenticated user does not have 'admin' role
55
56you are not allowed to call this route
57*/
58
59fastify.get(
60 '/has-role',
61 (req, reply) => {
62 // 'user' should already be defined in req object
63 reply.send(
64 fastify.guard.hasRole(req, 'admin') // will return a boolean value
65 )
66 }
67)
68
69fastify.get(
70 '/has-scope',
71 (req, reply) => {
72 // 'user' should already be defined in req object
73 reply.send(
74 fastify.guard.hasScope(req, 'profile') // will return a boolean value
75 )
76 }
77)
78
79fastify.listen(3000, () => {
80 console.log('Fastify server is running on port: 3000')
81})
82
83/*
84http://localhost:3000 -> will print out below result if the authenticated user does not have 'admin' role
85
86you are not allowed to call this route
87*/
88```
89
90## Options
91
92| Name | Type | Default | Description |
93| --- | --- | --- | --- |
94| requestProperty | string | `user` | The authenticated user property name that fastify-guard will search in request object |
95| roleProperty | string | `role` | The role property name that fastify-guard will search in authenticated user object |
96| scopeProperty | string | `scope` | The scope property name that fastify-guard will search in authenticated user object |
97| errorHandler | function | undefined | Custom error handler to manipulate the response that will be returned. As fallback, default HTTP error messages will be returned. |
98
99## API
100
101### `guard.role(role)`
102
103Returns a function which checks if the authenticated user has the given role(s). Multiple roles can be sent as separated parameters or in an array. If the given role(s) was not assigned to the authenticated user, the function will throw an HTTP Error (if no errorHandler provided in options otherwise the errorHandler will be invoked). The function supposed to be used in `preHandler` hook.
104
105### `guard.hasRole(request, role)`
106
107Returns a boolean value which indicates the authenticated user has the given role.
108
109`request` is the Fastify request object
110
111`role` is role name
112
113### `guard.scope(scope)`
114
115Returns a function which checks if the authenticated user has the given scope(s). Multiple scopes can be sent as separated parameters or in an array. If the given scope(s) was not assigned to the authenticated user, the function will throw an HTTP Error (if no errorHandler provided in options otherwise the errorHandler will be invoked). The function supposed to be used in `preHandler` hook.
116
117### `guard.hasScope(request, scope)`
118
119Returns a boolean value which indicates the authenticated user has the given scope.
120
121`request` is the Fastify request object
122
123`scope` is scope name