你可以在你的 npm 用户账户上启用双重身份验证 (2FA),以使用 安全密钥 防止未经授权的访问你的账户和软件包。

🌐 You can enable two-factor authentication (2FA) on your npm user account to protect against unauthorized access to your account and packages using a security-key.

重要: 发布到 npm 需要以下条件之一:

  • 你的账户已启用双重身份验证 (2FA),或者
  • 启用绕过双重身份验证 (2FA) 的细粒度访问令牌

欲了解更多信息,请参阅 npm 关于发布包时要求两步验证的文档。

🌐 For more information, see the npm documentation on requiring 2FA for package publishing.

先决条件

🌐 Prerequisites

在你的 npm 用户账户上启用 2FA 之前,你必须:

🌐 Before you enable 2FA on your npm user account, you must:

有关支持的双重身份验证(2FA)方法的更多信息,请参见“关于双重身份验证”。

🌐 For more information on supported 2FA methods, see "About two-factor authentication".

从网站配置 2FA

🌐 Configuring 2FA from the website

启用 2FA

🌐 Enabling 2FA

  1. 在 npm“登录”页面上,输入您的帐户详细信息并单击登录 Screenshot of npm login dialog
  2. 在页面的右上角,点击您的个人资料照片,然后点击帐户 Screenshot of account settings selection in user menu
  3. 在账户设置页面的“二步验证”下,点击 启用二步验证

    Screenshot showing Enable 2FA button
  4. 当提示时,输入你当前的账户密码,然后点击 确认密码以继续

  5. 在双因素认证(2FA)方法页面,选择你想启用的方法,然后点击 继续。有关支持的2FA方法的更多信息,请参见“关于双重身份验证”。

    Screenshot showing 2FA types
  6. 配置你的安全密钥:

    • 为你的安全密钥提供一个名称,然后点击 添加安全密钥。按照浏览器的具体步骤添加你的安全密钥。
    Screenshot showing security key setup
  • 以下是在 MacOS 上运行的 Microsoft Edge 的配置示例

    Screenshot showing 2FA device selection
  1. 在恢复代码页面,将恢复代码复制到你的电脑或其他安全位置,而不是第二验证设备。我们建议使用密码管理器。

    Screenshot showing the Recovery Code page

    恢复码是确保在丢失第二因素设备时仍能找回账户的唯一方式。每个代码只能使用一次。你可以在双重验证(2FA)设置页面上 查看并重新生成你的恢复码。有关辅助账户恢复选项,请参阅“配置账户恢复选项”。

  2. 确认已保存代码后,点击 返回设置

禁用 2FA

🌐 Disabling 2FA

如果你启用了 2FA,则可以将其从你的账户设置页面中删除。

🌐 If you have 2FA enabled, you can remove it from your account settings page.

注意: 如果你是要求使用双重身份验证(2FA)的组织成员,则无法移除2FA。你可以在个人资料页面的“组织”选项卡下查看你的组织成员身份列表。

  1. 在 npm“登录”页面上,输入您的帐户详细信息并单击登录 Screenshot of npm login dialog
  2. 在页面的右上角,点击您的个人资料照片,然后点击帐户 Screenshot of account settings selection in user menu
  3. 在账户设置页面的“二步验证”下,点击 修改二步验证

    Screenshot showing Modify 2FA button
  4. 滚动到“管理双重认证”页面的底部,然后点击禁用双重认证(2FA)。

    Screenshot showing Disable 2FA button
  5. 同意浏览器的提示。

从命令行配置 2FA

🌐 Configuring 2FA from the command line

从命令行启用 2FA

🌐 Enabling 2FA from the command line

使用 WebAuthn 的安全密钥可以用于从 Web 和命令行进行身份验证,但只能通过 Web 进行配置。

🌐 Security-key with WebAuthn can be used for authentication from both the web and the command line, but it can only be configured from the web.

注意: 你在命令行上配置的设置也将应用于你在 npm 网站上的个人资料设置。

为了获得最佳安全体验,我们建议你通过 Web 界面配置双重身份验证 (2FA),你可以在其中设置安全密钥身份验证。

🌐 For the best security experience, we recommend configuring 2FA through the web interface where you can set up security-key authentication.

如果你需要从命令行启用双重身份验证 (2FA):

🌐 If you need to enable 2FA from the command line:

  1. 如果你在命令行中已退出,请使用 npm login 命令登录。
  2. 在命令行中,输入 npm profile 命令,并附上你想要启用的双重认证(2FA)模式选项:
    • 要为授权和写入启用 2FA,请键入:

      npm profile enable-2fa auth-and-writes
    • 要仅为授权启用 2FA,请键入:

      npm profile enable-2fa auth-only

使用命令进行双重身份验证

🌐 Using 2FA with commands

如果你已启用 2FA 验证与写入,则在使用安全密钥时认证将自动处理。对于需要 2FA 的命令,系统会提示你使用已配置的 2FA 方法进行验证。

🌐 If you have enabled 2FA auth-and-writes, authentication will be handled automatically when using security-keys. For commands that require 2FA, you will be prompted to authenticate with your configured 2FA method.

从命令行删除 2FA

🌐 Removing 2FA from the command line

  1. 如果你在命令行中已退出,请使用 npm login 命令登录。

  2. 在命令行上,键入以下命令:

    npm profile disable-2fa
  3. 出现提示时,输入你的 npm 密码:

    npm 密码:
  4. 系统将提示你使用已配置的双重身份验证 (2FA) 方法进行身份验证以完成移除操作。

配置账户恢复选项

🌐 Configuring account recovery options

当你在 npm 用户账户上启用双重身份验证(2FA)时,我们强烈建议你将 GitHub 账户与你的 npm 用户账户关联。如果你丢失了访问 2FA 设备和恢复代码的权限,可以使用这个关联账户来验证你的身份,加快 npm 账户的恢复过程。

🌐 When you enable 2FA on your npm user account, we strongly recommend you link your GitHub account to your npm user account. In the event you lose access to your 2FA device and recovery codes, this linked account can be used to verify your identity and expedite the recovery of your npm account.

  1. 在 npm“登录”页面上,输入您的帐户详细信息并单击登录 Screenshot of npm login dialog
  2. 在页面的右上角,点击您的个人资料照片,然后点击帐户 Screenshot of account settings selection in user menu
  3. 链接你的 GitHub 账户的账户设置页面中,在“关联账户与恢复选项”下,点击 与 GitHub 关联

    Screenshot showing Link GitHub account button
  4. 在授权页面上,确认所有信息是否正确。然后点击 授权 npm 账户链接

GitHub 账户现在已与你的 npm 账户关联。要移除关联,你可以点击账户旁边的 移除 按钮。

🌐 The GitHub account is now linked to your npm account. To remove the link, you can click the Remove button next to the account.