令牌使用nodejs speakeasy库失效

时间:2017-12-08 20:17:50

标签: node.js

我的目的是在1小时(3600秒)后使令牌失效。尝试使用nodejs speakeasy时,令牌在此之前就会失效。下面的日志是“1分钟,10分钟和60分钟”,并且在1分钟之前也会失效muche。最大的时间我得到不一致的结果。

部分代码段

let secret = speakeasy.generateSecret({
length: 10
});
let seconds= 3600; //1Hr

let token = speakeasy.totp({
  secret: secret.base32,
  step: seconds
});

let otp = {
  "secret": secret.base32.toString(),
  "token": token
};

function checkOTP(otp) {
  let verified = speakeasy.totp.verify({
    secret: otp.secret,
    token: otp.token,
    step: seconds
});
return verified;
}

我做错了吗?来自示例脚本的控制台日志很少:

For 1 minute - Invalidated before 18secs
[ Fri Dec 08 2017 09:16:18 GMT-0800 (Pacific Standard Time) ](true) 9:16:59 AM
[ Fri Dec 08 2017 09:16:18 GMT-0800 (Pacific Standard Time) ](false) 9:17:00 AM

For 10Mins - Invalidated before 7minutes
[ Fri Dec 08 2017 09:18:28 GMT-0800 (Pacific Standard Time) ](true) 9:19:59 AM
[ Fri Dec 08 2017 09:18:28 GMT-0800 (Pacific Standard Time) ](true) 9:19:59 AM
[ Fri Dec 08 2017 09:18:28 GMT-0800 (Pacific Standard Time) ](true) 9:19:59 AM
[ Fri Dec 08 2017 09:18:28 GMT-0800 (Pacific Standard Time) ](true) 9:19:59 AM
[ Fri Dec 08 2017 09:18:28 GMT-0800 (Pacific Standard Time) ](false) 9:20:00 AM

For 1Hr - Invalidated before 7minutes
[ Fri Dec 08 2017 11:07:01 GMT-0800 (Pacific Standard Time) ](true) 11:56:41 AM
[ Fri Dec 08 2017 11:07:01 GMT-0800 (Pacific Standard Time) ](true) 11:56:43 AM
[ Fri Dec 08 2017 11:07:01 GMT-0800 (Pacific Standard Time) ](false) 12:00:37 PM

在上述窗口中验证的适当方法是什么?

1 个答案:

答案 0 :(得分:0)

从自述文件的自述文件中看来,您的token参数错误:

var token = speakeasy.totp({
  secret: secret.base32,
  encoding: 'base32',
  time: 1453667708 // You have this as 'step' not 'time' 
});