Catalog npm
Back to packageRELEASE DETAILS

1.4.0

npm

2fa-util

Lightweight utility to generate a two-factor TOTP secret with QR code to be used by authenticators such as Google or Microsoft Authenticator.

2 factor2fagoogle authenticatormicrosoft authenticatorqrqr codeqrcodetotptwo factortwo-factor
Published4 Mar 2025
Direct dependencies7
Transitive dependenciesNot calculated

README

Current package documentation

2fa-util

Build Status License NPM Version

A lightweight, robust Node.js utility for generating Two-Factor Authentication (TOTP) secrets, QR codes, and verifying tokens. Compatible with Google Authenticator, Microsoft Authenticator, and Authy.

🚀 Live Demo

Features

  • Easy Setup: Generate a secret and QR code in one function call.
  • Standard Compatible: Works with any RFC 6238 compliant authenticator app.
  • Flexible Verification: Supports custom windows, steps, and other otplib options.
  • Zero-Dependency (Runtime): Bundles necessary logic efficiently (uses otplib and qrcode under the hood).

Installation

npm install 2fa-util

Usage

Basic Example

const { generateSecret, verify } = require('2fa-util');

(async () => {
    // 1. Generate a Secret and QR Code
    const { secret, qrcode, otpauth } = await generateSecret('john.doe@example.com', 'MyApp');
    
    console.log('Secret:', secret);
    console.log('QR Code Data URL:', qrcode); // Display this in an <img src="...">

    // ... User scans QR code ...

    // 2. Verify a Token
    const userToken = '123456'; // Input from user
    const isValid = verify(userToken, secret);

    console.log('Is Valid:', isValid);
})();

Advanced Verification (Custom Options)

You can pass standard otplib options to the verify function, such as window (for clock drift) or step.

const isValid = verify(token, secret, {
    window: 1, // Allow 1 step before/after (approx +/- 30sec)
    step: 60   // Custom step size in seconds
});

API Reference

generateSecret(label, [issuer])

Generates a new TOTP secret and corresponding QR code.

  • label (string): The username or account identifier (e.g., email).
  • issuer (string, optional): The name of your application or company.
  • Returns: Promise<Object>
    • secret: The base32 encoded secret key.
    • qrcode: A Data URI string (base64) of the QR code image.
    • otpauth: The raw otpauth:// URL.

verify(token, secret, [options])

Verifies a TOTP token against a secret.

  • token (string): The 6-digit token provided by the user.
  • secret (string): The user's stored secret key.
  • options (Object, optional): Configuration object passed to otplib.
  • Returns: boolean (true if valid, false otherwise).

generate(secret)

Generates the current token for a given secret (useful for testing or dev tools).

  • secret (string): The secret key.
  • Returns: string (The current 6-digit token).

Development

Clone the repository and install dependencies:

git clone https://github.com/jzhobes/2fa-util.git
cd 2fa-util
npm install

Run tests:

npm test

Run linting:

npm run lint

License

MIT © John Ho

Downloads & popularity

5downloads
7-day window · source-reported counts22 Oct 2026
52.50
2026-10-22: 5 · observed 2026-10-22T21:23:42.2534837Z
22 Oct22 Oct

npm · 1 observations shown. Missing days are unknown.

View observed values
Date / source periodValueObserved at
2026-10-2252026-10-22T21:23:42.2534837Z

Declared dependencies 7

Native requirements
7 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
otplib
Environment
{
  "local_name": "otplib",
  "alias": false,
  "peer_metadata": null
}
^12.0.1runtime
qrcode
Environment
{
  "local_name": "qrcode",
  "alias": false,
  "peer_metadata": null
}
^1.5.4runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^29.7.0development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^9.21.0development
globals
Environment
{
  "local_name": "globals",
  "alias": false,
  "peer_metadata": null
}
^16.0.0development
@eslint/js
Environment
{
  "local_name": "@eslint/js",
  "alias": false,
  "peer_metadata": null
}
^9.21.0development
eslint-plugin-jest
Environment
{
  "local_name": "eslint-plugin-jest",
  "alias": false,
  "peer_metadata": null
}
^28.11.0development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "426b9e1c4c329641c885287410b97c862e669276",
  "tarball": "https://registry.npmjs.org/2fa-util/-/2fa-util-1.4.0.tgz",
  "fileCount": 10,
  "integrity": "sha512-MWo+aZR2OfltNl8UpxxnCVWRFRlsF3J7FXpVinhPBI3mT+T2TyKNfrDlK+jzAWbNHRhTZ1iIrPav93F3vAPpNA==",
  "signatures": [
    {
      "sig": "MEUCIElVtOgyJuC6DB6KAEG/q0dloo97zsdckYk4vnEHimpgAiEAzHKT20Mbm6ZAK8LPIKeKAr+GtVFuhhdpK9aT41y63VY=",
      "keyid": "SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"
    }
  ],
  "unpackedSize": 11776
}

Repository

PENDING
jzhobes/2fa-util

Shared repository data · GitHub

License detected in repository
Not collected

Repository

PENDING
jzhobes/2fa-config

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:12 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:935f3f1a8d85ccd1fb7cf930ccaf73d2e167d491aa545b5d0f26c59a5efc0d1b

Other collected versions

1.5.04 Dec1.2.015 May1.1.11 Nov1.1.013 Oct1.0.311 Oct1.0.27 OctView all collected versions →