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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fastify-bit-oidc-auth

v1.0.0

Published

A reusable Fastify OIDC authentication and RBAC library

Readme

fastify-bit-oidc-auth

A reusable, production-ready Fastify middleware suite designed to handle OIDC (OpenID Connect) authentication, Role-Based Access Control (RBAC), and Multi-Tenant scoping in a secure, "default-deny" manner.

Installation

npm install fastify-bit-oidc-auth

Ensure you have your peer dependencies installed (Fastify v4 or v5):

npm install fastify

Features

  • Stateless JWT Validation: Validates OIDC Bearer tokens using remote JWKS (jose) with customizable caching.
  • RBAC (Role-Based Access Control): Restricts endpoints to specific user roles.
  • Multi-Tenant Isolation: Enforces tenant scoping via organization_id claim, with support for cross-tenant bypass for super administrator roles.

Usage

import Fastify from 'fastify';
import { 
  createAuthMiddleware, 
  requireRoles, 
  createTenantMiddleware 
} from 'fastify-bit-oidc-auth';

const fastify = Fastify({ logger: true });

// 1. Initialize authentication middleware
const authMiddleware = createAuthMiddleware({
  jwksUri: 'https://keycloak.example.com/realms/myrealm/protocol/openid-connect/certs',
  issuer: 'https://keycloak.example.com/realms/myrealm',
  audience: 'my-client-id',           // Optional: Verifies 'azp' claim
  rolesClaim: 'my_custom_roles_claim' // Optional: Fallback is 'realm_access.roles'
});

// 2. Initialize tenant separation middleware
const tenantMiddleware = createTenantMiddleware({
  superAdminRole: 'platform_admin'     // Optional: Role that bypasses tenant scoping
});

// 3. Register protected routes
fastify.get('/api/dashboard', {
  preHandler: [
    authMiddleware, 
    requireRoles('org_admin', 'campaign_manager'), 
    tenantMiddleware
  ]
}, async (request, reply) => {
  // Safe to access:
  const user = request.user; // { id, email, roles, organization_id }
  return { 
    message: 'Hello secure world!', 
    organization: user.organization_id 
  };
});

fastify.listen({ port: 3000 });

Configuration Options

AuthConfig

  • jwksUri (string, required): URI of your Identity Provider's certificate endpoint.
  • issuer (string, required): Expected token issuer.
  • audience (string, optional): Expected client ID (azp claim).
  • rolesClaim (string, optional): Key inside token containing the user roles array.
  • jwksCacheTtlMs (number, optional): Cache duration of JWKS (default: 1 hour).

TenantConfig

  • superAdminRole (string, optional): Role name that bypasses tenant checks (default: platform_admin).

License

MIT