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

@airdraft/auth

v0.1.7

Published

Airdraft authentication utilities — session, API key, embed token

Readme

@airdraft/auth

Low-level JWT utilities and RBAC types for Airdraft. Used internally by @airdraft/plugin-auth and @airdraft/next. You typically don't need this package directly unless you're building custom auth middleware or an Airdraft adapter.

Installation

npm install @airdraft/auth

RBAC

Roles

type Role = 'admin' | 'publisher' | 'editor'

| Role | Description | |---|---| | admin | Full access — manage schema, team, and content | | publisher | Can publish and delete published entries; no team/schema management | | editor | Can create and edit drafts; cannot publish or delete published entries |

RolePermissions

interface RolePermissions {
  publish: boolean
  deletePublished: boolean
  manageSchema: boolean
  manageTeam: boolean
}

ROLE_PERMISSIONS

import { ROLE_PERMISSIONS } from '@airdraft/auth'

ROLE_PERMISSIONS.admin     // { publish: true, deletePublished: true, manageSchema: true, manageTeam: true }
ROLE_PERMISSIONS.publisher // { publish: true, deletePublished: true, manageSchema: false, manageTeam: false }
ROLE_PERMISSIONS.editor    // { publish: false, deletePublished: false, manageSchema: false, manageTeam: false }

can(user)

Returns RolePermissions for the given user:

import { can } from '@airdraft/auth'

const perms = can(user)
if (perms.manageTeam) { /* ... */ }

Tokens

signAccessToken(user, secret, ttl?)

Signs a JWT access token. TTL defaults to ACCESS_TOKEN_TTL (15 minutes).

signRefreshToken()

Generates a random opaque refresh token string.

verifyAccessToken(token, secret)

Returns AuthUser | null. Returns null on expiry or invalid signature.

Token TTLs

import { ACCESS_TOKEN_TTL, REFRESH_TOKEN_TTL } from '@airdraft/auth'

ACCESS_TOKEN_TTL   // 14400 seconds (4 hours)
REFRESH_TOKEN_TTL  // 2592000 seconds (30 days)

API Keys

generateApiKey()

Generates a new API key pair. The returned key is a secret to be given to the client; hash is a SHA-256 hex digest to store server-side.

import { generateApiKey } from '@airdraft/auth'

const { key, hash } = generateApiKey()
// key: 'ntk_<64-char-hex>' — send to client once
// hash: '<sha256-hex>'    — store in database

verifyApiKey(raw, hash)

Returns true if the raw key matches the stored hash.

import { verifyApiKey } from '@airdraft/auth'

const valid = verifyApiKey(incoming, stored.hash)

Embed Tokens

Short-lived HMAC-SHA256 signed tokens for public read-only embeds. Tokens are base64url-encoded and verified with crypto.timingSafeEqual to prevent timing attacks.

import { generateEmbedToken, verifyEmbedToken } from '@airdraft/auth'

const token = generateEmbedToken(
  {
    projectId: 'proj_123',
    allowedOrigin: 'https://my-site.com',
    ttlSeconds: 3600,       // default 3600, max 86400
    claims: { collections: ['posts'] },  // optional
  },
  process.env.EMBED_TOKEN_SECRET!,
)

const result = verifyEmbedToken(token, process.env.EMBED_TOKEN_SECRET!, requestOrigin)
if (result.valid) {
  console.log(result.payload.projectId)
} else {
  // result.reason: 'expired' | 'origin_mismatch' | 'invalid_signature' | 'malformed'
}

Breaking change (v0.1.5): The embed token signature algorithm changed from a concatenated SHA-256 hash to proper HMAC-SHA256 (createHmac). Tokens issued by earlier versions will fail verification. Reissue any live embed tokens after upgrading.

Types

interface AuthUser {
  id: string
  email: string
  name?: string
  role: Role
  meta?: Record<string, unknown>
}

interface ApiKeyPair {
  key: string   // 'ntk_<64-char-hex>' — send to client once
  hash: string  // SHA-256 hex digest — store server-side, never return to clients
}

Changelog

See CHANGELOG.md.