-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswordManager.cpp
More file actions
123 lines (103 loc) · 2.97 KB
/
passwordManager.cpp
File metadata and controls
123 lines (103 loc) · 2.97 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
#include "passwordManager.h"
PasswordManager::PasswordManager()
{
this->timeOffset = 0;
}
PasswordManager::PasswordManager(const long timeOffset)
{
this->timeOffset = timeOffset;
}
void PasswordManager::init(const char* pwdFileName, const char* logFileName)
{
FileIOHandler passwordHandler;
passwordHandler.init();
if((!passwordHandler.exists(pwdFileName))||(!passwordHandler.exists(logFileName)))
{
return;
}
/* read the password file and store the data into the map */
this->pwdFileName = pwdFileName;
this->logFileName = logFileName;
JsonDocument pwdJson = passwordHandler.readJson(pwdFileName);
JsonObject users = pwdJson["Users"];
for(JsonPair user:users)
{
userPwdMap[user.key().c_str()] = user.value().as<const char*>();
}
}
void PasswordManager::updatePwdFile()
{
JsonDocument pwdJson;
JsonObject users = pwdJson.createNestedObject("Users");
for(const auto& pair: userPwdMap)
{
users[pair.first.c_str()] = pair.second.c_str();
}
std::string msg;
serializeJson(pwdJson, msg);
FileIOHandler passwordHandler;
passwordHandler.init();
passwordHandler.cleanFile(pwdFileName.c_str());
passwordHandler.writeFile(pwdFileName.c_str(), msg.c_str());
}
void PasswordManager::login(const char* userName)
{
if((userName == nullptr)||(userName[0] == '\0'))
{
return;
}
int day, month, year, hour, minute, second;
NTPHandler ntpHandler(this->timeOffset);
ntpHandler.updateNTP();
day = ntpHandler.getDay();
month = ntpHandler.getMonth();
year = ntpHandler.getYear();
hour = ntpHandler.getHour();
minute = ntpHandler.getMinute();
second = ntpHandler.getSecond();
std::string timeStamp = std::to_string(day) \
+ "/" + std::to_string(month) \
+ "/" + std::to_string(year) \
+ " " + std::to_string(hour) \
+ ":" + std::to_string(minute) \
+ ":" + std::to_string(second);
std::string jsonContent = "{\"userName\": \"" + std::string(userName) + "\", \"time\": \"" + timeStamp + "\"}\n";
this->latestLoginMsg = jsonContent;
FileIOHandler logHandler;
logHandler.init();
logHandler.writeFile(logFileName.c_str(), jsonContent.c_str());
}
bool PasswordManager::verifyPassword(const char* pwd)
{
for(const auto& pair: userPwdMap)
{
if(pair.second == pwd)
{
login(pair.first.c_str());
return true;
}
}
return false;
}
bool PasswordManager::changePassword(const char* userName, const char* newPwd)
{
if(userPwdMap.find(userName) != userPwdMap.end())
{
userPwdMap[userName] = newPwd;
updatePwdFile();
return true;
}
return false;
}
std::string PasswordManager::getLatestLoginMsg()
{
return latestLoginMsg;
}
std::string PasswordManager::getPassword(const char* userName)
{
if(userPwdMap.find(userName) != userPwdMap.end())
{
return userPwdMap[userName];
}
return "";
}