-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
37 lines (33 loc) · 1.08 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const speakeasy = require('speakeasy');
const {
__TOTP_SECRET: secret,
__TOTP_ENCODING: encoding = 'base32',
__TOTP_ALGORITHM: algorithm = 'sha256',
__TOTP_WINDOW: window = 1,
__TOTP_STEP: step = 30,
__TOTP_DIGITS: digits = 6
} = process.env;
const token = process.argv[2];
/**
* Validate a TOTP token
*
* @param {string} token - The actual token to validate
* @param {string} secret - The secret key used to generate the token
* @param {string} encoding - The encoding used for the secret key
* @param {string} algorithm - The algorithm used to generate the token
* @param {number} window - The allowable time drift in seconds
* @param {number} step - The time step in seconds
* @param {number} digits - The number of digits in the token
* @returns {boolean} - Whether the token is valid
*/
const isValidToken = speakeasy.totp.verify({
token, // the actual token to validate
secret,
encoding,
algorithm,
window,
step,
digits
});
console.log('TOTP token to validate:', token);
console.log('Is the token valid?', isValidToken);