• Stars
    star
    340
  • Rank 119,988 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JWT utils for Fastify

@fastify/jwt

CI NPM version js-standard-style

JWT utils for Fastify, internally it uses fast-jwt.

NOTE: The plugin has been migrated from using jsonwebtoken to fast-jwt. Even though fast-jwt has 1:1 feature implementation with jsonwebtoken, some exotic implementations might break. In that case please open an issue with details of your implementation. See Upgrading notes for more details about what changes this migration introduced.

@fastify/jwt supports Fastify@3. @fastify/jwt v1.x supports both Fastify@2.

Install

npm i @fastify/jwt

Usage

Register as a plugin. This will decorate your fastify instance with the following methods: decode, sign, and verify; refer to their documentation to find how to use the utilities. It will also register request.jwtVerify and reply.jwtSign. You must pass a secret when registering the plugin.

const fastify = require('fastify')()
fastify.register(require('@fastify/jwt'), {
  secret: 'supersecret'
})

fastify.post('/signup', (req, reply) => {
  // some code
  const token = fastify.jwt.sign({ payload })
  reply.send({ token })
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

For verifying & accessing the decoded token inside your services, you can use a global onRequest hook to define the verification process like so:

const fastify = require('fastify')()
fastify.register(require('@fastify/jwt'), {
  secret: 'supersecret'
})

fastify.addHook("onRequest", async (request, reply) => {
  try {
    await request.jwtVerify()
  } catch (err) {
    reply.send(err)
  }
})

Afterwards, just use request.user in order to retrieve the user information:

module.exports = async function(fastify, opts) {
  fastify.get("/", async function(request, reply) {
    return request.user
  })
}

However, most of the time we want to protect only some of the routes in our application. To achieve this you can wrap your authentication logic into a plugin like

const fp = require("fastify-plugin")

module.exports = fp(async function(fastify, opts) {
  fastify.register(require("@fastify/jwt"), {
    secret: "supersecret"
  })

  fastify.decorate("authenticate", async function(request, reply) {
    try {
      await request.jwtVerify()
    } catch (err) {
      reply.send(err)
    }
  })
})

Then use the onRequest of a route to protect it & access the user information inside:

module.exports = async function(fastify, opts) {
  fastify.get(
    "/",
    {
      onRequest: [fastify.authenticate]
    },
    async function(request, reply) {
      return request.user
    }
  )
}

Make sure that you also check @fastify/auth plugin for composing more complex strategies.

Auth0 tokens verification

If you need to verify Auth0 issued HS256 or RS256 JWT tokens, you can use fastify-auth0-verify, which is based on top of this module.

Options

secret (required)

You must pass a secret to the options parameter. The secret can be a primitive type String, a function that returns a String or an object { private, public }.

In this object { private, public } the private key is a string, buffer or object containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECDSA. In case of a private key with passphrase an object { private: { key, passphrase }, public } can be used (based on crypto documentation), in this case be sure you pass the algorithm inside the signing options prefixed by the sign key of the plugin registering options).

In this object { private, public } the public key is a string or buffer containing either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.

Function based secret is supported by the request.jwtVerify() and reply.jwtSign() methods and is called with request, token, and callback parameters.

Example

const { readFileSync } = require('fs')
const path = require('path')
const fastify = require('fastify')()
const jwt = require('@fastify/jwt')
// secret as a string
fastify.register(jwt, { secret: 'supersecret' })
// secret as a function with callback
fastify.register(jwt, {
  secret: function (request, token, callback) {
    // do something
    callback(null, 'supersecret')
  }
})
// secret as a function returning a promise
fastify.register(jwt, {
  secret: function (request, token) {
    return Promise.resolve('supersecret')
  }
})
// secret as an async function
fastify.register(jwt, {
  secret: async function (request, token) {
    return 'supersecret'
  }
})
// secret as an object of RSA keys (without passphrase)
// the files are loaded as strings
fastify.register(jwt, {
  secret: {
    private: readFileSync(`${path.join(__dirname, 'certs')}/private.key`, 'utf8'),
    public: readFileSync(`${path.join(__dirname, 'certs')}/public.key`, 'utf8')
  },
  sign: { algorithm: 'RS256' }
})
// secret as an object of P-256 ECDSA keys (with a passphrase)
// the files are loaded as buffers
fastify.register(jwt, {
  secret: {
    private: {
      key: readFileSync(`${path.join(__dirname, 'certs')}/private.pem`),
      passphrase: 'super secret passphrase'
    },
    public: readFileSync(`${path.join(__dirname, 'certs')}/public.pem`)
  },
  sign: { algorithm: 'ES256' }
})

Optionally you can define global default options that will be used by @fastify/jwt API if you do not override them.

Additionally, it is also possible to reject tokens selectively (i.e. blacklisting) by providing the option trusted with the following signature: (request, decodedToken) => boolean|Promise<boolean>|SignPayloadType|Promise<SignPayloadType> where request is a FastifyRequest and decodedToken is the parsed (and verified) token information. Its result should be false or Promise<false> if the token should be rejected or, otherwise, be true or Promise<true> if the token should be accepted and, considering that request.user will be used after that, the return should be decodedToken itself.

Example

const { readFileSync } = require('fs')
const path = require('path')
const fastify = require('fastify')()
const jwt = require('@fastify/jwt')
fastify.register(jwt, {
  secret: {
    private: readFileSync(`${path.join(__dirname, 'certs')}/private.pem`, 'utf8')
    public: readFileSync(`${path.join(__dirname, 'certs')}/public.pem`, 'utf8')
  },
  // Global default decoding method options
  decode: { complete: true },
  // Global default signing method options
  sign: {
    algorithm: 'ES256',
    iss: 'api.example.tld'
  },
  // Global default verifying method options
  verify: { allowedIss: 'api.example.tld' }
})

fastify.get('/decode', async (request, reply) => {
  // We clone the global signing options before modifying them
  let altSignOptions = Object.assign({}, fastify.jwt.options.sign)
  altSignOptions.iss = 'another.example.tld'

  // We generate a token using the default sign options
  const token = await reply.jwtSign({ foo: 'bar' })
  // We generate a token using overrided options
  const tokenAlt = await reply.jwtSign({ foo: 'bar' }, altSignOptions)

  // We decode the token using the default options
  const decodedToken = fastify.jwt.decode(token)

  // We decode the token using completely overided the default options
  const decodedTokenAlt = fastify.jwt.decode(tokenAlt, { complete: false })

  return { decodedToken, decodedTokenAlt }
  /**
   * Will return:
   *
   * {
   *   "decodedToken": {
   *     "header": {
   *       "alg": "ES256",
   *       "typ": "JWT"
   *     },
   *     "payload": {
   *       "foo": "bar",
   *       "iat": 1540305336
   *       "iss": "api.example.tld"
   *     },
   *     "signature": "gVf5bzROYB4nPgQC0nbJTWCiJ3Ya51cyuP-N50cidYo"
   *   },
   *   decodedTokenAlt: {
   *     "foo": "bar",
   *     "iat": 1540305337
   *     "iss": "another.example.tld"
   *   },
   * }
   */
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

cookie

Example using cookie

In some situations you may want to store a token in a cookie. This allows you to drastically reduce the attack surface of XSS on your web app with the httpOnly and secure flags. Cookies can be susceptible to CSRF. You can mitigate this by either setting the sameSite flag to strict, or by using a CSRF library such as @fastify/csrf.

Note: This plugin will look for a decorated request with the cookies property. @fastify/cookie supports this feature, and therefore you should use it when using the cookie feature. The plugin will fallback to looking for the token in the authorization header if either of the following happens (even if the cookie option is enabled):

  • The request has both the authorization and cookie header
  • Cookie is empty, authorization header is present

If you are signing your cookie, you can set the signed boolean to true which will make sure the JWT is verified using the unsigned value.

const fastify = require('fastify')()
const jwt = require('@fastify/jwt')

fastify.register(jwt, {
  secret: 'foobar'
  cookie: {
    cookieName: 'token',
    signed: false
  }
})

fastify
  .register(require('@fastify/cookie'))

fastify.get('/cookies', async (request, reply) => {
  const token = await reply.jwtSign({
    name: 'foo',
    role: ['admin', 'spy']
  })

  reply
    .setCookie('token', token, {
      domain: 'your.domain',
      path: '/',
      secure: true, // send cookie over HTTPS only
      httpOnly: true,
      sameSite: true // alternative CSRF protection
    })
    .code(200)
    .send('Cookie sent')
})

fastify.addHook('onRequest', (request) => request.jwtVerify())

fastify.get('/verifycookie', (request, reply) => {
  reply.send({ code: 'OK', message: 'it works!' })
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

onlyCookie

Setting this options to true will decode only the cookie in the request. This is useful for refreshToken implementations where the request typically has two tokens: token and refreshToken. The main authentication token usually has a shorter timeout and the refresh token normally stored in the cookie has a longer timeout. This allows you to check to make sure that the cookie token is still valid, as it could have a different expiring time than the main token. The payloads of the two different tokens could also be different.

const fastify = require('fastify')()
const jwt = require('@fastify/jwt')

fastify.register(jwt, {
  secret: 'foobar',
  cookie: {
    cookieName: 'refreshToken',
  },
  sign: {
    expiresIn: '10m'
  }
})

fastify
  .register(require('@fastify/cookie'))

fastify.get('/cookies', async (request, reply) => {

  const token = await reply.jwtSign({
    name: 'foo'
  })

  const refreshToken = await reply.jwtSign({
    name: 'bar'
  }, {expiresIn: '1d'})

  reply
    .setCookie('refreshToken', refreshToken, {
      domain: 'your.domain',
      path: '/',
      secure: true, // send cookie over HTTPS only
      httpOnly: true,
      sameSite: true // alternative CSRF protection
    })
    .code(200)
    .send({token})
})

fastify.addHook('onRequest', (request) => {
    request.jwtVerify()
    request.jwtVerify({onlyCookie: true})
})

fastify.get('/verifycookie', (request, reply) => {
  reply.send({ code: 'OK', message: 'it works!' })
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

trusted

Example trusted tokens

const fastify = require('fastify')()

fastify.register(require('@fastify/jwt'), {
  secret: 'foobar',
  trusted: validateToken
})

fastify.addHook('onRequest', (request) => request.jwtVerify())

fastify.get('/', (request, reply) => {
  reply.send({ code: 'OK', message: 'it works!' })
})

fastify.listen({ port: 3000 }, (err) => {
  if (err) {
    throw err
  }
})

// ideally this function would do a query against some sort of storage to determine its outcome
async function validateToken(request, decodedToken) {
  const denylist = ['token1', 'token2']

  return !denylist.includes(decodedToken.jti)
}

formatUser

Example with formatted user

You may customize the request.user object setting a custom sync function as parameter:

const fastify = require('fastify')();
fastify.register(require('@fastify/jwt'), {
  formatUser: function (user) {
    return {
      departmentName: user.department_name,
      name: user.name
    }
  },
  secret: 'supersecret'
});

fastify.addHook('onRequest', (request, reply) =>  request.jwtVerify());

fastify.get("/", async (request, reply) => {
  return `Hello, ${request.user.name} from ${request.user.departmentName}.`;
});

namespace

To define multiple JWT validators on the same routes, you may use the namespace option. You can combine this with custom names for jwtVerify and jwtSign.

When you omit the jwtVerify and jwtSign options, the default function name will be <namespace>JwtVerify and <namespace>JwtSign.

Example with namespace

const fastify = require('fastify')

fastify.register(jwt, {
  secret: 'test',
  namespace: 'security',
  jwtVerify: 'securityVerify',
  jwtSign: 'securitySign'
})

fastify.register(jwt, {
  secret: 'fastify',
  namespace: 'airDrop'
})

// use them like this:
fastify.post('/sign/:namespace', async function (request, reply) {
  switch (request.params.namespace) {
    case 'security':
      return reply.securitySign(request.body)
    default:
      return reply.airDropJwtSign(request.body)
  }
})

messages

For your convenience, you can override the default HTTP response messages sent when an unauthorized or bad request error occurs. You can choose the specific messages to override and the rest will fallback to the default messages. The object must be in the format specified in the example below.

Example

const fastify = require('fastify')

const myCustomMessages = {
  badRequestErrorMessage: 'Format is Authorization: Bearer [token]',
  badCookieRequestErrorMessage: 'Cookie could not be parsed in request',
  noAuthorizationInHeaderMessage: 'Autorization header is missing!',
  noAuthorizationInCookieMessage: 'No Authorization was found in request.cookies',
  authorizationTokenExpiredMessage: 'Authorization token expired',
  authorizationTokenUntrusted: 'Untrusted authorization token',
  authorizationTokenUnsigned: 'Unsigned authorization token
  // for the below message you can pass a sync function that must return a string as shown or a string
  authorizationTokenInvalid: (err) => {
    return `Authorization token is invalid: ${err.message}`
  }
}

fastify.register(require('@fastify/jwt'), {
  secret: 'supersecret',
  messages: myCustomMessages
})

decoratorName

If this plugin is used together with fastify/passport, we might get an error as both plugins use the same name for a decorator. We can change the name of the decorator, or user will default

Example

const fastify = require('fastify')
fastify.register(require('@fastify/jwt'), {
  secret: 'supersecret',
  decoratorName: 'customName'
})

decode

  • complete: Return an object with the decoded header, payload, signature and input (the token part before the signature), instead of just the content of the payload. Default is false.
  • checkTyp: When validating the decoded header, setting this option forces the check of the typ property against this value. Example: checkTyp: 'JWT'. Default is undefined.

sign

  • key: A string or a buffer containing the secret for HS* algorithms or the PEM encoded public key for RS*, PS*, ES* and EdDSA algorithms. The key can also be a function accepting a Node style callback or a function returning a promise. If provided, it will override the value of secret provided in the options.

  • algorithm: The algorithm to use to sign the token. The default is autodetected from the key, using RS256 for RSA private keys, HS256 for plain secrets and the correspondent ES or EdDSA algorithms for EC or Ed* private keys.

  • mutatePayload: If set to true, the original payload will be modified in place (via Object.assign) by the signing function. This is useful if you need a raw reference to the payload after claims have been applied to it but before it has been encoded into a token.

  • expiresIn: Time span after which the token expires, added as the exp claim in the payload. It is expressed in seconds or a string describing a time span (E.g.: 60, "2 days", "10h", "7d"). A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc.), otherwise milliseconds unit is used by default ("120" is equal to "120ms"). This will override any existing value in the claim.

  • notBefore: Time span before the token is active, added as the nbf claim in the payload. It is expressed in seconds or a string describing a time span (E.g.: 60, "2 days", "10h", "7d"). A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc.), otherwise milliseconds unit is used by default ("120" is equal to "120ms"). This will override any existing value in the claim.

  • ... the rest of the sign options can be found here.

verify

  • key: A string or a buffer containing the secret for HS* algorithms or the PEM encoded public key for RS*, PS*, ES* and EdDSA algorithms. The key can also be a function accepting a Node style callback or a function returning a promise. If provided, it will override the value of secret provided in the options.
  • algorithms: List of strings with the names of the allowed algorithms. By default, all algorithms are accepted.
  • complete: Return an object with the decoded header, payload, signature and input (the token part before the signature), instead of just the content of the payload. Default is false.
  • cache: A positive number specifying the size of the verified tokens cache (using LRU strategy). Setting this to true is equivalent to provide the size 1000. When enabled the performance is dramatically improved. By default the cache is disabled.
  • cacheTTL: The maximum time to live of a cache entry (in milliseconds). If the token has a earlier expiration or the verifier has a shorter maxAge, the earlier takes precedence. The default is 600000, which is 10 minutes.
  • maxAge: The maximum allowed age for tokens to still be valid. It is expressed in seconds or a string describing a time span (E.g.: 60, "2 days", "10h", "7d"). A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc.), otherwise milliseconds unit is used by default ("120" is equal to "120ms"). By default this is not checked.
  • ... the rest of the verify options can be found here.

API Spec

fastify.jwt.sign(payload [,options] [,callback])

This method is used to sign the provided payload. It returns the token. The payload must be an Object. Can be used asynchronously by passing a callback function; synchronously without a callback. options must be an Object and can contain sign options.

fastify.jwt.verify(token, [,options] [,callback])

This method is used to verify provided token. It accepts a token (as Buffer or a string) and returns the payload or the sections of the token. Can be used asynchronously by passing a callback function; synchronously without a callback. options must be an Object and can contain verify options.

Example

const token = fastify.jwt.sign({ foo: 'bar' })
// synchronously
const decoded = fastify.jwt.verify(token)
// asycnhronously
fastify.jwt.verify(token, (err, decoded) => {
  if (err) fastify.log.error(err)
  fastify.log.info(`Token verified. Foo is ${decoded.foo}`)
})

fastify.jwt.decode(token [,options])

This method is used to decode the provided token. It accepts a token (as a Buffer or a string) and returns the payload or the sections of the token. options must be an Object and can contain decode options. Can only be used synchronously.

Example

const token = fastify.jwt.sign({ foo: 'bar' })
const decoded = fastify.jwt.decode(token)
fastify.log.info(`Decoded JWT: ${decoded}`)

fastify.jwt.options

For your convenience, the decode, sign, verify and messages options you specify during .register are made available via fastify.jwt.options that will return an object { decode, sign, verify, messages } containing your options.

Example

const { readFileSync } = require('fs')
const path = require('path')
const fastify = require('fastify')()
const jwt = require('@fastify/jwt')
fastify.register(jwt, {
  secret: {
    private: readFileSync(`${path.join(__dirname, 'certs')}/private.key`),
    public: readFileSync(`${path.join(__dirname, 'certs')}/public.key`)
  },
  sign: {
    algorithm: 'RS256',
    aud: 'foo',
    iss: 'example.tld'
  },
  verify: {
    allowedAud: 'foo',
    allowedIss: 'example.tld',
  }
})

fastify.get('/', (request, reply) => {
  const globalOptions = fastify.jwt.options

  // We recommend that you clone the options like this when you need to mutate them
  // modifiedVerifyOptions = { audience: 'foo', issuer: 'example.tld' }
  let modifiedVerifyOptions = Object.assign({}, fastify.jwt.options.verify)
  modifiedVerifyOptions.allowedAud = 'bar'
  modifiedVerifyOptions.allowedSub = 'test'

  return { globalOptions, modifiedVerifyOptions }
  /**
   * Will return :
   * {
   *   globalOptions: {
   *     decode: {},
   *     sign: {
   *       algorithm: 'RS256',
   *       aud: 'foo',
   *       iss: 'example.tld'
   *     },
   *     verify: {
   *       allowedAud: 'foo',
   *       allowedIss: 'example.tld'
   *     }
   *   },
   *   modifiedVerifyOptions: {
   *     allowedAud: 'bar',
   *     allowedIss: 'example.tld',
   *     allowedSub: 'test'
   *   }
   * }
   */
})

fastify.listen({ port: 3000 }, err => {
  if (err) throw err
})

fastify.jwt.cookie

For your convenience, request.jwtVerify() will look for the token in the cookies property of the decorated request. You must specify cookieName. Refer to the cookie example to see sample usage and important caveats.

reply.jwtSign(payload, [options,] callback)

options must be an Object and can contain sign options.

request.jwtVerify([options,] callback)

options must be an Object and can contain verify and decode options.

request.jwtDecode([options,] callback)

Decode a JWT without verifying

As of 3.2.0, decorated when options.jwtDecode is truthy. Will become non-conditionally decorated in 4.0.0. This avoid breaking change that would effect fastify-auth0-verify.

options must be an Object and can contain verify and decode options.

Algorithms supported

The following algorithms are currently supported by fast-jwt that is internally used by @fastify/jwt.

Name Description
none Empty algorithm - The token signature section will be empty
HS256 HMAC using SHA-256 hash algorithm
HS384 HMAC using SHA-384 hash algorithm
HS512 HMAC using SHA-512 hash algorithm
ES256 ECDSA using P-256 curve and SHA-256 hash algorithm
ES384 ECDSA using P-384 curve and SHA-384 hash algorithm
ES512 ECDSA using P-521 curve and SHA-512 hash algorithm
RS256 RSASSA-PKCS1-v1_5 using SHA-256 hash algorithm
RS384 RSASSA-PKCS1-v1_5 using SHA-384 hash algorithm
RS512 RSASSA-PKCS1-v1_5 using SHA-512 hash algorithm
PS256 RSASSA-PSS using SHA-256 hash algorithm
PS384 RSASSA-PSS using SHA-384 hash algorithm
PS512 RSASSA-PSS using SHA-512 hash algorithm
EdDSA EdDSA tokens using Ed25519 or Ed448 keys, only supported on Node.js 12+

You can find the list here.

Examples

Certificates Generation

Here some example on how to generate certificates and use them, with or without passphrase.

Signing and verifying (jwtSign, jwtVerify)

const fastify = require('fastify')()
const jwt = require('@fastify/jwt')
const request = require('request')

fastify.register(jwt, {
  secret: function (request, reply, callback) {
    // do something
    callback(null, 'supersecret')
  }
})

fastify.post('/sign', function (request, reply) {
  reply.jwtSign(request.body.payload, function (err, token) {
    return reply.send(err || { 'token': token })
  })
})

fastify.get('/verify', function (request, reply) {
  request.jwtVerify(function (err, decoded) {
    return reply.send(err || decoded)
  })
})

fastify.listen({ port: 3000 }, function (err) {
  if (err) fastify.log.error(err)
  fastify.log.info(`Server live on port: ${fastify.server.address().port}`)

  // sign payload and get JWT
  request({
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: {
      payload: {
        foo: 'bar'
      }
    },
    uri: `http://localhost:${fastify.server.address().port}/sign`,
    json: true
  }, function (err, response, body) {
    if (err) fastify.log.error(err)
    fastify.log.info(`JWT token is ${body.token}`)

    // verify JWT
    request({
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        authorization: 'Bearer ' + body.token
      },
      uri: 'http://localhost:' + fastify.server.address().port + '/verify',
      json: true
    }, function (err, response, body) {
      if (err) fastify.log.error(err)
      fastify.log.info(`JWT verified. Foo is ${body.foo}`)
    })
  })
})

Verifying with JWKS

The following example integrates the get-jwks package to fetch a JWKS and verify a JWT against a valid public JWK.

Example
const Fastify = require('fastify')
const fjwt = require('@fastify/jwt')
const buildGetJwks = require('get-jwks')

const fastify = Fastify()
const getJwks = buildGetJwks()

fastify.register(fjwt, {
  decode: { complete: true },
  secret: (request, token) => {
    const { header: { kid, alg }, payload: { iss } } = token
    return getJwks.getPublicKey({ kid, domain: iss, alg })
  }
})

fastify.addHook('onRequest', async (request, reply) => {
  try {
    await request.jwtVerify()
  } catch (err) {
    reply.send(err)
  }
})

fastify.listen({ port: 3000 })

TypeScript

This plugin has two available exports, the default plugin function fastifyJwt and the plugin options object FastifyJWTOptions.

Import them like so:

import fastifyJwt, { FastifyJWTOptions } from '@fastify/jwt'

Define custom Payload Type and Attached User Type to request object

typescript declaration merging

// fastify-jwt.d.ts
import "@fastify/jwt"

declare module "@fastify/jwt" {
  interface FastifyJWT {
    payload: { id: number } // payload type is used for signing and verifying
    user: {
      id: number,
      name: string,
      age: number
      } // user type is return type of `request.user` object
  }
}

// index.ts
fastify.get('/', async (request, reply) => {
  request.user.name // string

  const token = await reply.jwtSign({
    id: '123'
    // ^ Type 'string' is not assignable to type 'number'.
  });
})

Acknowledgements

This project is kindly sponsored by:

License

Licensed under MIT.

More Repositories

1

fastify

Fast and low overhead web framework, for Node.js
JavaScript
29,975
star
2

fast-json-stringify

2x faster than JSON.stringify()
JavaScript
3,341
star
3

fastify-dx

Archived
JavaScript
909
star
4

fastify-vite

Fastify plugin for Vite integration.
JavaScript
795
star
5

fastify-swagger

Swagger documentation generator for Fastify
JavaScript
643
star
6

fastify-cli

Run a Fastify application with one command!
JavaScript
605
star
7

benchmarks

Fast and low overhead web framework fastify benchmarks.
JavaScript
502
star
8

fluent-json-schema

A fluent API to generate JSON schemas
JavaScript
479
star
9

aws-lambda-fastify

Insipired by aws-serverless-express to work with Fastify with inject functionality.
JavaScript
479
star
10

fastify-nextjs

React server side rendering support for Fastify with Next
JavaScript
450
star
11

fastify-sensible

Defaults for Fastify that everyone can agree on
JavaScript
405
star
12

fastify-static

Plugin for serving static files as fast as possible
JavaScript
396
star
13

avvio

Asynchronous bootstrapping of Node applications
JavaScript
385
star
14

fastify-multipart

Multipart support for Fastify
JavaScript
343
star
15

fastify-rate-limit

A low overhead rate limiter for your routes
JavaScript
335
star
16

fastify-http-proxy

Proxy your http requests to another server, with hooks.
JavaScript
310
star
17

fastify-helmet

Important security headers for Fastify
JavaScript
305
star
18

fastify-websocket

basic websocket support for fastify
JavaScript
290
star
19

fastify-cors

Fastify CORS
JavaScript
276
star
20

point-of-view

Template rendering plugin for Fastify
JavaScript
272
star
21

fastify-auth

Run multiple auth functions in Fastify
JavaScript
268
star
22

fastify-example-twitter

Fastify example - clone twitter
JavaScript
262
star
23

docs-chinese

Fastify 中文文档
253
star
24

light-my-request

Fake HTTP injection library
JavaScript
243
star
25

fastify-autoload

Require all plugins in a directory
JavaScript
242
star
26

under-pressure

Measure process load with automatic handling of "Service Unavailable" plugin for Fastify.
JavaScript
234
star
27

fastify-passport

Use passport strategies for authentication within a fastify application
TypeScript
234
star
28

fastify-oauth2

Enable to perform login using oauth2 protocol
JavaScript
229
star
29

fastify-cookie

A Fastify plugin to add cookies support
JavaScript
224
star
30

middie

Middleware engine for Fastify.
JavaScript
206
star
31

fastify-mongodb

Fastify MongoDB connection plugin
JavaScript
200
star
32

fastify-express

Express compatibility layer for Fastify
JavaScript
190
star
33

secure-json-parse

JSON.parse() drop-in replacement with prototype poisoning protection
JavaScript
176
star
34

fastify-env

Fastify plugin to check environment variables
JavaScript
175
star
35

fastify-caching

A Fastify plugin to facilitate working with cache headers
JavaScript
163
star
36

fast-proxy

Node.js framework agnostic library that enables you to forward an http request to another HTTP server. Supported protocols: HTTP, HTTPS, HTTP2
JavaScript
163
star
37

fastify-plugin

Plugin helper for Fastify
JavaScript
159
star
38

fastify-compress

Fastify compression utils
JavaScript
157
star
39

env-schema

Validate your env variable using Ajv and dotenv
JavaScript
154
star
40

fastify-redis

Plugin to share a common Redis connection across Fastify.
JavaScript
151
star
41

github-action-merge-dependabot

This action automatically approves and merges dependabot PRs.
JavaScript
151
star
42

fastify-secure-session

Create a secure stateless cookie session for Fastify
JavaScript
145
star
43

fastify-postgres

Fastify PostgreSQL connection plugin
JavaScript
145
star
44

fastify-reply-from

fastify plugin to forward the current http request to another server
JavaScript
142
star
45

fastify-request-context

Request-scoped storage support, based on Asynchronous Local Storage (with fallback to cls-hooked)
JavaScript
138
star
46

fastify-type-provider-typebox

A Type Provider for Typebox
TypeScript
136
star
47

fastify-bearer-auth

A Fastify plugin to require bearer Authorization headers
JavaScript
136
star
48

csrf-protection

A fastify csrf plugin.
JavaScript
127
star
49

fastify-formbody

A Fastify plugin to parse x-www-form-urlencoded bodies
JavaScript
125
star
50

fastify-circuit-breaker

A low overhead circuit breaker for your routes
JavaScript
113
star
51

fastify-swagger-ui

Serve Swagger-UI for Fastify
JavaScript
100
star
52

example

Runnable examples of Fastify
JavaScript
96
star
53

create-fastify

Rapidly generate a Fastify project
JavaScript
92
star
54

fastify-routes

Decorates fastify instance with a map of routes
JavaScript
91
star
55

session

Session plugin for fastify
JavaScript
89
star
56

restartable

Restart Fastify without losing a request
JavaScript
86
star
57

fastify-schedule

Fastify plugin for scheduling periodic jobs.
JavaScript
76
star
58

website-metalsmith

This project is used to build the website for fastify web framework and publish it online.
HTML
76
star
59

fastify-awilix

Dependency injection support for fastify
JavaScript
75
star
60

fastify-error

JavaScript
74
star
61

fast-uri

Dependency free RFC 3986 URI toolbox
JavaScript
74
star
62

fastify-hotwire

Use the Hotwire pattern with Fastify
JavaScript
69
star
63

fastify-etag

Automatically generate etags for HTTP responses, for Fastify
JavaScript
69
star
64

fastify-funky

Make fastify functional! Plugin, adding support for fastify routes returning functional structures, such as Either, Task or plain parameterless function.
JavaScript
68
star
65

fastify-example-todo

A Simple Fastify REST API Example
JavaScript
64
star
66

fastify-accepts

Add accepts parser to fastify
JavaScript
63
star
67

help

Need help with Fastify? File an Issue here.
61
star
68

fastify-basic-auth

Fastify basic auth plugin
JavaScript
59
star
69

fastify-mysql

JavaScript
57
star
70

busboy

A streaming parser for HTML form data for node.js
JavaScript
56
star
71

fastify-url-data

A plugin to provide access to the raw URL components
JavaScript
55
star
72

releasify

A tool to release in a simpler way your module
JavaScript
55
star
73

fastify-kafka

Fastify plugin to interact with Apache Kafka.
JavaScript
51
star
74

fastify-elasticsearch

Fastify plugin for Elasticsearch
JavaScript
40
star
75

fastify-routes-stats

provide stats for routes using perf_hooks, for fastify
JavaScript
40
star
76

deepmerge

Merges the enumerable properties of two or more objects deeply. Fastest implementation of deepmerge
JavaScript
39
star
77

manifetch

A manifest-based fetch() API client builder.
JavaScript
37
star
78

fastify-response-validation

A simple plugin that enables response validation for Fastify.
JavaScript
36
star
79

fastify-type-provider-json-schema-to-ts

A Type Provider for json-schema-to-ts
TypeScript
32
star
80

skeleton

Template repository to create standardized Fastify plugins.
31
star
81

fastify-accepts-serializer

Serializer according to the accept header
JavaScript
24
star
82

website

JavaScript
24
star
83

fastify-leveldb

Plugin to share a common LevelDB connection across Fastify.
JavaScript
21
star
84

tsconfig

Shared TypeScript configuration for fastify projects
21
star
85

fastify-flash

Flash message plugin for Fastify
TypeScript
20
star
86

process-warning

A small utility for creating warnings and emitting them.
JavaScript
19
star
87

docs-korean

18
star
88

one-line-logger

JavaScript
18
star
89

fastify-api

A radically simple API routing and method injection plugin for Fastify.
JavaScript
18
star
90

ajv-compiler

Build and manage the AJV instances for the fastify framework
JavaScript
17
star
91

fastify-early-hints

Draft plugin of the HTTP 103 implementation
JavaScript
17
star
92

vite-plugin-blueprint

Vite plugin for shadowing files from a blueprint folder.
JavaScript
17
star
93

fastify-bankai

Bankai assets compiler for Fastify
JavaScript
15
star
94

fastify-diagnostics-channel

Plugin to deal with diagnostics_channel on Fastify
JavaScript
14
star
95

csrf

CSRF utilities for fastify
JavaScript
13
star
96

.github

Default community health files
13
star
97

any-schema-you-like

Save multiple schemas and decide which one to use to serialize the payload
JavaScript
13
star
98

fastify-throttle

Throttle the download speed of a request
JavaScript
12
star
99

fastify-typescript-extended-sample

This project is supposed to be a large, fake Fastify & TypeScript app. It is meant to be a reference as well as a pseudo-sandbox for Fastify TypeScript changes.
TypeScript
11
star
100

fastify-soap-client

Fastify plugin for a SOAP client
JavaScript
10
star