-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.js
executable file
·57 lines (51 loc) · 1.27 KB
/
generate.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
'use strict';
const Configstore = require('configstore');
const pkg = require('./package.json');
const passcode = require('passcode');
const util = require('util');
const chalk = require('chalk');
const conf = new Configstore(pkg.name, {})
function doGenerate(label, nolabel) {
var value = conf.get(label);
var secret = value.secret;
var token = passcode.totp({
secret: secret,
encoding: value.encoding || 'base32',
digits: value.digits || 6,
algorithm: value.algorithm || 'sha1',
step: value.step || 30
});
if (!nolabel) {
console.log(util.format(
'%s : %s',
chalk.cyan(label),
chalk.bold.green(token)));
} else {
console.log(token);
}
}
function generate(arg, options) {
var nolabel = options.nolabel;
if (arg) doGenerate(arg, nolabel);
else {
var keys = Object.keys(conf.all);
for (var n = 0; n < keys.length; n++) {
doGenerate(keys[n],nolabel);
};
}
}
generate.list = function(arg, options) {
var keys = Object.keys(conf.all);
for (var n = 0; n < keys.length; n++) {
console.log(keys[n]);
};
};
generate.delete = function(arg, options) {
if (!arg) {
console.error('no label provided');
process.exit(-1);
}
conf.del(arg);
console.log('deleted!');
};
module.exports = generate;