-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
309 lines (281 loc) · 7.5 KB
/
index.js
File metadata and controls
309 lines (281 loc) · 7.5 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import readline from "readline";
import fs from "fs";
import chalk from "chalk";
import ora from "ora";
const dataFilePath = "./passwords.json";
const masterKey = "your_master_password";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const symbols = {
success: chalk.green("✔"),
error: chalk.red("✘"),
warning: chalk.yellow("▶️"),
};
const { success, error, warning } = symbols;
const loadData = () => {
try {
if (!fs.existsSync(dataFilePath)) {
console.log(`${chalk.red(error + " Data file does not exist. Creating new file...")}`);
saveData([]);
return [];
}
return JSON.parse(fs.readFileSync(dataFilePath));
} catch (error) {
console.error(`${chalk.red(error + " Error loading data from file:")}`);
console.error(error);
return [];
}
};
const saveData = (data) => {
try {
fs.writeFileSync(dataFilePath, JSON.stringify(data));
} catch (error) {
console.error(`${chalk.red(error + " Error saving data to file:")}`);
console.error(error);
}
};
const addPassword = (website, usernameOrEmail, password) => {
const data = loadData();
if (website === undefined || usernameOrEmail === undefined || password === undefined) {
console.error(
`${chalk.red(error + " Missing arguments.")} ${chalk.grey(
"add <website> <username/email> <password>",
)}`,
);
showMenu();
return;
}
const isEmail = usernameOrEmail.includes("@");
data.push({
website,
[isEmail ? "email" : "username"]: usernameOrEmail,
password,
});
saveData(data);
console.log(`${chalk.green(success + " Account added successfully!")}`);
showMenu();
};
const listPasswords = () => {
const data = loadData();
if (data.length === 0) {
console.log(`${chalk.yellow(warning + " No passwords saved.")}`);
showMenu();
return;
}
console.log(chalk.white.bold("Saved Passwords:"));
data.forEach(({ website, email, username, password }, index) => {
const identifier = email ? "Email" : "Username";
const value = email || username;
console.log(
`${index + 1}. Website: ${chalk.cyan(website)}, ${identifier}: ${chalk.cyan(
value,
)}, Password: ${chalk.cyan(password)}`,
);
});
showMenu();
};
const removePassword = (index) => {
const data = loadData();
if (index < 0 || index >= data.length) {
console.error(`${chalk.red(error + " Invalid index.")}`);
showMenu();
return;
}
const { website, username, email, password } = data[index];
rl.question(
`${chalk.yellow(
warning +
` Are you sure you want to remove the password for ${chalk.cyan(
` ${website}/${email || username}:${"*".repeat(password.length)}`,
)}? `,
)} (yes/no): `,
(answer) => {
if (answer.toLowerCase() === "yes" || answer.toLowerCase() === "y") {
data.splice(index, 1);
saveData(data);
console.log(`${chalk.green(success + " Password removed successfully!")}`);
} else {
console.log(`${chalk.yellow(warning + " Operation canceled.")}`);
}
showMenu();
},
);
};
const updatePassword = (index, newWebsite, newUsername, newPassword) => {
const data = loadData();
if (
index === undefined ||
newWebsite === undefined ||
newUsername === undefined ||
newPassword === undefined
) {
console.error(
`${chalk.red(error + " Missing arguments.")} ${chalk.grey(
"update <index> <newWebsite> <newUsername/email> <newPassword>",
)}`,
);
showMenu();
return;
}
if (index < 0 || index >= data.length) {
console.error(`${chalk.red(error + " Invalid index.")}`);
showMenu();
return;
}
const entry = data[index];
rl.question(
`${chalk.yellow(
warning +
` Are you sure you want to update the password for ${chalk.cyan(
` ${entry.website}/${entry.username}:${"*".repeat(entry.password.length)}`,
)}? `,
)} (yes/no): `,
(answer) => {
if (answer.toLowerCase() === "yes" || answer.toLowerCase() === "y") {
entry.website = newWebsite;
entry.username = newUsername;
entry.password = newPassword;
saveData(data);
console.log(`${chalk.green(success + " Password updated successfully!")}`);
} else {
console.log(`${chalk.yellow(warning + " Operation canceled.")}`);
}
showMenu();
},
);
};
const purgePasswords = (masterPassword) => {
if (masterPassword === undefined) {
console.error(
`${chalk.red(error + " Missing master password.")} ${chalk.grey(
"purge <masterPassword>",
)}`,
);
showMenu();
return;
}
if (masterPassword === masterKey) {
rl.question(
`${chalk.yellow(
warning +
" Are you sure you want to purge all saved passwords? This action cannot be undone.",
)} (yes/no): `,
(answer) => {
if (answer.toLowerCase() === "yes" || answer.toLowerCase() === "y") {
saveData([]);
console.log(
`${chalk.green(success + " All saved passwords purged successfully!")}`,
);
} else {
console.log(`${chalk.yellow(warning + " Operation canceled.")}`);
}
showMenu();
},
);
} else {
console.log(
`${chalk.red(error + " Master password incorrect. Purge operation aborted.")} `,
);
showMenu();
}
};
const showMenu = () => {
console.log(chalk.cyan("\nWhat would you like to do?"));
rl.prompt("\n");
};
const showHelp = () => {
const categories = [
{
title: chalk.white.bold("Managing Passwords:"),
commands: [
{
command: "add <website> <username/email> <password>",
description: "Add a new account entry.",
},
{ command: "list[ls]", description: "List all saved accounts." },
{ command: "remove[rm] <index>", description: "Remove an account entry by index." },
{
command: "update[edit] <index> <newWebsite> <newUsername/email> <newPassword>",
description: "Update a password entry.",
},
{
command: "purge[prune] <masterPassword>",
description: "Delete all saved passwords with master password verification.",
},
],
},
{
title: chalk.white.bold("Other Commands:"),
commands: [
{ command: "help", description: "Show this help menu." },
{ command: "clear", description: "Clear the screen." },
{ command: "credits", description: "Display the authors links." },
{ command: "exit", description: "Exit the password manager." },
],
},
];
categories.forEach(({ title, commands }) => {
console.log(title);
commands.forEach(({ command, description }) => {
console.log(chalk.cyan(command), "-", description);
});
});
showMenu();
};
const processCommand = (input) => {
const [command, ...args] = input.trim().split(" ");
switch (command.toLowerCase()) {
case "add":
case "new":
addPassword(...args);
break;
case "list":
case "ls":
listPasswords();
break;
case "remove":
case "rm":
removePassword(parseInt(args[0]) - 1);
break;
case "update":
case "edit":
updatePassword(parseInt(args[0]) - 1, ...args.slice(1));
break;
case "purge":
case "prune":
purgePasswords(args[0]);
break;
case "help":
showHelp();
break;
case "clear":
case "cls":
console.clear();
main();
break;
case "exit":
console.log(chalk.cyan("Exiting..."));
rl.close();
break;
case "credits":
console.log(
chalk.cyan.bold("This was coded by meta.") +
chalk.cyan.italic("\nhttps://github.com/2cbs\nhttps://metas.codes"),
);
showMenu();
break;
default:
console.error(`${chalk.red(error + " Invalid command. Type 'help' for assistance.")}`);
showMenu();
}
};
const main = () => {
console.clear();
console.log(chalk.cyan.bold("Welcome to the Password Manager!"));
console.log(chalk.dim("Type 'help' to see available commands."));
showMenu();
};
main();
rl.on("line", processCommand);