我正在使用https://www.npmjs.com/package/speakeasy生成OTP,我希望到期时间为10分钟。
这是生成的代码
const generateOtp = function generateOtp() {
let token = speakeasy.totp({
secret:process.env.OTP_KEY,
encoding: 'base32',
digits:4,
window:10
});
return token;
}验证OTP
const verifyOtp = function verifyOtp(token){
let expiry = speakeasy.totp.verify({
secret:process.env.OTP_KEY,
encoding: 'base32',
token: token,
window:10
});
console.log(expiry)
}但我不知道如何将有效期设定为10分钟?
发布于 2018-09-22 20:44:07
阅读文档,您可以发现基本的step是30秒,所以如果您想要有10分钟的到期时间,您需要将step设置为60。然后,使用verifyDelta方法,您应该能够检查令牌是否过期。
const generateOtp = function generateOtp() {
let token = speakeasy.totp({
secret:process.env.OTP_KEY,
encoding: 'base32',
digits:4,
step: 60,
window:10
});
return token;
}
const verifyOtp = function verifyOtp(token){
let expiry = speakeasy.totp.verifyDelta({
secret:process.env.OTP_KEY,
encoding: 'base32',
token: token,
step: 60,
window:10
});
console.log(expiry)
}https://stackoverflow.com/questions/52460530
复制相似问题