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

@nitrotool/oauth-server

v1.0.5

Published

## Installation

Readme

@nitrotool/oauth-server

Installation

pnpm add @nitrotool/oauth-server

Usage

Authorize Endpoint

// server/api/oauth/authorize.ts
import {
  defineOAuthAuthorizeHandler,
  isValidRedirectUrl,
  oauthError,
} from '@nitrotool/oauth-server';
import { sendRedirect } from 'h3';

export default defineOAuthAuthorizeHandler(async (event, payload) => {
  //This will always pass, since we return the id we give
  const client = await getOAuthClientById(payload.client_id);

  if (!client) {
    return oauthError(event, 'unauthorized_client', 'Invalid client ID.');
  }

  if (!isValidRedirectUrl(payload.redirect_uri, client.redirectUris)) {
    return oauthError(event, 'invalid_request', 'Invalid redirect URI.');
  }

  //TODO: Store & fetch this somewhere safe.
  const userId = '1234567890';
  const code = 'SOME-RANDOM-CODE';

  await saveAuthCode(code, {
    client_id: payload.client_id,
    redirect_uri: payload.redirect_uri,
    user_id: userId,
    scope: payload.scope,
    code_challenge: payload.code_challenge,
  });

  const url = new URL(payload.redirect_uri);
  url.searchParams.set('code', code);
  if (payload.state) {
    url.searchParams.set('state', payload.state);
  }

  return sendRedirect(event, url.toString());
});

OAuth Token Endpoint

// server/api/oauth/token.post.ts
import { defineOAuthTokenHandler, oauthError } from '@nitrotool/oauth-server';

export default defineOAuthTokenHandler(async (event, payload) => {
  if (payload.grant_type === 'authorization_code') {
    const data = await consumeAuthCode(payload.code);
    if (!data)
      return oauthError(event, 'invalid_grant', 'Invalid authorization code.');

    // Use shared validation
    const error = validateTokenRequest(event, payload, data);
    if (error) return error;

    return issueTokens(data, payload.client_id);
  }

  if (payload.grant_type === 'refresh_token') {
    const data = await getRefreshToken(payload.refresh_token);
    if (!data)
      return oauthError(event, 'invalid_grant', 'Invalid refresh token.');

    // Just issue a new access token for refresh flow
    const tokens = await issueTokens(data, payload.client_id);
    return {
      access_token: tokens.access_token,
      token_type: tokens.token_type,
      expires_in: tokens.expires_in,
    };
  }

  return oauthError(event, 'unsupported_grant_type');
});

OIDC Authorize endpoint

// server/api/oidc/authorize.ts
import {
  defineOAuthServerHandler,
  oidcAuthorizeSchema, // Use the OIDC schema with nonce/scope requirements
} from '@nitrotool/oauth-server';
import { sendRedirect, getQuery } from 'h3';

export default defineOAuthServerHandler({
  schema: oidcAuthorizeSchema,
  read: getQuery,
  async handler(event, payload) {
    // 1. Shared Validation
    const { client, error } = await validateAuthorizeRequest(event, payload);
    if (error) return error;

    // 2. Identity Logic (Typically behind a session check/login)
    const userId = '1234567890';
    const code = 'OIDC-' + Math.random().toString(36).substring(2);

    // 3. Store OIDC Context
    // Crucially, we store the 'nonce' and 'scope' provided in the request
    await saveAuthCode(code, {
      client_id: payload.client_id,
      redirect_uri: payload.redirect_uri,
      user_id: userId,
      scope: payload.scope, // Will include 'openid'
      nonce: payload.nonce, // <--- MUST be stored for OIDC
      code_challenge: payload.code_challenge,
    });

    // 4. Redirect back to client
    const redirectUrl = buildAuthorizeRedirect(
      payload.redirect_uri,
      code,
      payload.state,
    );
    return sendRedirect(event, redirectUrl);
  },
});
  • payload is strongly typed as OIDCAuthQuery.
  • Always echo back the state parameter.
  • Store the nonce from payload.nonce with the authorization code for inclusion in the id_token.

OIDC Token Endpoint

// server/api/oidc/token.ts
import {defineOIDCTokenHandler, OIDCTokenResponse} from '@nitrotool/oauth-server'

export default defineOIDCTokenHandler({
  async consumeCode(code) {
    return await consumeAuthCode(code);
  },
  async handler(event, payload, ctx) {
    // 1. Validation (Payload must be auth_code grant for OIDC)
    const error = validateTokenRequest(event, payload as any, ctx);
    if (error) return error;

    // 2. Issue Standard Tokens
    const tokens = await issueTokens(ctx, payload.client_id);

    // 3. Generate OIDC specific ID Token
    const idToken = await encodeJwt(
            {
              sub: ctx.user_id,
              aud: payload.client_id,
              nonce: ctx.nonce,
              type: 'id_token',
            },
            3600,
    );

    return {
      ...tokens,
      id_token: idToken,
    };
  },
});
  • payload is strongly typed as OIDCTokenPayload.
  • The return type must conform to OIDCTokenResponse.
  • access_token and expires_in are required; refresh_token, scope, and id_token are optional depending on the grant and requested scopes.

Notes

  • No client code: This library is purely for implementing an OAuth/OIDC provider (server).
  • You must persist authorization codes, nonces, and optionally refresh tokens in your database.
  • isValidRedirectUrl helps safely validate client redirect URIs.