Catalog npm
Back to packageRELEASE DETAILS

1.1.1

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
Published1 Nov 2021
Direct dependencies5
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

631downloads
7-day window · source-reported counts22 Oct 2026
631315.50
2026-10-22: 631 · 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-226312026-10-22T21:23:42.2534837Z

Declared dependencies 5

Native requirements
5 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.4.4runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^27.3.1development
eslint
Environment
{
  "local_name": "eslint",
  "alias": false,
  "peer_metadata": null
}
^8.1.0development
eslint-plugin-jest
Environment
{
  "local_name": "eslint-plugin-jest",
  "alias": false,
  "peer_metadata": null
}
^25.2.2development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "3c4ba97e85a0247d8057ff147c49fcce0b3e7dbb",
  "tarball": "https://registry.npmjs.org/2fa-util/-/2fa-util-1.1.1.tgz",
  "fileCount": 9,
  "integrity": "sha512-DplBQOPMheQuutnf9friLipU9XKinEFn2wQBB6RT6bnrPbDOhY/wXB4A8kH3yhagb7Z/sqIOrAwuXTeDY0OmIA==",
  "signatures": [
    {
      "sig": "MEQCIEgNi1SXjx/Rmhru9SSg//TiM0DpekuNHTw0nut8bNXgAiArmbjyO/uaD6cdI5npKjsuFXFIuL6kNb7YvWrdEpssng==",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 11088,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhuebaCRA9TVsSAnZWagAA/KgP/1zNZlYqTrqRZeCptNMg\nH/LziGgUXlCLhZdf8m6PuCilgQEwz1qSA2/xY6V+k+hGMGd837X1Gw5rGjDw\nUwM1d9WfjdW+Pj0G5YN8YBooazgvisuGuE9hecWeZa+TooQmciwj6MYQ3c0G\nJ/uQmZ/4C8TSzAi+PlpJRfx3SX/bQVs8uItsdYRdsIpS5NHrLhAA+GtsUnPm\n0b85yfYv8ccCne4LD4n1fs7MNOdLrXVuLYg8rKtKoF01gwmJhBQWzCL0gb98\n1HHtF256hihb4K8/8Rcxr/cDcYJL1U3yRfLtNxOHZrH0Q9pYd+vrVOMMN8/9\nDJ2ep1JOEysk46PiW0O1B1QQKK4yu5COXdn4b51UOzvmu9rUlbk/y3fFHj9i\nXqHo6zqxZYlBNjjgqsQ5ZLtx6oHjCO+XA68dmB/rrD/lklRoXtr2BggW3A6P\nIb6uP8krtd16eghC4IcX3wihns53oetZYS6sVFJNwQ3xHMyDU3XMsE1fN75f\nXW78yPkGX41LADHgBLLBrvdKnwuYWWX1uNA4S0aTdh8/NJXszj81sq81aTBu\nadyvhiw9fN2aPy6BakHu7N4tzFzBb7WpRg9f37AIFdqb4GnweJoI2BvzRS26\nbjgYmonnSZf2RY7NcFBlAwN4dTK4rh7qsaA/SHH9IA3W9As3biWiKZdY2G4M\niaS6\r\n=PX+y\r\n-----END PGP SIGNATURE-----\r\n"
}

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:d484740aecc6865b8f6f463e1ae8ce95b2c918f16a3e2f796911fc7e4a714c71

Other collected versions

1.5.04 Dec1.4.04 Mar1.2.015 May1.1.013 Oct1.0.311 Oct1.0.27 OctView all collected versions →