-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuth.cpp
More file actions
251 lines (222 loc) · 8.13 KB
/
Auth.cpp
File metadata and controls
251 lines (222 loc) · 8.13 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
#include "Auth.h"
#include <fstream>
#include <ctime>
#include <sstream>
#include <iomanip>
// Simple hash function for password hashing
std::string Auth::hashPassword(const std::string& password) {
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (char c : password) {
ss << std::setw(2) << static_cast<int>(c);
}
return ss.str();
}
Auth::Auth() {
userTable = new HashTable();
isLoggedIn = false;
loadFromFile();
}
Auth::~Auth() {
saveToFile();
delete userTable;
}
bool Auth::validateUsername(const std::string& username) {
// Username must be 3-20 characters and contain only letters, numbers, and underscores
if (username.length() < 3 || username.length() > 20) return false;
for (char c : username) {
if (!isalnum(c) && c != '_') return false;
}
return true;
}
bool Auth::validatePassword(const std::string& password) {
std::cout << "\n=== Password Validation ===" << std::endl;
std::cout << "Password: " << password << std::endl;
std::cout << "Password length: " << password.length() << std::endl;
// Password must be at least 6 characters
if (password.length() < 6) {
std::cout << "❌ Password too short! Must be at least 6 characters." << std::endl;
return false;
}
std::cout << "✅ Password length is valid" << std::endl;
// Check for at least one uppercase letter
bool hasUpper = false;
for (char c : password) {
if (isupper(c)) {
hasUpper = true;
break;
}
}
if (!hasUpper) {
std::cout << "❌ Password must contain at least one uppercase letter (A-Z)" << std::endl;
return false;
}
std::cout << "✅ Password contains uppercase letter" << std::endl;
// Check for at least one lowercase letter
bool hasLower = false;
for (char c : password) {
if (islower(c)) {
hasLower = true;
break;
}
}
if (!hasLower) {
std::cout << "❌ Password must contain at least one lowercase letter (a-z)" << std::endl;
return false;
}
std::cout << "✅ Password contains lowercase letter" << std::endl;
// Check for at least one number
bool hasNumber = false;
for (char c : password) {
if (isdigit(c)) {
hasNumber = true;
break;
}
}
if (!hasNumber) {
std::cout << "❌ Password must contain at least one number (0-9)" << std::endl;
return false;
}
std::cout << "✅ Password contains number" << std::endl;
// Check for at least one special character
bool hasSpecial = false;
for (char c : password) {
if (!isalnum(c)) {
hasSpecial = true;
break;
}
}
if (!hasSpecial) {
std::cout << "❌ Password must contain at least one special character (!@#$%^&* etc.)" << std::endl;
return false;
}
std::cout << "✅ Password contains special character" << std::endl;
std::cout << "✅ Password validation successful!" << std::endl;
std::cout << "===========================\n" << std::endl;
return true;
}
bool Auth::registerUser(const std::string& username, const std::string& password) {
std::cout << "\n=== Registration Debug Info ===" << std::endl;
std::cout << "Username: " << username << std::endl;
std::cout << "Password: " << password << std::endl;
std::cout << "Password length: " << password.length() << std::endl;
std::cout << "\nStep 1: Validating username..." << std::endl;
if (!validateUsername(username)) {
std::cout << "Username validation failed!" << std::endl;
std::cout << "Username must be 3-20 characters and contain only letters, numbers, and underscores" << std::endl;
return false;
}
std::cout << "Username validation passed!" << std::endl;
std::cout << "\nStep 2: Validating password..." << std::endl;
if (!validatePassword(password)) {
std::cout << "Password validation failed!" << std::endl;
std::cout << "Password must be at least 6 characters and contain at least one number" << std::endl;
return false;
}
std::cout << "Password validation passed!" << std::endl;
std::cout << "\nStep 3: Checking if username exists..." << std::endl;
if (userTable->get(username) != nullptr) {
std::cout << "Username already exists!" << std::endl;
return false;
}
std::cout << "Username is available!" << std::endl;
std::cout << "\nStep 4: Hashing password..." << std::endl;
std::string hashedPassword = hashPassword(password);
std::cout << "Password hashed successfully!" << std::endl;
std::cout << "\nStep 5: Inserting user into hash table..." << std::endl;
userTable->insert(username, hashedPassword);
std::cout << "User inserted into hash table!" << std::endl;
std::cout << "\nStep 6: Saving to file..." << std::endl;
saveToFile();
std::cout << "Registration complete!" << std::endl;
std::cout << "===========================\n" << std::endl;
return true;
}
bool Auth::login(const std::string& username, const std::string& password) {
std::string* storedPassword = userTable->get(username);
if (storedPassword == nullptr) {
return false;
}
std::string hashedInput = hashPassword(password);
if (*storedPassword == hashedInput) {
currentUser = username;
isLoggedIn = true;
return true;
}
return false;
}
void Auth::logout() {
currentUser = "";
isLoggedIn = false;
}
bool Auth::isUserLoggedIn() const {
return isLoggedIn;
}
std::string Auth::getCurrentUser() const {
return currentUser;
}
bool Auth::updateProfile(const std::string& username, const std::string& field, const std::string& value) {
if (!isLoggedIn || currentUser != username) {
return false;
}
// Implementation for updating profile fields
return true;
}
bool Auth::deleteAccount(const std::string& username, const std::string& password) {
if (!login(username, password)) {
return false;
}
userTable->remove(username);
logout();
saveToFile();
return true;
}
void Auth::saveToFile() {
std::ofstream file("users.dat");
if (file.is_open()) {
// Save user data to file
for (int i = 0; i < userTable->TABLE_SIZE; i++) {
HashTable::Node* current = userTable->table[i];
while (current != nullptr) {
file << current->key << ":" << current->value << std::endl;
current = current->next;
}
}
file.close();
std::cout << "User data saved successfully!" << std::endl;
}
else {
std::cout << "Error: Could not open users.dat for writing!" << std::endl;
}
}
void Auth::loadFromFile() {
std::ifstream file("users.dat");
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
size_t pos = line.find(':');
if (pos != std::string::npos) {
std::string username = line.substr(0, pos);
std::string password = line.substr(pos + 1);
userTable->insert(username, password);
}
}
file.close();
std::cout << "User data loaded successfully!" << std::endl;
}
else {
std::cout << "No existing user data found. Starting with empty database." << std::endl;
}
}
// Get player's current score (based on total points)
int Auth::getPlayerScore(const std::string& username) {
// For now, score is the same as points
return getPlayerPoints(username);
}
// Implementation of getPlayerPoints method
int Auth::getPlayerPoints(const std::string& username) {
// For now, return a placeholder score based on username length
// In a real implementation, this would fetch points from user data
// Default score of 100 + username length to ensure different players have different scores
return 100 + username.length();
}