-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
90 lines (86 loc) · 2.74 KB
/
cli.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require("dotenv").config();
const {
askAccessQuestions,
askChoice,
askPasswordRequests,
askNewPassword,
askForMasterPassword,
CHOICE_GET,
CHOICE_SET,
CHOICE_DELETE,
} = require("./lib/questions");
const {
readPassword,
writePassword,
deletePassword,
readMasterPassword,
writeMasterPassword,
} = require("./lib/passwords");
const {
encrypt,
decrypt,
bcryptHash,
bcryptHashCompare,
} = require("./lib/crypto");
const { MongoClient } = require("mongodb");
const client = new MongoClient(process.env.MONGO_URL);
async function main() {
try {
await client.connect();
const database = client.db(process.env.MONGO_DB);
const masterMasterPassword = await readMasterPassword();
if (!masterMasterPassword) {
const { newMasterPassword } = await askForMasterPassword();
const masterMasterPassword = await bcryptHash(newMasterPassword, 10);
await writeMasterPassword(masterMasterPassword);
console.log("MasterPassword set");
return;
}
const { masterPassword, username } = await askAccessQuestions();
const isPasswordCorrect = await bcryptHashCompare(
masterPassword,
masterMasterPassword
);
if (isPasswordCorrect && username === "Slawo") {
console.log("Welcome");
const { option } = await askChoice();
if (option === CHOICE_GET) {
console.log("Ok, buddy!");
const { key } = await askPasswordRequests();
try {
const password = await readPassword(key, database);
const decryptedPassword = decrypt(password, masterPassword);
console.log(
`Hi ${username}, your needed password for ${key} is: ${decryptedPassword}!`
);
} catch (error) {
console.error("Something went wrong 😑", error);
}
} else if (option === CHOICE_SET) {
console.log("Let's go my friend!");
try {
const { title, password } = await askNewPassword();
const encryptedPassword = encrypt(password, masterPassword);
await writePassword(title, encryptedPassword, database);
console.log(
`You set up a password for ${title}. The new password is: ${password}`
);
} catch (error) {
console.error("Something went wrong 😑");
}
} else if (option === CHOICE_DELETE) {
console.log("Ok, bro. Let's delete some stuff!");
const { key } = await askPasswordRequests();
try {
await deletePassword(key, database);
console.log(`Your password is deleted!`);
} catch (error) {
console.error("Something went wrong 😑", error);
}
}
} else console.log("Your password or unsername is wrong");
} finally {
await client.close();
}
}
main();