-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·60 lines (53 loc) · 1.53 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env node
const cli = require('cli');
const OTP = require('otp');
const options = {
counter: ['c', 'Counter for HTOP'],
help: ['h', 'Help'],
method: ['m', 'Method for OTP HOTP or TOTP.', 'string', 'TOTP'],
otp: ['o', 'OTP value to be verified. Use with verify command', 'int'],
secret: ['s', 'Secret to be used for OTP generation. Can be set using OTP_SECRET environment variable. parameter overrides environment variable', 'string']
}
const params = cli.parse(options, ['generate', 'verify']);
const secret = params.secret ? params.secret : process.env.OTP_SECRET;
if (secret === null || secret === undefined) {
console.log('ERROR: Secret missing.\n\n');
cli.getUsage(1);
}
if (params.method === 'HOTP' && params.counter === null) {
console.log('ERROR: Counter required. HOTP Method selected.\n\n');
cli.getUsage(1);
}
var otp = new OTP({
secret: secret
});
const generatedOTP = params.method === 'TOTP' ? otp.totp() : otp.hotp(params.counter);
switch (cli.command) {
case 'generate':
if (params.help) {
cli.getUsage(1);
} else {
console.log(generatedOTP);
}
break;
case 'verify':
if (params.help) {
cli.getUsage(1);
} else {
if (params.otp === null) {
console.log('ERROR: OTP required.\n\n');
cli.getUsage(1);
}
if (generatedOTP === params.otp.toString()) {
console.log('OK')
process.exit(0);
} else {
console.log('FAIL');
process.exit(1);
}
}
break;
default:
cli.getUsage(1);
break;
}