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-user

v1.2.0

Published

Fastify plugin to extract user

Downloads

121,259

Readme

Fastify User plugin

This plugin provides a Fastify plugin to populate a request.user from a JWT token (custom claims) or a webhook (response body).

To use it, simply invoke the extractUser method on the request object, or add this hook:

app.addHook('preHandler', async (request, reply) => {
  await request.extractUser()
})

If JWT valdation or the webhook call fails, the request.user is not set.

JWT

It's build on top of fastify-jwt plugin, so you can use all the options available there (with the exception of namespace, see below)


const app = fastify()
app.register(fastifyUser, {
  jwt: {
    secret: <my-shared-secret>
  }
})

app.addHook('preHandler', async (request, reply) => {
  await request.extractUser()
})

app.get('/', async function (request, reply) {
  return request.user
})

await app.ready()

It's also possible to specify a JSON Web Key Set (JWKS) URI to retrieve the public keys from a remote server.

{
  jwt: {
    jwks: {
      allowedDomains: [
        "https://ISSUER_DOMAIN"
      ]
    }
  }
}

Any option supported by the get-jwks library can be specified in the jwt.jwks object.

namespace

The JWT namespace option is used to specify the namespace for custom claims used to populate request.user. For more info about this see OIDC Specification. Since these namespace are URLs, we might want to remove it. If the namespace is specified it will be removed automatically:

{
  jwt: {
    jwks: true,
    namespace: "https://test.com/" 
  }
}

In this case, if the token contains a claim https://test.com/email, the plugin will strip the namespace from the claim and populate request.user.email with its value.

Webhook

The plugin can also populate request.user from a webhook. When a request is received, fastify-user sends a POST to the webhook, replicating the same body and headers, except for:

  • host
  • connection

The webhook is expected to return a JSON object with the user information. The plugin will populate request.user with the response body.

Example of options:

{
  webhook: {
    url: `http://my-webhook-url/authorize`
  }
}

JWT and Webhook

In case both jwt and webhook options are specified, the plugin will try to populate request.user from the JWT token first. If the token is not valid, it will try to populate request.user from the webhook.

Custom auth strategies

In case if you want to use your own auth strategy, you can pass it as an option to the plugin. All custom auth strategies should have createSession method, which will be called on every request. This method should set request.user object. All custom strategies will be executed after jwt and webhook strategies.

{
  authStrategies: [{
    name: 'myAuthStrategy',
    createSession: async function (request, reply) {
      req.user = { id: 42, role: 'admin' }
    }
  }]
}

or you can add it via addAuthStrategy method:

app.addAuthStrategy({
  name: 'myAuthStrategy',
  createSession: async function (request, reply) {
    req.user = { id: 42, role: 'admin' }
  }
})

Run Tests

npm test