apply credentials to wifimanager configuration portal

This commit is contained in:
technyon
2024-01-23 15:46:58 +01:00
parent 31c510dd20
commit 8c8f524e6d
4 changed files with 89 additions and 10 deletions

View File

@@ -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);

View File

@@ -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<void()> _preotaupdatecallback;
std::function<void()> _configportaltimeoutcallback;
bool _hasCredentials = false;
char _credUser[31] = {0};
char _credPassword[31] = {0};
template <class T>
auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) {
return obj->fromString(s);

View File

@@ -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);

View File

@@ -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;