-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuth.cpp
More file actions
233 lines (199 loc) · 7.54 KB
/
Auth.cpp
File metadata and controls
233 lines (199 loc) · 7.54 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
#include "../include/Auth.h"
#include "../include/User.h"
#include "../include/constants.h"
#include "../include/Roles.h"
#include "../include/Utils.h"
#include "../include/AuditLogger.h"
#include <cstdlib>
#include <ctime>
string customHash(string input) {
unsigned long long hash = 5381;
int c;
for (int i = 0; i < input.length(); ++i) {
c = static_cast<char>(input[i]);
hash = (hash * 33) + c;
}
return to_string(hash);
}
User* login(string username, string password) {
ifstream usersFileHandle("data/users.txt");
AuditLogger auditLogger;
if (!usersFileHandle.is_open()) {
cout << RED << "[-] Error: Could not open users.txt" << RESET << endl;
return nullptr;
}
string fileUsername, filePassword, fileRole;
string userInputtedPasswordHash = customHash(password);
string line;
while (getline(usersFileHandle, line)) {
string* fields = split(line);
fileUsername = fields[0];
filePassword = fields[1];
fileRole = fields[2];
if (fileUsername == username && filePassword == userInputtedPasswordHash) {
srand((unsigned int)time(0));
int otp = 100000 + rand() % 900000;
time_t otpGenTime = time(0);
int userOtp;
cout << YELLOW << endl << "[OTP] Your one-time password is: " << otp << RESET << endl << endl;
userOtp = getErrFreeInt("Enter OTP: ");
time_t otpCheckTime = time(0);
if (otpCheckTime - otpGenTime > 60) {
cout << endl << RED << "[-] OTP expired. Please try logging in again." << RESET << endl;
auditLogger.log(username, "LOGIN", "OTP expired", "FAIL");
delete[] fields;
usersFileHandle.close();
return nullptr;
}
if (userOtp != otp) {
cout << RED << "[-] Invalid OTP." << RESET << endl;
auditLogger.log(username, "LOGIN", "Invalid OTP", "FAIL");
delete[] fields;
usersFileHandle.close();
return nullptr;
}
string roleStr = fileRole;
string status = "SUCCESS";
auditLogger.log(username, "LOGIN", "Role: " + trim(roleStr), status);
if (stoi(fileRole) == ROLE_JUNIOR){
usersFileHandle.close();
delete[] fields;
return new Junior(fileUsername);
}
else if (stoi(fileRole) == ROLE_EMPLOYEE){
usersFileHandle.close();
delete[] fields;
return new Employee(fileUsername);
}
else if (stoi(fileRole) == ROLE_MANAGER){
usersFileHandle.close();
delete[] fields;
return new Manager(fileUsername);
}
else if (stoi(fileRole) == ROLE_DIRECTOR){
usersFileHandle.close();
delete[] fields;
return new Director(fileUsername);
}
else if (stoi(fileRole) == ROLE_EXECUTIVE){
usersFileHandle.close();
delete[] fields;
return new Executive(fileUsername);
}
}
delete[] fields;
}
auditLogger.log(username, "LOGIN", "Invalid credentials", "FAIL");
usersFileHandle.close();
return nullptr;
}
User* registerUser(string username, string password, int role, string favoriteMovie) {
ifstream usersFileHandle("data/users.txt");
AuditLogger auditLogger;
if (!usersFileHandle.is_open()) {
cout << RED << "[-] Error: Could not open users.txt" << RESET << endl;
return nullptr;
}
string line;
string userInputtedPasswordHash = customHash(password);
string hashedFavoriteMovie = customHash(favoriteMovie);
while (getline(usersFileHandle, line)) {
string* fields = split(line);
string fileUsername = fields[0];
if (fileUsername == username) {
cout << RED << "[-] Error: User already exists" << RESET << endl;
delete[] fields;
usersFileHandle.close();
auditLogger.log(username, "REGISTER", "User already exists", "FAIL");
return nullptr;
}
delete[] fields;
}
usersFileHandle.close();
ofstream usersFile("data/users.txt", ios::app);
if (!usersFile.is_open()) {
cout << RED << "[-] Error: Could not open users.txt" << RESET << endl;
return nullptr;
}
usersFile << username + "|" + userInputtedPasswordHash + "|" + to_string(role) + "|" + hashedFavoriteMovie << endl;
usersFile.close();
auditLogger.log(username, "REGISTER", "Role: " + to_string(role), "SUCCESS");
if (role == ROLE_JUNIOR)
return new Junior(username);
else if (role == ROLE_EMPLOYEE)
return new Employee(username);
else if (role == ROLE_MANAGER)
return new Manager(username);
else if (role == ROLE_DIRECTOR)
return new Director(username);
else if (role == ROLE_EXECUTIVE)
return new Executive(username);
return nullptr;
}
bool verifySecurityQuestion(string username, string favoriteMovie) {
ifstream usersFileHandle("data/users.txt");
AuditLogger auditLogger;
if (!usersFileHandle.is_open()) {
cout << RED << "[-] Error: Could not open users.txt" << RESET << endl;
return false;
}
string line;
string hashedFavoriteMovie = customHash(favoriteMovie);
while (getline(usersFileHandle, line)) {
string* fields = split(line);
if (fields[0] == username) {
bool verified = (fields[3] == hashedFavoriteMovie);
delete[] fields;
usersFileHandle.close();
auditLogger.log(username, "SECURITY_VERIFY", verified ? "Success" : "Failed", verified ? "SUCCESS" : "FAIL");
return verified;
}
delete[] fields;
}
usersFileHandle.close();
auditLogger.log(username, "SECURITY_VERIFY", "User not found", "FAIL");
return false;
}
bool resetPassword(string username, string newPassword) {
ifstream usersFileHandle("data/users.txt");
AuditLogger auditLogger;
ofstream tempFile("data/temp_users.txt");
bool found = false;
if (!usersFileHandle.is_open() || !tempFile.is_open()) {
cout << RED << "[-] Error: Could not open users file" << RESET << endl;
return false;
}
string line;
string newHashedPassword = customHash(newPassword);
while (getline(usersFileHandle, line)) {
string* fields = split(line);
if (fields[0] == username) {
tempFile << fields[0] + "|" + newHashedPassword + "|" + fields[2] + "|" + fields[3] << endl;
found = true;
} else {
tempFile << line << endl;
}
delete[] fields;
}
usersFileHandle.close();
tempFile.close();
if (!found) {
remove("data/temp_users.txt");
auditLogger.log(username, "PASSWORD_RESET", "User not found", "FAIL");
return false;
}
remove("data/users.txt");
rename("data/temp_users.txt", "data/users.txt");
auditLogger.log(username, "PASSWORD_RESET", "Password changed", "SUCCESS");
return true;
}
void logout(User* user) {
AuditLogger auditLogger;
if (user) {
auditLogger.log(user->getUsername(), "LOGOUT", "", "SUCCESS");
cout << GREEN << "[+] Successfully logged out." << RESET << endl;
cout << endl << "Returning to main menu..." << endl << endl << endl << endl;
} else {
cout << RED << "[-] Error: No user is currently logged in." << RESET << endl;
}
}