esp8266-react-framework/src/SecurityManager.cpp

85 lines
2.1 KiB
C++
Raw Normal View History

2019-04-29 23:30:43 +00:00
#include <SecurityManager.h>
SecurityManager::SecurityManager(AsyncWebServer* server, FS* fs) : SettingsPersistence(fs, SECURITY_SETTINGS_FILE) {
server->on(USERS_PATH, HTTP_GET, std::bind(&SecurityManager::fetchUsers, this, std::placeholders::_1));
2019-04-29 23:30:43 +00:00
}
SecurityManager::~SecurityManager() {}
void SecurityManager::readFromJsonObject(JsonObject& root) {
// secret
2019-04-29 23:30:43 +00:00
_jwtSecret = root["jwt_secret"] | DEFAULT_JWT_SECRET;
// roles
_roles.clear();
if (root["roles"].is<JsonArray>()) {
JsonArray roles = root["roles"];
for (JsonVariant role : roles) {
_roles.push_back(role.as<String>());
}
2019-04-29 23:30:43 +00:00
}
// users
_users.clear();
2019-04-29 23:30:43 +00:00
if (root["users"].is<JsonArray>()) {
JsonArray users = root["users"];
for (JsonVariant user : users) {
String username = user["username"];
2019-04-29 23:30:43 +00:00
String password = user["password"];
String role = user["role"];
_users.push_back(User(username, password, role));
2019-04-29 23:30:43 +00:00
}
}
}
void SecurityManager::writeToJsonObject(JsonObject& root) {
// secret
root["jwt_secret"] = _jwtSecret;
// roles
JsonArray roles = root.createNestedArray("roles");
for (String _role : _roles) {
roles.add(_role);
}
// users
JsonArray users = root.createNestedArray("users");
for (User _user : _users) {
JsonObject user = users.createNestedObject();
user["username"] = _user.getUsername();
user["password"] = _user.getPassword();
user["role"] = _user.getRole();
}
}
void SecurityManager::fetchUsers(AsyncWebServerRequest *request) {
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_USERS_SIZE);
JsonObject jsonObject = response->getRoot();
writeToJsonObject(jsonObject);
response->setLength();
request->send(response);
2019-04-29 23:30:43 +00:00
}
void SecurityManager::begin() {
readFromFS();
2019-04-29 23:30:43 +00:00
}
User SecurityManager::verifyUser(String jwt) {
// TODO
return NOT_AUTHENTICATED;
}
User SecurityManager::authenticate(String username, String password) {
for (User _user : _users) {
if (_user.getUsername() == username && _user.getPassword() == password){
return _user;
}
}
2019-04-29 23:30:43 +00:00
return NOT_AUTHENTICATED;
}
String SecurityManager::generateJWT(User user) {
2019-04-29 23:30:43 +00:00
// TODO
return "";
}