chore: Refactor stores to use PersistableStore CRTP template (#2464)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#include "PersistableStore.h"
|
||||
|
||||
#include <HalStorage.h>
|
||||
#include <Logging.h>
|
||||
#include <ObfuscationUtils.h>
|
||||
|
||||
bool PersistableStoreBase::writeDocToFile(const char* path, const JsonDocument& doc) {
|
||||
Storage.mkdir("/.crosspoint");
|
||||
String json;
|
||||
serializeJson(doc, json);
|
||||
if (!Storage.writeFile(path, json)) {
|
||||
LOG_ERR("PERSIST", "Failed to write %s", path);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PersistableStoreBase::readDocFromFile(const char* path, JsonDocument& doc) {
|
||||
if (!Storage.exists(path)) {
|
||||
return false; // Expected on first boot — not an error.
|
||||
}
|
||||
String json = Storage.readFile(path);
|
||||
if (json.isEmpty()) {
|
||||
LOG_ERR("PERSIST", "Failed to read %s (empty)", path);
|
||||
return false;
|
||||
}
|
||||
auto error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
LOG_ERR("PERSIST", "JSON parse error in %s: %s", path, error.c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string PersistableStoreBase::extractPassword(JsonVariantConst doc, bool& needsResave) {
|
||||
bool ok = false;
|
||||
std::string pass = obfuscation::deobfuscateFromBase64(doc["password_obf"] | "", &ok);
|
||||
if (!ok) {
|
||||
// Deobfuscation failed — fall back to legacy plaintext password.
|
||||
pass = doc["password"] | "";
|
||||
if (!pass.empty()) needsResave = true;
|
||||
}
|
||||
// A successfully decoded empty string is a legitimate value; preserve as-is.
|
||||
return pass;
|
||||
}
|
||||
Reference in New Issue
Block a user