2019-05-29 22:48:16 +00:00
|
|
|
#include <SecuritySettingsService.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) :
|
|
|
|
AdminSettingsService(server, fs, this, SECURITY_SETTINGS_PATH, SECURITY_SETTINGS_FILE),
|
|
|
|
SecurityManager() {
|
|
|
|
}
|
|
|
|
SecuritySettingsService::~SecuritySettingsService() {
|
|
|
|
}
|
2019-05-29 22:48:16 +00:00
|
|
|
|
|
|
|
void SecuritySettingsService::readFromJsonObject(JsonObject& root) {
|
2019-12-03 23:16:06 +00:00
|
|
|
// secret
|
2019-05-29 22:48:16 +00:00
|
|
|
_jwtHandler.setSecret(root["jwt_secret"] | DEFAULT_JWT_SECRET);
|
|
|
|
|
|
|
|
// users
|
|
|
|
_users.clear();
|
|
|
|
if (root["users"].is<JsonArray>()) {
|
2019-12-03 23:16:06 +00:00
|
|
|
for (JsonVariant user : root["users"].as<JsonArray>()) {
|
2019-05-29 22:48:16 +00:00
|
|
|
_users.push_back(User(user["username"], user["password"], user["admin"]));
|
|
|
|
}
|
2019-12-29 17:54:12 +00:00
|
|
|
} else {
|
|
|
|
_users.push_back(User(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_USERNAME, true));
|
|
|
|
_users.push_back(User(DEFAULT_GUEST_USERNAME, DEFAULT_GUEST_USERNAME, false));
|
2019-05-29 22:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SecuritySettingsService::writeToJsonObject(JsonObject& root) {
|
|
|
|
// secret
|
|
|
|
root["jwt_secret"] = _jwtHandler.getSecret();
|
|
|
|
|
|
|
|
// users
|
|
|
|
JsonArray users = root.createNestedArray("users");
|
|
|
|
for (User _user : _users) {
|
|
|
|
JsonObject user = users.createNestedObject();
|
|
|
|
user["username"] = _user.getUsername();
|
|
|
|
user["password"] = _user.getPassword();
|
|
|
|
user["admin"] = _user.isAdmin();
|
|
|
|
}
|
|
|
|
}
|