-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (53 loc) · 1.74 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
59
60
61
const fs = require('fs');
const prompt = require('prompt-sync')();
const dictionaryGenerator = require('./dictionary-generator');
const tokenGenerator = require('./token-generator');
/**
* Maximum amount of attempts for entering verification code
*/
const maxAttempts = 5;
/**
* Destination of passphrase dump for debug purposes
*/
const dumpFileName = 'passphrase_dump.txt';
/**
* Current command arguments from the console
*/
const argv = require('minimist')(process.argv.slice(2));
/**
* Dumps passphrase, secret & salt into temporary file.
* This shouldn't be done in real application - you would
* persist secret and salt, send passphrase to user and
* then remove it from the memory.
*/
const dumpPassphrase = (tokenBundle) => {
const dump = `Passphrase: ${tokenBundle.passPhrase}\nSecret: ${tokenBundle.secret}\nSalt: ${tokenBundle.salt}\n`;
fs.writeFileSync(dumpFileName, dump);
};
if (argv.dictionary) {
const language = argv.dictionary;
dictionaryGenerator(language);
} else if (argv.token) {
const language = argv.token;
const tokenBundle = tokenGenerator.generate(language);
dumpPassphrase(tokenBundle);
/**
* Humans do mistakes, so we allow several attempts
* to enter the code.
*/
var attemptCounts = 0;
while (true) {
const attempt = prompt('Please enter token: ').trim();
if (tokenGenerator.verify(attempt, tokenBundle.secret, tokenBundle.salt)) {
console.log('Token has been successfully verified');
break;
}
attemptCounts++;
if (attemptCounts >= maxAttempts) {
console.log('Verification failed! Too many attempts!');
break;
}
const remainingAttempts = maxAttempts - attemptCounts;
console.log(`Incorrect token! Remaining attempts: ${remainingAttempts}.`);
}
}