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 "mqtt_client.h"
#include "esp_crt_bundle.h" #include "esp_crt_bundle.h"
#include "Preferences.h"
#include "hasp/hasp.h" #include "hasp/hasp.h"
#include "hasp_mqtt.h" #include "hasp_mqtt.h"
@ -429,8 +430,14 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
void mqttSetup() 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)); 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(); 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; if(strcmp(mqttUsername, settings[FPSTR(FP_CONFIG_USER)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_USER)] = mqttUsername; settings[FPSTR(FP_CONFIG_USER)] = mqttUsername;
if(strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true; // if(strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_PASS)] = mqttPassword; // 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); if(changed) configOutput(settings, TAG_MQTT);
return changed; return changed;
@ -636,6 +645,9 @@ bool mqttGetConfig(const JsonObject& settings)
**/ **/
bool mqttSetConfig(const JsonObject& settings) bool mqttSetConfig(const JsonObject& settings)
{ {
Preferences preferences;
preferences.begin("mqtt", false);
configOutput(settings, TAG_MQTT); configOutput(settings, TAG_MQTT);
bool changed = false; bool changed = false;
@ -680,6 +692,7 @@ bool mqttSetConfig(const JsonObject& settings)
settings[FPSTR(FP_CONFIG_PASS)].as<String>() != String(FPSTR(D_PASSWORD_MASK))) { settings[FPSTR(FP_CONFIG_PASS)].as<String>() != String(FPSTR(D_PASSWORD_MASK))) {
changed |= strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0; changed |= strcmp(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0;
strncpy(mqttPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(mqttPassword)); 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()); snprintf_P(mqttNodeTopic, sizeof(mqttNodeTopic), PSTR(MQTT_PREFIX "/%s/"), haspDevice.get_hostname());

View File

@ -23,6 +23,7 @@
#endif #endif
#include <WiFi.h> #include <WiFi.h>
#include "Preferences.h"
#elif defined(ARDUINO_ARCH_ESP8266) #elif defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include "user_interface.h" // Wifi Reasons #include "user_interface.h" // Wifi Reasons
@ -482,6 +483,13 @@ void wifiSetup()
disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected); disconnectedEventHandler = WiFi.onStationModeDisconnected(wifiSTADisconnected);
#elif defined(ARDUINO_ARCH_ESP32) #elif defined(ARDUINO_ARCH_ESP32)
WiFi.onEvent(wifi_callback); 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 #endif
wifiReconnect(); 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; if(strcmp(wifiSsid, settings[FPSTR(FP_CONFIG_SSID)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_SSID)] = wifiSsid; settings[FPSTR(FP_CONFIG_SSID)] = wifiSsid;
if(strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true; // if(strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)].as<String>().c_str()) != 0) changed = true;
settings[FPSTR(FP_CONFIG_PASS)] = wifiPassword; // 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); if(changed) configOutput(settings, TAG_WIFI);
return changed; return changed;
@ -655,6 +665,9 @@ bool wifiGetConfig(const JsonObject& settings)
**/ **/
bool wifiSetConfig(const JsonObject& settings) bool wifiSetConfig(const JsonObject& settings)
{ {
Preferences preferences;
preferences.begin("wifi", false);
configOutput(settings, TAG_WIFI); configOutput(settings, TAG_WIFI);
bool changed = false; bool changed = false;
@ -667,6 +680,7 @@ bool wifiSetConfig(const JsonObject& settings)
settings[FPSTR(FP_CONFIG_PASS)].as<String>() != String(FPSTR(D_PASSWORD_MASK))) { settings[FPSTR(FP_CONFIG_PASS)].as<String>() != String(FPSTR(D_PASSWORD_MASK))) {
changed |= strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0; changed |= strcmp(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)]) != 0;
strncpy(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(wifiPassword)); strncpy(wifiPassword, settings[FPSTR(FP_CONFIG_PASS)], sizeof(wifiPassword));
nvsUpdateString(preferences, FP_CONFIG_PASS, settings[FPSTR(FP_CONFIG_PASS)]);
} }
return changed; return changed;

View File

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

View File

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