-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
239 lines (209 loc) · 11.1 KB
/
Program.cs
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
#pragma warning disable
using CsvHelper.Configuration;
using CsvHelper;
using PasswordManager.Classes;
using PasswordManager.Models;
using System.Globalization;
using System.Threading;
namespace PasswordManager
{
class Program
{
static void Main(string[] args)
{
string userName;
string email;
string firstName;
string lastName;
bool loggedIn = false;
string authUser = "";
string configFile = "config.txt";
string workDir = File.Exists(configFile) ? File.ReadAllText(configFile) : "..\\..\\PasswordManager\\database\\";
FileHandler fileHandler = new FileHandler(workDir);
EncryptedType encryptedType = new EncryptedType();
Utils utils = new Utils();
if (args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "--workdir":
if (i + 1 < args.Length && !args[i + 1].StartsWith("--"))
{
workDir = args[i + 1];
fileHandler.SetWorkDir(workDir, configFile);
i++;
}
else
{
Console.WriteLine("\nError: Missing or invalid path after --workdir.\n");
return;
}
break;
case "--register":
if (i + 1 < args.Length)
{
utils.badArgument("--register");
return;
}
else
{
Console.WriteLine("\n*******************### HELLO USER ! ###*******************\nTo create an account, please fill out the fields below.\n");
firstName = utils.GetUserInput(" First Name");
lastName = utils.GetUserInput(" Last Name");
userName = utils.GetUserInput(" Username");
email = utils.GetUserInput(" Email");
if (fileHandler.emailPresent(email) == true) break;
string pwd = utils.GetPasswordInput("Master Password");
string confirmPassword = utils.GetPasswordInput("Repeat Password");
if (pwd != confirmPassword)
{
Console.WriteLine("\n\nThe entered passwords do not match!\n");
break;
}
var user = new User
{
UserName = userName,
Email = email,
PassWord = encryptedType.Encrypt(pwd, userName),
FirstName = firstName,
LastName = lastName
};
fileHandler.FileWrite(user);
break;
}
case "--login":
if (i + 1 < args.Length)
{
utils.badArgument("--login");
return;
}
Console.WriteLine("\nPlease enter your master credentials to access your vault:\n");
while (true)
{
userName = utils.GetUserInput("Username");
string password = utils.GetPasswordInput(" Password");
string userCsvPath = Path.Combine(workDir, "user.csv");
string storedPassword = utils.GetStoredPassword(userName, userCsvPath);
if (utils.ValidateUser(storedPassword, password, userName))
{
loggedIn = true; // user logged in
authUser = userName;
Console.WriteLine("\n ~ ~ ~ ~ ~ ~ Successful Authentication! ~ ~ ~ ~ ~ ~\n");
Thread.Sleep(500);
Console.WriteLine(" ╔═══════════════════════════════╗");
Thread.Sleep(100);
Console.WriteLine(" ║ WELCOME BACK ! ║");
Thread.Sleep(100);
Console.WriteLine(" ╚═══════════════════════════════╝\n\nPossible commands: 'add', 'list', 'delete'. Use 'exit' to log out:");
break;
}
else
{
Console.WriteLine("\n\nInvalid username or password! Please try again:\n");
// Allow the user to try again; continue to the next iteration of the loop
}
}
break;
default:
Console.WriteLine($"\nUnknown command-line argument: '{args[i]}'\n");
break;
}
}
}
else
{
Console.WriteLine("\n No arguments given!\n");
}
while (loggedIn)
{
Console.Write(">> ");
string input = Console.ReadLine();
if (input!.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
loggedIn = false; // Log out and exit the loop
authUser = "";
}
else
{
switch (input)
{
case "list":
var currentOrdering = "";
var secrets = fileHandler.GetSecretsForUser(authUser);
if (secrets.Count == 0)
{
Console.WriteLine("\nYou have no secrets stored!");
break;
}
// Prompt the user to choose the ordering
Console.WriteLine("\nSelect ordering: (1) Website | (2) Username | (3) Password");
Console.Write("\nEnter the number: ");
int orderChoice;
if (int.TryParse(Console.ReadLine(), out orderChoice) && (orderChoice >= 1 && orderChoice <= 3) )
{
switch (orderChoice)
{
case 1:
currentOrdering = "Website";
secrets = secrets.OrderBy(s => s.WebSite, StringComparer.OrdinalIgnoreCase).ToList();
break;
case 2:
currentOrdering = "Username";
secrets = secrets.OrderBy(s => s.UserName, StringComparer.OrdinalIgnoreCase).ToList();
break;
case 3:
currentOrdering = "Password";
// Decrypt the passwords and then sort based on the decrypted passwords
secrets = secrets.OrderBy(s => encryptedType.Decrypt(s.PassWord, authUser), StringComparer.OrdinalIgnoreCase).ToList();
break;
}
}
else
{
Console.WriteLine("\nInvalid selection!\n");
break;
}
// Display the sorted secrets
Console.WriteLine("\nStored Websites and Passwords (Ordered by " + currentOrdering + "):\n" +
$"----------------------------------------------------------------------");
foreach (var secret in secrets)
{
// Decrypt the password and print the website, username, and decrypted password
string decryptedPassword = encryptedType.Decrypt(secret.PassWord, authUser);
Console.WriteLine($"| Website: '{secret.WebSite}' | Username: '{secret.UserName}' | Password: '{decryptedPassword}'\n" +
$"----------------------------------------------------------------------");
}
break;
case "add":
Console.WriteLine("\nTo save a new secret, please fill out the required fields below!");
while (true)
{
string website = utils.GetUserInput(" Website Address");
if (fileHandler.websitePresent(website) == true) continue;
string webUserName = utils.GetUserInput("Website Username");
string webPwd = utils.GetPasswordInput(" Website Password");
var vault = new Vault
{
UserId = authUser,
UserName = webUserName,
WebSite = website,
PassWord = encryptedType.Encrypt(webPwd, authUser)
};
fileHandler.FileWrite(vault);
break;
}
break;
case "delete":
fileHandler.DeleteSecret(authUser);
break;
default:
Console.WriteLine("\nUnknown command: " + input+"\n");
break;
}
}
}
}
}
}