From 8c8f524e6d90f5c6490bfa12717682689cd1c07f Mon Sep 17 00:00:00 2001 From: technyon Date: Tue, 23 Jan 2024 15:46:58 +0100 Subject: [PATCH] apply credentials to wifimanager configuration portal --- lib/WiFiManager/WiFiManager.cpp | 75 ++++++++++++++++++++++++++++++++- lib/WiFiManager/WiFiManager.h | 5 +++ networkDevices/WifiDevice.cpp | 17 ++++---- networkDevices/WifiDevice.h | 2 +- 4 files changed, 89 insertions(+), 10 deletions(-) diff --git a/lib/WiFiManager/WiFiManager.cpp b/lib/WiFiManager/WiFiManager.cpp index 6d28256..e0531b3 100644 --- a/lib/WiFiManager/WiFiManager.cpp +++ b/lib/WiFiManager/WiFiManager.cpp @@ -212,8 +212,19 @@ WiFiManager::WiFiManager(Print& consolePort):_debugPort(consolePort){ WiFiManagerInit(); } +WiFiManager::WiFiManager(const char* user, const char* password) { + WiFiManagerInit(); + + if(strlen(user) > 0) + { + strcpy(_credUser, user); + strcpy(_credPassword, password); + _hasCredentials = true; + } +} + WiFiManager::WiFiManager() { - WiFiManagerInit(); + WiFiManagerInit(); } void WiFiManager::WiFiManagerInit(){ @@ -1329,6 +1340,9 @@ void WiFiManager::handleRoot() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Root")); #endif + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } if (captivePortal()) return; // If captive portal redirect instead of displaying the page handleRequest(); String page = getHTTPHead(_title); // @token options @todo replace options with title @@ -1355,6 +1369,11 @@ void WiFiManager::handleWifi(boolean scan) { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Wifi")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); String page = getHTTPHead(FPSTR(S_titlewifi)); // @token titlewifi if (scan) { @@ -1411,6 +1430,11 @@ void WiFiManager::handleParam(){ #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Param")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi @@ -1790,6 +1814,11 @@ void WiFiManager::handleWiFiStatus(){ #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi status ")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); String page; // String page = "{\"result\":true,\"count\":1}"; @@ -1807,6 +1836,11 @@ void WiFiManager::handleWifiSave() { DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi save ")); DEBUG_WM(DEBUG_DEV,F("Method:"),server->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST)); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); //SAVE/connect here @@ -1882,6 +1916,11 @@ void WiFiManager::handleParamSave() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Method:"),server->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST)); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); doParamSave(); @@ -1951,6 +1990,11 @@ void WiFiManager::handleInfo() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Info")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); String page = getHTTPHead(FPSTR(S_titleinfo)); // @token titleinfo reportStatus(page); @@ -2296,6 +2340,11 @@ void WiFiManager::handleExit() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Exit")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); String page = getHTTPHead(FPSTR(S_titleexit)); // @token titleexit page += FPSTR(S_exiting); // @token exiting @@ -2313,6 +2362,11 @@ void WiFiManager::handleReset() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Reset")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); String page = getHTTPHead(FPSTR(S_titlereset)); //@token titlereset page += FPSTR(S_resetting); //@token resetting @@ -2338,6 +2392,11 @@ void WiFiManager::handleErase(boolean opt) { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_NOTIFY,F("<- HTTP Erase")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + handleRequest(); String page = getHTTPHead(FPSTR(S_titleerase)); // @token titleerase @@ -2424,6 +2483,11 @@ void WiFiManager::stopCaptivePortal(){ // HTTPD CALLBACK, handle close, stop captive portal, if not enabled undefined void WiFiManager::handleClose(){ DEBUG_WM(DEBUG_VERBOSE,F("Disabling Captive Portal")); + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + stopCaptivePortal(); #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP close")); @@ -3815,6 +3879,11 @@ void WiFiManager::handleUpdate() { #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- Handle update")); #endif + + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + if (captivePortal()) return; // If captive portal redirect instead of displaying the page String page = getHTTPHead(_title); // @token options String str = FPSTR(HTTP_ROOT_MAIN); @@ -3926,6 +3995,10 @@ void WiFiManager::handleUpdateDone() { DEBUG_WM(DEBUG_VERBOSE, F("<- Handle update done")); // if (captivePortal()) return; // If captive portal redirect instead of displaying the page + if (_hasCredentials && !server->authenticate(_credUser, _credPassword)) { + return server->requestAuthentication(); + } + String page = getHTTPHead(FPSTR(S_options)); // @token options String str = FPSTR(HTTP_ROOT_MAIN); str.replace(FPSTR(T_t),_title); diff --git a/lib/WiFiManager/WiFiManager.h b/lib/WiFiManager/WiFiManager.h index 36fe039..441fbaf 100644 --- a/lib/WiFiManager/WiFiManager.h +++ b/lib/WiFiManager/WiFiManager.h @@ -239,6 +239,7 @@ class WiFiManager { public: WiFiManager(Print& consolePort); + WiFiManager(const char* user, const char* password); WiFiManager(); ~WiFiManager(); void WiFiManagerInit(); @@ -820,6 +821,10 @@ class WiFiManager std::function _preotaupdatecallback; std::function _configportaltimeoutcallback; + bool _hasCredentials = false; + char _credUser[31] = {0}; + char _credPassword[31] = {0}; + template auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) { return obj->fromString(s); diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 1cf1709..9720eaf 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -8,16 +8,17 @@ RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17]; -WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences, const IPConfiguration* ipConfiguration) -: NetworkDevice(hostname, ipConfiguration) +WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration) +: NetworkDevice(hostname, ipConfiguration), + _wm(preferences->getString(preference_cred_user).c_str(), preferences->getString(preference_cred_password).c_str()) { _startAp = strcmp(WiFiDevice_reconfdetect, "reconfigure_wifi") == 0; - _restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect); + _restartOnDisconnect = preferences->getBool(preference_restart_on_disconnect); - size_t caLength = _preferences->getString(preference_mqtt_ca,_ca,TLS_CA_MAX_SIZE); - size_t crtLength = _preferences->getString(preference_mqtt_crt,_cert,TLS_CERT_MAX_SIZE); - size_t keyLength = _preferences->getString(preference_mqtt_key,_key,TLS_KEY_MAX_SIZE); + size_t caLength = preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE); + size_t crtLength = preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE); + size_t keyLength = preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE); _useEncryption = caLength > 1; // length is 1 when empty @@ -41,12 +42,12 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences, const _mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO); } - if(_preferences->getBool(preference_mqtt_log_enabled)) + if(preferences->getBool(preference_mqtt_log_enabled)) { _path = new char[200]; memset(_path, 0, sizeof(_path)); - String pathStr = _preferences->getString(preference_mqtt_lock_path); + String pathStr = preferences->getString(preference_mqtt_lock_path); pathStr.concat(mqtt_topic_log); strcpy(_path, pathStr.c_str()); Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial); diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 99a7dbc..8413b43 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -11,7 +11,7 @@ class WifiDevice : public NetworkDevice { public: - WifiDevice(const String& hostname, Preferences* _preferences, const IPConfiguration* ipConfiguration); + WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration); const String deviceName() const override;