npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-aws-jwt-verify

v1.0.6

Published

Fastify plugin wrapper around aws-jwt-verify for JWT authentication/authorization with AWS Cognito

Downloads

2,760

Readme

fastify-aws-jwt-verify

Fastify plugin wrapper around aws-jwt-verify for JWT authentication/authorization with AWS Cognito

The options provided to the plugin registration are passed directly to CognitoJwtVerifier.create with helpers for ease of reuse.

Install

npm i fastify-aws-jwt-verify

Usage

Register as a plugin, providing the following options:

tokenProvider

Either use a built-in method for extracting the JWT token from a request or provide your own

Built-in method(~~s~~)

  • Bearer: Follow the Bearer Authorization scheme, where the token is provided via the Authorization header with value Bearer <TOKEN>

Custom token provider

Accept the FastifyRequest object and return the extracted string token, e.g.

await fastify.register(FastifyAwsJwtVerify, {
  tokenProvider: req => {
    if ('X-Auth-Token' in req.headers) {
      return req.headers['X-Auth-Token']
    } else {
      throw new Unauthorized("Missing 'X-Auth-Token' header")
    }
  },
  userPoolId: 'user-pool-id'
})

User pool options

Plugin configuration supports scenarios for both a single user pool or multiple potential user pools.

Single user pool

For a single user pool, set verifier fields in the top-level plugin configuration object, e.g.

await fastify.register(FastifyAwsJwtVerify, {
  clientId: 'app-client-id',
  tokenProvider: 'Bearer',
  tokenUse: 'access',
  userPoolId: 'user-pool-id'
})

For complete set of options, see the aws-jwt-verify package documentation

Multiple user pools

For multiple user pools, set verifier fields in the pools field of the plugin configuration, e.g.

await fastify.register(FastifyAwsJwtVerify, {
  pools: [
    {
      clientId: 'app-client-id-1',
      tokenUse: 'access',
      userPoolId: 'user-pool-1'
    },
    {
      clientId: 'app-client-id-2',
      tokenUse: 'access',
      userPoolId: 'user-pool-2'
    }
  ],
  tokenProvider: 'Bearer'
})

For complete set of options, see the aws-jwt-verify package documentation

Request handlers

Registering the plugin on its own does not require authentication/authorization on any route. Three decorators are provided to simplify attaching handlers.

auth.require(options?: FastifyAwsJwtVerifyOptions)

If no options provided, requires authorization based on the options provided at plugin registration time. This is the simplest usage.

fastify.addHook('onRequest', fastify.auth.require())

If any option is provided, it overrides the corresponding option in the base plugin configuration options. (Note: Providing the pools field here overrides the entire pools field in the base options.)

fastify.addHook('onRequest', fastify.auth.require({
  groups: [ 'Administrators', 'Moderators' ]
}))

auth.client(...clientIds: string[])

Specify allowed client IDs in place of the base configuration options, e.g.

fastify.addHook('onRequest', fastify.auth.client( 'app-client-1', 'app-client-2' ))

This is semantically equivalent to

fastify.addHook('onRequest', fastify.auth.require({
  clientId: [ 'app-client-1', 'app-client-2' ]
}))

auth.groups(...groups: string[])

Specify allowed groups in place of the base configuration options, e.g.

fastify.addHook('onRequest', fastify.auth.groups( 'Administrators', 'Moderators' ))

This is semantically equivalent to

fastify.addHook('onRequest', fastify.auth.require({
  groups: [ 'Administrators', 'Moderators' ]
}))

User payload

Upon successful JWT verification, the request is decorated with the decoded user payload in the user field.

fastify.post('/item', {
  onRequest: fastify.auth.groups('Administrators')
}, req => {
  fastify.log.info(`User: ${req.user}`)
  // Add item
})

Example

const fastify = Fastify()

await fastify.register(fp(fastifyAwsJwtVerifyPlugin), {
    clientId: 'app-client-1',
    tokenProvider: 'Bearer',
    tokenUse: 'access',
    userPoolId: 'user-pool-1'
})

// No authorization required
await fastify.get('/', req => { /** Handle request **/ })

// Require authorization based on registration options
await fastify.get('/account', {
  onRequest: fastify.auth.require()
}, req => { /** Handle request **/ })

// Administrators group membership required
await fastify.post('/item', {
  onRequest: fastify.auth.groups('Administrators')
}, req => { /** Handle request **/ })

License

Licensed under Apache 2.0