2fa-util

中文文档
1.5.0发布于 2025/12/04MITCommonJS近 30 天下载 8715

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

原始使用文档
文档目录12 节
  1. 🚀 Live Demo
  2. Features
  3. Installation
  4. Usage
  5. Basic Example
  6. Advanced Verification (Custom Options)
  7. API Reference
  8. generateSecret(label, [issuer])
  9. verify(token, secret, [options])
  10. generate(secret)
  11. Development
  12. License

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('[email protected]', '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

版本与发布记录

2fa-util 共发布 9 个版本,首次发布于 2021/10/07;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.5.0latest2025/12/0411 个月前
1.4.02025/03/041.7 年前
1.2.02023/05/153 年前
1.1.12021/11/025 年前
1.1.02021/10/145 年前
1.0.32021/10/125 年前

关于 2fa-util 的常见问题

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.

如何安装 2fa-util?

在项目目录运行 npm install 2fa-util,或使用 pnpm add / yarn add / bun add。

2fa-util 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/2fa-util 是否存在。

2fa-util 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

2fa-util 可以商用吗?

2fa-util 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

2fa-util 的下载量有多少?

根据 npm 官方下载统计,2fa-util 在 2026/10/04 – 2026/11/02 这 30 天内被下载 8,715 次,日均约 291 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

2fa-util 还在维护吗?

最新版本 1.5.0 发布于 2025/12/04(11 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2021/10/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。