Skip to content

Commit 6bb0019

Browse files
committed
Update
Updated to use ArduinoJson 6 Fixed bug where having debug mode without Serial configured would cause crash (to be tested more) Settings now load properly (?)
1 parent 52516a7 commit 6bb0019

File tree

4 files changed

+59
-36
lines changed

4 files changed

+59
-36
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ It's meant to be an universal, secure toolbox.
3030
* Cookie auth works best with Firefox
3131
* encryptKey takes values >=1 because of how key verification works
3232
* Using a function without configuring it (or loading settings) may cause crash
33-
* Requires ArduinoJson v5
3433

3534
### ToDo list
36-
1. Update to ArduinoJson v6
37-
2. Encrypt config file
38-
3. Change key structure to [id]=(...) to make cookie auth working on all browsers
39-
4. Rewrite token verification function to consume less memory
35+
For ToDo list check issues.

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=settingsManager
2-
version=1.0.1
2+
version=1.0.2
33
author=Marek Ledworowski (marecl)
44
maintainer=Marek Ledworowski (marecl)
55
sentence=Simple settings management for ESP8266

src/settingsManager.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ settingsManager::settingsManager(const char* _f) {
1010
this->_pwd = new char[16];
1111
this->_ntp = new char[32];
1212
this->_file = new char[32];
13+
#ifdef DEBUG_INSECURE
14+
this->_debug = NULL;
15+
#endif
1316
if (_f == NULL) memset(this->_file, 0, 32);
1417
else this->setField(this->_file, _f, 32);
1518
this->_dhcp = true;
@@ -18,12 +21,8 @@ settingsManager::settingsManager(const char* _f) {
1821
this->timezone = 0;
1922
this->readInterval = 5;
2023
this->tokenLifespan = 600;
21-
#ifdef DEBUG_INSECURE
22-
this->_debug = NULL;
23-
#endif
2424
}
2525

26-
2726
settingsManager::~settingsManager() {
2827
delete[] _name;
2928
delete[] _ssid;
@@ -48,8 +47,7 @@ void settingsManager::ntpServer(const char* _ntp) {
4847
}
4948

5049
void settingsManager::save() {
51-
if (this->_file[0] == 0) return;
52-
SPIFFS.remove(this->_file);
50+
if (this->_file == NULL) return;
5351
fs::File _toSave = SPIFFS.open(this->_file, "w");
5452
_toSave.print(F("{\"SN\":\""));
5553
_toSave.print(this->_name);
@@ -140,37 +138,40 @@ bool settingsManager::load() {
140138
this->_print(F("Found file"));
141139
#endif
142140
fs::File _toSave = SPIFFS.open(this->_file, "r");
143-
//Longest config i could create
144-
StaticJsonBuffer<800> _ld;
145-
JsonObject& _data = _ld.parseObject(_toSave);
146-
_toSave.close();
147-
if (!_data.success()) {
141+
//(about the) Longest config i could create
142+
StaticJsonDocument<800> _ld;
143+
144+
DeserializationError err = deserializeJson(_ld, _toSave);
145+
146+
if (err || !_toSave) {
148147
#ifdef DEBUG_INSECURE
149148
this->_print(F("Load error"));
150149
#endif
150+
_toSave.close();
151151
return false;
152152
}
153153

154-
this->setField(this->_name, _data["SN"], 32);
155-
this->setField(this->_user, _data["SL"], 16);
156-
this->setField(this->_pwd, _data["SPL"], 16);
157-
this->setField(this->_ssid, _data["OS"], 32);
158-
this->setField(this->_pass, _data["OP"], 32);
159-
this->setField(this->_ssidap, _data["SS"], 32);
160-
this->setField(this->_passap, _data["SPA"], 32);
161-
this->_ip = stringToIP(_data["OI"]);
162-
this->_gw = stringToIP(_data["OG"]);
163-
this->_mask = stringToIP(_data["OM"]);
164-
this->_dhcp = _data["OD"].as<bool>();
165-
this->useNTP = _data["UN"].as<bool>();
166-
this->setField(this->_ntp, _data["NS"], 32);
167-
this->lastUpdate = _data["LU"].as<uint32_t>();
168-
this->timezone = _data["TZ"].as<int8_t>();
169-
this->readInterval = _data["RI"].as<uint16_t>();
170-
this->tokenLifespan = _data["TL"].as<uint16_t>();
154+
this->setField(this->_name, _ld["SN"], 32);
155+
this->setField(this->_user, _ld["SL"], 16);
156+
this->setField(this->_pwd, _ld["SPL"], 16);
157+
this->setField(this->_ssid, _ld["OS"], 32);
158+
this->setField(this->_pass, _ld["OP"], 32);
159+
this->setField(this->_ssidap, _ld["SS"], 32);
160+
this->setField(this->_passap, _ld["SPA"], 32);
161+
this->_ip = stringToIP(_ld["OI"]);
162+
this->_gw = stringToIP(_ld["OG"]);
163+
this->_mask = stringToIP(_ld["OM"]);
164+
this->_dhcp = _ld["OD"];
165+
this->useNTP = _ld["UN"];
166+
this->setField(this->_ntp, _ld["NS"], 32);
167+
this->lastUpdate = _ld["LU"];
168+
this->timezone = _ld["TZ"];
169+
this->readInterval = _ld["RI"];
170+
this->tokenLifespan = _ld["TL"];
171171
#ifdef DEBUG_INSECURE
172172
this->_print(F("Loaded successfully"));
173173
#endif
174+
_toSave.close();
174175
return true;
175176
}
176177

@@ -224,9 +225,9 @@ void settingsManager::printConfig() {
224225
this->_debug->print(this->lastUpdate);
225226
this->_debug->print(F(",\"TZ\":"));
226227
this->_debug->print(this->timezone);
227-
this->_debug->print(F("\",\"RI\":"));
228+
this->_debug->print(F(",\"RI\":"));
228229
this->_debug->print(this->readInterval);
229-
this->_debug->print(F("\",\"TL\":"));
230+
this->_debug->print(F(",\"TL\":"));
230231
this->_debug->print(this->tokenLifespan);
231232
this->_debug->print(F("}\r\n"));
232233
return void();

src/settingsManager.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,45 @@
2323

2424
class settingsManager {
2525
public:
26+
/* File name to store settings */
2627
settingsManager(const char* = NULL);
2728
~settingsManager();
2829
void save();
2930
bool load();
31+
32+
/* SSID, password */
3033
void configSTA(const char*, const char* = NULL);
3134
void configAP(const char*, const char* = NULL);
35+
36+
/* SSID */
3237
void ssid(const char*);
3338
void ssidAP(const char*);
39+
40+
/* IP addresses */
3441
void configIP(IPAddress, IPAddress, IPAddress);
3542
void configIP(const char*, const char*, const char*);
3643
void useDHCP(bool);
3744
bool useDHCP();
3845
void name(const char*);
46+
/* Username, password */
3947
void configUser(const char*, const char*);
4048
bool authenticate(const char*, const char*);
49+
4150
bool beginSTA();
4251
bool beginAP();
52+
53+
/* Direct NTP address. Pools are not tested */
4354
void ntpServer(const char*);
55+
56+
/* Server, update service, path */
4457
void configUpdateServer(ESP8266WebServer*, ESP8266HTTPUpdateServer*, const char*);
58+
4559
void beginOTA(uint16_t = 8266);
4660
bool webAuthenticate(ESP8266WebServer*);
61+
62+
/* Username, password, file to remove from SPIFFS */
4763
bool remove(const char*, const char*, const char*);
64+
4865
IPAddress localIP();
4966
IPAddress gatewayIP();
5067
IPAddress subnetMask();
@@ -57,9 +74,17 @@ class settingsManager {
5774
uint32_t lastUpdate;
5875
int8_t timezone;
5976
uint16_t readInterval;
77+
78+
/* Decrypted key, canary */
6079
uint8_t verifyKey(String, uint32_t);
80+
81+
/* Encrypted key, canary */
6182
uint8_t verifyEncryptedKey(String, uint32_t);
83+
84+
/* Canary */
6285
String encryptKey(uint32_t);
86+
87+
/* Encrypted key */
6388
String decryptKey(String);
6489
uint32_t tokenLifespan;
6590

@@ -88,6 +113,7 @@ class settingsManager {
88113
}
89114

90115
#ifdef DEBUG_INSECURE
116+
/* Pointer to Serial */
91117
void serialDebug(HardwareSerial*);
92118
void printConfigFile();
93119
void printConfig();

0 commit comments

Comments
 (0)