Code Monkey home page Code Monkey logo

fastify-auth0-verify's Introduction

fastify-auth0-verify

Package Version ci

Auth0 verification plugin for Fastify, internally uses @fastify/jwt.

Installation

Just run:

npm install fastify-auth0-verify --save

Usage

Register as a plugin, providing one or more of the following options:

  • domain: The Auth0 tenant domain. It enables verification of RS256 encoded JWT tokens. It is also used to verify the token issuer (iss). Either provide a domain or the full URL, including the trailing slash (https://domain.com/).
  • audience: The Auth0 audience (aud), usually the API name. If you provide the value true, the domain will be also used as audience. Accepts a string value, or an array of strings for multiple providers.
  • issuer: The Auth0 issuer (iss), usually the API name. By default the domain will be also used as audience. Accepts a string value, or an array of strings for multiple issuers.
  • secret: The Auth0 client secret. It enables verification of HS256 encoded JWT tokens.
  • complete: If to return also the header and signature of the verified token.
  • secretsTtl: How long (in milliseconds) to cache RS256 secrets before getting them again using well known JWKS URLS. Setting to 0 or less disables the cache.
  • cookie: Used to indicate that the token can be passed using cookie, instead of the Authorization header.
    • cookieName: The name of the cookie.
    • signed: Indicates whether the cookie is signed or not. If set to true, the JWT will be verified using the unsigned value.

Since this plugin is based on the @fastify/jwt verify, it is also possibile to pass the options documented here, see the example below.

Once registered, your fastify instance and request will be decorated as describe by @fastify/jwt.

In addition, the request will also get the authenticate decorator.

This decorator can be used as preValidation hook to add authenticate to your routes. The token information will be available in request.user.

Example:

const server = require('fastify')()

server.register(require('fastify-auth0-verify'), {
  domain: '<auth0 auth domain>',
  audience: '<auth0 app audience>'
})

server.register(function (instance, _options, done) {
  instance.get('/verify', {
    handler: function (request, reply) {
      reply.send(request.user)
    },
    preValidation: instance.authenticate
  })

  done()
})

server.listen(0, err => {
  if (err) {
    throw err
  }
})

You can configure there to be more than one Auth0 API audiences:

const server = require('fastify')()

server.register(require('fastify-auth0-verify'), {
  domain: '<auth0 auth domain>',
  audience: ['<auth0 app audience>', '<auth0 admin audience>']
})

server.register(function (instance, _options, done) {
  instance.get('/verify', {
    handler: function (request, reply) {
      reply.send(request.user)
    },
    preValidation: instance.authenticate
  })
  done()
})

server.listen(APP_PORT, err => {
  if (err) {
    throw err
  }
})

You can include @fastify/jwt verify options:

server.register(require('fastify-auth0-verify'), {
  domain: '<auth0 auth domain>',
  audience: ['<auth0 app audience>', '<auth0 admin audience>'],
  cache: true, // @fastify/jwt cache
  cacheTTL: 100, // @fastify/jwt cache ttl
  errorCacheTTL: -1 // @fastify/jwt error cache ttl
})

Contributing

See CONTRIBUTING.md

License

Copyright NearForm Ltd. Licensed under the Apache-2.0 license.

fastify-auth0-verify's People

Contributors

admataz avatar brainrepo avatar davideroffo avatar dependabot[bot] avatar gilach avatar github-actions[bot] avatar guilhermelimak avatar ilteoood avatar marco-ippolito avatar mattmazzola avatar mcollina avatar nlindley avatar notwoods avatar optic-release-automation[bot] avatar radomird avatar ramonmulia avatar ruanmartinelli avatar salmanm avatar sameer-coder avatar shogunpanda avatar simoneb avatar sundeqvist avatar uzlopak avatar wilkmaia avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.