-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthentication.cpp
More file actions
164 lines (130 loc) · 4.59 KB
/
Copy pathAuthentication.cpp
File metadata and controls
164 lines (130 loc) · 4.59 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
#include "Authentication.h"
#include <sstream>
#include <iomanip>
#include <random>
#include <chrono>
// Password Hasher implementation
std::string PasswordHasher::generateSalt(size_t length) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, sizeof(alphanum) - 2);
std::string salt;
salt.reserve(length);
for (size_t i = 0; i < length; ++i) {
salt += alphanum[dis(gen)];
}
return salt;
}
std::string PasswordHasher::simpleHash(const std::string& input) {
std::hash<std::string> hasher;
auto hashValue = hasher(input);
std::stringstream ss;
ss << std::hex << std::setw(16) << std::setfill('0') << hashValue;
return ss.str();
}
std::string PasswordHasher::hashPassword(const std::string& password) {
std::string salt = generateSalt();
std::string combined = salt + password;
// Multiple iterations to strengthen the hash
std::string result = combined;
for (int i = 0; i < 1000; i++) {
result = simpleHash(result);
}
// Return salt:hash format
return salt + ":" + result;
}
bool PasswordHasher::verifyPassword(const std::string& password, const std::string& storedHash) {
size_t delimiterPos = storedHash.find(':');
if (delimiterPos == std::string::npos) {
return false;
}
std::string salt = storedHash.substr(0, delimiterPos);
std::string storedHashValue = storedHash.substr(delimiterPos + 1);
std::string combined = salt + password;
std::string result = combined;
for (int i = 0; i < 1000; i++) {
result = simpleHash(result);
}
return storedHashValue == result;
}
// Authentication Manager implementation
AuthenticationManager::AuthenticationManager() : currentUserId(0) {}
std::string AuthenticationManager::generateSessionToken() {
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto value = now_ms.time_since_epoch().count();
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 999999);
std::stringstream ss;
ss << std::hex << std::setw(16) << std::setfill('0') << value << dis(gen);
return ss.str();
}
bool AuthenticationManager::registerUser(const std::string& username, const std::string& password) {
// Check if username already exists
User existingUser = storage.getUserByUsername(username);
if (existingUser.getId() != 0) {
return false; // User already exists
}
// Hash password
std::string hashedPassword = PasswordHasher::hashPassword(password);
// Create new user
int userId = storage.getNextUserId();
User newUser(userId, username, hashedPassword);
// Save user
return storage.saveUser(newUser);
}
bool AuthenticationManager::login(const std::string& username, const std::string& password) {
User user = storage.getUserByUsername(username);
if (user.getId() == 0) {
return false; // User not found
}
if (user.isLocked()) {
return false; // Account locked
}
if (!PasswordHasher::verifyPassword(password, user.getPasswordHash())) {
// Update failed attempts count
if (!user.attemptLogin("")) { // Pass empty string to ensure it fails but updates counter
storage.saveUser(user);
}
return false; // Incorrect password
}
// Create session
currentUserId = user.getId();
currentSessionToken = generateSessionToken();
Session session(currentUserId, currentSessionToken);
storage.saveSession(session);
return true;
}
bool AuthenticationManager::logout() {
if (!isLoggedIn()) {
return false;
}
bool result = storage.deleteSession(currentSessionToken);
currentSessionToken = "";
currentUserId = 0;
return result;
}
bool AuthenticationManager::isLoggedIn() const {
return !currentSessionToken.empty() && currentUserId > 0;
}
std::string AuthenticationManager::getCurrentSessionToken() const {
return currentSessionToken;
}
int AuthenticationManager::getCurrentUserId() const {
return currentUserId;
}
bool AuthenticationManager::validateSession(const std::string& token) {
Session session = storage.getSessionByToken(token);
if (session.getUserId() == 0 || !session.isValid()) {
return false;
}
// Renew session
session.renew();
storage.saveSession(session);
return true;
}