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

@twreporter-academy/keystone-totp

v0.0.2

Published

TOTP (authenticator-app) two-factor authentication for [Keystone 6](https://keystonejs.com/), shaped like `@keystone-6/auth`: `createTotp()` returns `{ withTotp, fields }` and you wrap your config.

Readme

@twreporter-academy/keystone-totp

TOTP (authenticator-app) two-factor authentication for Keystone 6, shaped like @keystone-6/auth: createTotp() returns { withTotp, fields } and you wrap your config.

Install

yarn add @twreporter-academy/keystone-totp
# peer: @keystone-6/core ^6, react ^18

Wire it up

1. Create the plugin (pass your app's Keystone imports)

GraphQL object types must come from the same @keystone-6/core / graphql instance as your lists. Always pass keystone — graphql plus the field constructors this plugin uses to stamp TOTP columns onto that list:

// auth/totp.ts
import { graphql } from '@keystone-6/core'
import { integer, text, timestamp, virtual } from '@keystone-6/core/fields'
import { createCrypto, createTotp } from '@twreporter-academy/keystone-totp'

export const totp = createTotp({
  listKey: 'User',
  identityField: 'email', // used as authenticator label
  issuer: 'My CMS',
  // Local AES for keystone dev; Cloud Run injects KMS via the same crypto shape.
  crypto: createCrypto(process.env.SESSION_SECRET!),
  canManageTotp: (session, targetUserId) =>
    session?.data?.role === 'admin' || session?.itemId === targetUserId,
  // optional: override field access (defaults keep totpSecret/totpLastStep/
  // totpFailedAttempts/totpLockedUntil unwritable via CRUD — disable/reset is
  // clearTotp + canManageTotp only)
  // access: { totpSecret: { update: () => false } },
  keystone: {
    graphql,
    // Host-app constructors (not field configs). Duplicate @keystone-6/core copies
    // break GraphQL types. Spread as totp.fields in step 2:
    //   text → totpSecret, integer → totpLastStep / totpFailedAttempts,
    //   timestamp → totpLockedUntil, virtual → totpControls
    fields: { text, integer, timestamp, virtual },
  },
  // optional: labels (English defaults), pagePath ('/two-factor')
})

2. Spread fields onto the auth list

import { list } from '@keystone-6/core'
import { password, text } from '@keystone-6/core/fields'
import { totp } from '../auth/totp'

export const User = list({
  fields: {
    email: text({ isIndexed: 'unique', validation: { isRequired: true } }),
    password: password({ validation: { isRequired: true } }),
    ...totp.fields, // totpSecret, totpLastStep, totpFailedAttempts, totpLockedUntil, totpControls
  },
})

3. Wrap config outside withAuth

import { createAuth } from '@keystone-6/auth'
import { config } from '@keystone-6/core'
import { statelessSessions } from '@keystone-6/core/session'
import { totp } from './auth/totp'

const { withAuth } = createAuth({
  listKey: 'User',
  identityField: 'email',
  secretField: 'password',
  sessionData: 'id email',
})

export default totp.withTotp(
  withAuth(
    config({
      lists: { User },
      session: statelessSessions({ secret: process.env.SESSION_SECRET! }),
      ui: {
        // Allow password-only (pending TOTP) sessions into the Admin shell
        // so /two-factor can load. pageMiddleware then enforces TOTP.
        isAccessAllowed: (context) => !!context.session?.data,
      },
    })
  )
)

withTotp injects:

  • session start wrapper (totpOk)
  • GraphQL: totpSession, beginTotpEnrollment, confirmTotpEnrollment, verifyTotp, clearTotp
  • /two-factor Admin page via getAdditionalFiles
  • publicPages + pageMiddleware redirects for pending sessions

4. Client gate in custom Navigation

Keystone's sign-in page does router.push('/'), which skips pageMiddleware. If you customize Admin Navigation, render:

import { TotpPendingGate } from '@twreporter-academy/keystone-totp/admin/TotpPendingGate'

function Navigation(props) {
  return (
    <NavigationContainer {...props}>
      <TotpPendingGate />
      {/* ... */}
    </NavigationContainer>
  )
}

5. Gate list access on session.totpOk

access: {
  operation: {
    query: ({ session }) => Boolean(session?.totpOk),
    // ...
  },
}

Behaviour notes

  • Enroll: no totpSecret → /two-factor shows QR; confirm stores the encrypted secret. Admin status follows whether a secret is stored.
  • Verify: secret present → enter code. Password login never sets totpOk.
  • Replay: a successfully used code cannot be reused in the same timestep (totpLastStep); a second verify of the same code fails with invalidCode.
  • Lockout: five consecutive wrong codes set totpLockedUntil for 15 minutes (shared across sessions). Expired locks and successful verifies clear the counter; clearTotp clears it too.
  • Disable: clearTotp only (needs canManageTotp). Wipes totpSecret, totpLastStep, and lockout fields together. List updates cannot write those fields (field access defaults + optional overrides via createTotp({ access })).
  • Encryption: inject crypto: { encryptTotpSecret, decryptTotpSecret } (async). createCrypto(secret) is the local AES-256-GCM helper for development; production hosts typically pass GCP KMS (or similar) instead. Rotating a local AES secret invalidates stored TOTP secrets; KMS key versions continue to decrypt older ciphertext.

Reference

This monorepo's CMS (packages/cms) is the reference consumer: see auth/totp-plugin.ts, keystone.ts, and lists/system-user.ts.