Store passwords in NVS instead of config.json

This commit is contained in:
fvanroie 2022-12-26 23:08:07 +01:00
parent da2c975d39
commit ca6a59ae2c
4 changed files with 36 additions and 8 deletions

View File

@ -8,6 +8,7 @@
#include "mqtt_client.h"
#include "esp_crt_bundle.h"
#include "Preferences.h"
#include "hasp/hasp.h"
#include "hasp_mqtt.h"
@ -429,8 +430,14 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
void mqttSetup()
{
Preferences preferences;
preferences.begin("mqtt", true);
String password = preferences.getString(FP_CONFIG_PASS, MQTT_PASSWORD);
strncpy(mqttPassword, password.c_str(), sizeof(mqttPassword));
LOG_DEBUG(TAG_MQTT, F(D_BULLET "Read %s => %s (%d bytes)"), FP_CONFIG_PASS, password.c_str(), password.length());
queue = xQueueCreate(64, sizeof(mqtt_message_t));
esp_crt_bundle_set(rootca_crt_bundle_start);
arduino_esp_crt_bundle_set(rootca_crt_bundle_start);
mqttStart();
}
@ -619,8 +626,10 @@ bool mqttGetConfig(const JsonObject& settings)
if(strcmp(mqttUsername, settings[FPSTR(FP_CONFIG_USER)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_USER)] = mqttUsername;
if(strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_PASS)] = mqttPassword;
// if(strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
// settings[FPSTR(FP_CONFIG_PASS)] = mqttPassword;
if(strcmp(D_PASSWORD_MASK, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_PASS)] = D_PASSWORD_MASK;
if(changed) configOutput(settings, TAG_MQTT);
return changed;
@ -636,6 +645,9 @@ bool mqttGetConfig(const JsonObject& settings)
**/
bool mqttSetConfig(const JsonObject& settings)
{
Preferences preferences;
preferences.begin("mqtt", false);
configOutput(settings, TAG_MQTT);
bool changed = false;
@ -680,6 +692,7 @@ bool mqttSetConfig(const JsonObject& settings)
settings[FPSTR(FP_CONFIG_PASS)].as<String>() != String(FPSTR(D_PASSWORD_MASK))) {
changed |= strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0;
strncpy(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(mqttPassword));
nvsUpdateString(preferences, FP_CONFIG_PASS, settings[FPSTR(FP_CONFIG_PASS)]);
}
snprintf_P(mqttNodeTopic, sizeof(mqttNodeTopic), PSTR(MQTT_PREFIX "/%s/"), haspDevice.get_hostname());

View File

@ -23,6 +23,7 @@
#endif
#include <WiFi.h>
#include "Preferences.h"
#elif defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include "user_interface.h" // Wifi Reasons
@ -36,7 +37,7 @@ static WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
SPIClass espSPI(ESPSPI_MOSI, ESPSPI_MISO, ESPSPI_SCLK); // SPI port where esp is connected
#endif
//#include "DNSserver.h"
// #include "DNSserver.h"
char wifiSsid[MAX_SSID_LEN] = WIFI_SSID;
char wifiPassword[MAX_PASSPHRASE_LEN] = WIFI_PASSWORD;
@ -482,6 +483,13 @@ void wifiSetup()
disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected);
#elif defined(ARDUINO_ARCH_ESP32)
WiFi.onEvent(wifi_callback);
Preferences preferences;
preferences.begin("wifi", true);
String password = preferences.getString(FP_CONFIG_PASS, WIFI_PASSWORD);
strncpy(wifiPassword, password.c_str(), sizeof(wifiPassword));
LOG_DEBUG(TAG_WIFI, F(D_BULLET "Read %s => %s (%d bytes)"), FP_CONFIG_PASS, password.c_str(),
password.length());
#endif
wifiReconnect();
@ -638,8 +646,10 @@ bool wifiGetConfig(const JsonObject& settings)
if(strcmp(wifiSsid, settings[FPSTR(FP_CONFIG_SSID)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_SSID)] = wifiSsid;
if(strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_PASS)] = wifiPassword;
// if(strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
// settings[FPSTR(FP_CONFIG_PASS)] = wifiPassword;
if(strcmp(D_PASSWORD_MASK, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_PASS)] = D_PASSWORD_MASK;
if(changed) configOutput(settings, TAG_WIFI);
return changed;
@ -655,6 +665,9 @@ bool wifiGetConfig(const JsonObject& settings)
**/
bool wifiSetConfig(const JsonObject& settings)
{
Preferences preferences;
preferences.begin("wifi", false);
configOutput(settings, TAG_WIFI);
bool changed = false;
@ -667,6 +680,7 @@ bool wifiSetConfig(const JsonObject& settings)
settings[FPSTR(FP_CONFIG_PASS)].as<String>() != String(FPSTR(D_PASSWORD_MASK))) {
changed |= strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0;
strncpy(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(wifiPassword));
nvsUpdateString(preferences, FP_CONFIG_PASS, settings[FPSTR(FP_CONFIG_PASS)]);
}
return changed;

View File

@ -36,9 +36,9 @@ bool wifiSetConfig(const JsonObject& settings);
#ifndef WIFI_PASSWORD
#ifndef WIFI_PASSW
#define WIFI_PASSWORD "";
#define WIFI_PASSWORD ""
#else
#define WIFI_PASSWORD WIFI_PASSW;
#define WIFI_PASSWORD WIFI_PASSW
#endif
#endif

View File

@ -8,6 +8,7 @@
#if defined(ARDUINO_ARCH_ESP32)
#include "Update.h"
#include "Preferences.h"
#include "sdkconfig.h" // for CONFIG_IDF_TARGET_ESP32* defines
#include <uri/UriBraces.h>
#include <uri/UriRegex.h>