2factor-auth 中文文档教程

发布于 8年前 浏览 31 项目主页 更新于 3年前

2FA

NPM

用于生成和验证 2FA 代码(特别是 TOTP 和 HOTP)的模块。

还包含用于处理 2FA 逻辑的实用程序,例如生成备份代码。

Install

npm install --save 2factor-auth

Usage

var tfa = require('2factor-auth');

// lets generate a new key for a user
// tfa.generateKey(length (optional), cb)
tfa.generateKey(32, function(err, key) {
  // crypto secure hex key with 32 characters

  // generate crypto-secure backups codes in a user-friendly pattern
  // tfa.generateBackupCodes(num, pattern (optional), cb)
  tfa.generateBackupCodes(8, 'xxxx-xxxx-xxxx', function(err, codes) {
    // [ '7818-b7b8-c928', '3526-dc04-d3f2', 'be3c-5d9f-cb68', ... ]

    // these should be sent to the user, stored and checked when we get a 2fa code
  });  

  var opts = {
    // the number of counters to check before what we're given
    // default: 0
    beforeDrift: 2,
    // and the number to check after
    // default: 0
    afterDrift: 2,
    // if before and after drift aren't specified,
    // before + after drift are set to drift / 2
    // default: 0
    drift: 4,
    // the step for the TOTP counter in seconds
    // default: 30
    step: 30
  };

  // calculate the counter for the HOTP (pretending it's actually TOTP)
  var counter = Math.floor(Date.now() / 1000 / opts.step);

  // generate a valid code (in real-life this will be user-input)
  var code = tfa.generateCode(key, counter);

  // verify it as a HOTP
  var validHOTP = tfa.verifyHOTP(key, code, counter, opts);
  // true

  // for TOTP, the counter is calculated internally using Date.now();
  var validTOTP = tfa.verifyTOTP(key, code, opts);
  // true

});

2FA

NPM

Module for generating and verifying 2FA codes (specifically TOTP and HOTP).

Also contains utilities for handing 2FA logic, such as generating backup codes.

Install

npm install --save 2factor-auth

Usage

var tfa = require('2factor-auth');

// lets generate a new key for a user
// tfa.generateKey(length (optional), cb)
tfa.generateKey(32, function(err, key) {
  // crypto secure hex key with 32 characters

  // generate crypto-secure backups codes in a user-friendly pattern
  // tfa.generateBackupCodes(num, pattern (optional), cb)
  tfa.generateBackupCodes(8, 'xxxx-xxxx-xxxx', function(err, codes) {
    // [ '7818-b7b8-c928', '3526-dc04-d3f2', 'be3c-5d9f-cb68', ... ]

    // these should be sent to the user, stored and checked when we get a 2fa code
  });  

  var opts = {
    // the number of counters to check before what we're given
    // default: 0
    beforeDrift: 2,
    // and the number to check after
    // default: 0
    afterDrift: 2,
    // if before and after drift aren't specified,
    // before + after drift are set to drift / 2
    // default: 0
    drift: 4,
    // the step for the TOTP counter in seconds
    // default: 30
    step: 30
  };

  // calculate the counter for the HOTP (pretending it's actually TOTP)
  var counter = Math.floor(Date.now() / 1000 / opts.step);

  // generate a valid code (in real-life this will be user-input)
  var code = tfa.generateCode(key, counter);

  // verify it as a HOTP
  var validHOTP = tfa.verifyHOTP(key, code, counter, opts);
  // true

  // for TOTP, the counter is calculated internally using Date.now();
  var validTOTP = tfa.verifyTOTP(key, code, opts);
  // true

});
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文