@duosecurity/duo_universal

中文文档
3.1.0发布于 2026/08/21MITTypeScriptESM + CJS构建来源已验证近 30 天下载 3.2万

Node.js implementation of the Duo Universal SDK.

原始使用文档
文档目录13 节
  1. Getting Started
  2. TLS 1.2 and 1.3 Support
  3. Usage Details
  4. 1. Import client
  5. 2. Create client
  6. 3. Heath check
  7. 4. Generate state
  8. 5. Create authentication URL
  9. 6. Token & code exchange
  10. Example
  11. Contribute
  12. Tests
  13. Lint

Duo Universal Node.js library

Build Status npm version GitHub license

This library allows a web developer to quickly add Duo's interactive, self-service, two-factor authentication to any Node.js web login form.

See our developer documentation at https://www.duosecurity.com/docs/duoweb for guidance on integrating Duo 2FA into your web application.

Duo especially thanks Lukas Hroch for creating the initial version of this library.

Getting Started

This library requires Node.js LTS (v20 or later).

To use this client in your existing developing environment, install it with your package manager of choice.

npm install @duosecurity/duo_universal

pnpm add @duosecurity/duo_universal

yarn add @duosecurity/duo_universal

Once it's installed, see our developer documentation at https://duo.com/docs/duoweb and the example folder in this repo for guidance on integrating Duo 2FA into your web application.

TLS 1.2 and 1.3 Support

Duo_universal_nodejs uses the Node tls library and OpenSSL for TLS operations. All versions of Node receiving security support (14 and higher) use OpenSSL 1.1.1 which supports TLS 1.2 and 1.3.

Usage Details

1. Import client

import { Client } from '@duosecurity/duo_universal';

2. Create client

Creates new client instance. Provide your Duo Security application credentials and host URL. Include redirect URL to make a way back to your application.

const client = new Client({
    clientId: 'yourDuoApplicationClientId',
    clientSecret: 'yourDuoApplicationSecret',
    apiHost: 'api-12345678.duosecurity.com',
    redirectUrl: 'http://localhost:3000/redirect',
});

3. Heath check

Determines if Duo’s servers are accessible and available to accept the 2FA request.

const status = await client.healthCheck();

4. Generate state

Generates new state (random string) to link the with authentication attempt. Store appropriately, so you can retrieve/compare on callback.

const state = client.generateState();

5. Create authentication URL

Creates authentication URL to redirect user to Duo Security Universal prompt. Provide user identifier and state generated in previous step.

const authUrl = await client.createAuthUrl('username', 'state');

6. Token & code exchange

Exchanges received duo code from callback redirect for token result.

const token = await client.exchangeAuthorizationCodeFor2FAResult('duoCode', 'username');

Example

A complete implementation example can be found in example/. It's a simple express-based application. Please follow the example/README.md to spin it up.

Contribute

Fork the repository

Install dependencies

pnpm install

Make your proposed changes. Add tests if applicable, lint the code. Submit a pull request.

Tests

pnpm run test

Lint

pnpm run lint

版本与发布记录

@duosecurity/duo_universal 共发布 8 个版本,首次发布于 2022/02/02;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.1.0latest2026/08/212 个月前
3.0.12026/04/017 个月前
3.0.02025/07/211.3 年前
2.1.02025/04/291.5 年前
2.0.32024/03/262 年前
2.0.22023/02/093 年前

关于 @duosecurity/duo_universal 的常见问题

@duosecurity/duo_universal 是什么?

Node.js implementation of the Duo Universal SDK.

如何安装 @duosecurity/duo_universal?

在项目目录运行 npm install @duosecurity/duo_universal,或使用 pnpm add / yarn add / bun add。

@duosecurity/duo_universal 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@duosecurity/duo_universal 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@duosecurity/duo_universal 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@duosecurity/duo_universal 可以商用吗?

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

@duosecurity/duo_universal 的下载量有多少?

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

@duosecurity/duo_universal 还在维护吗?

最新版本 3.1.0 发布于 2026/08/21(2 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/02/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。