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

@defra/hapi-auth-oidc

v0.2.0

Published

A lightweight Node.js library for authentication using OIDC tokens and hapi server integration.

Readme

Hapi Auth OIDC

A Hapi.js plugin providing OpenID Connect (OIDC) authentication with PKCE support. Supports federated Cognito tokens, mock tokens for testing, and automatic token validation and refresh. Designed for server-side Hapi applications.

Features

  • Pre-login redirect to OIDC provider with PKCE support
  • Post-login handling with token validation
  • Automatic access token refresh with configurable early refresh window
  • Federated token support via Cognito Identity Pools
  • Optional request decoration to validate and refresh tokens
  • Mock OIDC provider for development and testing environments
  • Fully configurable cookie options

Installation

npm install @defra/hapi-auth-oidc

Usage

Importing from the library

All public functions, providers, and plugins are exported from the library entry point:

import {
  HapiAuthOidcPlugin,
  CognitoTokenProvider,
  MockProvider,
  preLogin,
  postLogin,
  validateAndRefreshToken
} from '@defra/hapi-auth-oidc'

Alternatively, import the entire library as a namespace:

import * as OIDC from '@defra/hapi-auth-oidc'
const plugin = OIDC.HapiAuthOidcPlugin
const tokenProvider = new OIDC.CognitoTokenProvider({...})

Registering the plugin

import Hapi from '@hapi/hapi'
import { AuthOidcPlugin } from './auth-oidc.js'

const server = Hapi.server({ port: 3000 })

await server.register(AuthOidcPlugin)
server.auth.strategy('azure-oidc', 'hapi-auth-oidc')

Decorating requests

If enableRefreshDecoration is true (default), requests are decorated with validateAndRefreshToken:

server.route({
  method: 'GET',
  path: '/protected',
  handler: async (request, h) => {
    const token = await request.validateAndRefreshToken({
      accessToken: request.auth.credentials.accessToken,
      refreshToken: request.auth.credentials.refreshToken
    })
    return { token }
  },
  options: {
    auth: 'azure-oidc'
  }
})

Token refresh

Standalone usage:

import { validateAndRefreshToken } from '@defra/hapi-auth-oidc'

const refreshedToken = await validateAndRefreshToken(
  { accessToken, refreshToken },
  getOidcConfig,
  60000,
  oidcScope,
  logger
)

Configuration

Plugin options

  • strategyName - Name of the Hapi auth strategy (default: 'hapi-auth-oidc')

  • oidc - OIDC configuration object:

    • clientId - OIDC client ID
    • discoveryUri - URL for OIDC discovery document
    • authProvider - Token provider (CognitoTokenProvider or MockProvider)
    • useHttp - Boolean flag to allow insecure HTTP requests for discovery (default: false)
    • loginCallbackUri - Redirect URI after OIDC login
    • scope - Space-separated string of scopes
    • externalBaseUrl - Base URL used to construct absolute callback URLs
    • defaultPostLoginUri - Redirect URI after login if not otherwise specified (default: '/')
    • enableRefreshDecoration - Whether to decorate requests with validateAndRefreshToken (default: true)
    • earlyRefreshMs - Milliseconds before token expiry to refresh early (default: 60000)
  • cookieOptions - Cookie options for Iron encryption and security

    • password - Secret used for Iron encryption (required)
    • isSecure - Send cookie over HTTPS only (default: true)
    • encoding - 'none' | 'base64' | 'base64json' | 'iron' (default: 'iron')
    • path - Cookie path (default: '/')
    • isHttpOnly - Cookie inaccessible to client-side JS (default: true)
    • isSameSite - 'Strict' | 'Lax' | 'None' (default: 'Lax')
    • ttl - Time-to-live in milliseconds (optional)
    • domain - Cookie domain (optional)
    • ignoreErrors - Ignore encoding errors (default: true)
    • clearInvalid - Automatically clear invalid cookies (default: true)

Token Providers

CognitoTokenProvider

  • Federated token provider for AWS Cognito Identity Pools.
  • Automatically caches the token and refreshes it before expiry.
  • Supports optional early refresh via earlyRefreshMs.

MockProvider

  • Simple mock token provider for development/testing.
  • Returns a hardcoded token.

Example Usage

import { config } from '../../../../config/config.js'
import {
  CognitoTokenProvider,
  HapiAuthOidcPlugin,
  MockProvider
} from '../hapi-auth-oidc/index.js'
import * as openid from 'openid-client'

const authProvider = config.get('isProduction')
  ? new CognitoTokenProvider({
      poolId: config.get('azureFederatedCredentials.identityPoolId'),
      logins: { 'cdp-portal-frontend-aad-access': 'cdp-portal-frontend' }
    })
  : new MockProvider()

const oidcCookieConfig = config.get('hapi-auth-oidc.cookie')

export const AuthOidcPlugin = {
  plugin: HapiAuthOidcPlugin,
  options: {
    strategyName: 'azure-oidc',
    oidc: {
      clientId: config.get('azureClientId'),
      discoveryUri: config.get('oidcWellKnownConfigurationUrl'),
      authProvider,
      useHttp: config.get('auth.Mock'),
      loginCallbackUri: config.get('appBaseUrl') + '/auth/callback',
      scope: `api://${config.get('azureClientId')}/cdp.user openid profile email offline_access user.read`,
      externalBaseUrl: config.get('appBaseUrl')
    },
    cookieOptions: {
      isSecure: oidcCookieConfig.isSecure,
      password: oidcCookieConfig.password
    }
  }
}