From 2e101839a457ead0bd6d4ede0856902000d3b65c Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 10 Jan 2019 12:57:42 +0100 Subject: [PATCH] Change web authentication Change web authentication (#4865) --- sonoff/_changelog.ino | 3 +- sonoff/xdrv_01_webserver.ino | 71 +++++++++++++++++++----------------- sonoff/xdrv_02_mqtt.ino | 4 +- sonoff/xdrv_07_domoticz.ino | 4 +- sonoff/xdrv_09_timers.ino | 4 +- sonoff/xdrv_11_knx.ino | 4 +- sonoff/xsns_34_hx711.ino | 4 +- 7 files changed, 49 insertions(+), 45 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 00e3701f4..2bb633598 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -5,7 +5,8 @@ * Fix Home Assistant Sensor Discovery Software Watchdog restart (#4831) * Add support for OBI Power Socket 2 (#4829) * Add support for YTF IR Bridge (#4855) - * + * Change web authentication (#4865) + * * 6.4.1.7 20190106 * Fix HLW8012, HJL01 and BL0937 based energy sensors low Power (below 10W) measurement regression from 6.4.1.6 * Add Power status functionality to LED2 when configured leaving LED1 for Link status indication diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 6072f20fd..9c62a7561 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -755,11 +755,17 @@ void HandleAjaxStatusRefresh(void) WebServer->send(200, FPSTR(HDR_CTYPE_HTML), mqtt_data); } -boolean HttpUser(void) +boolean HttpCheckPriviledgedAccess(boolean autorequestauth = true) { - boolean status = (HTTP_USER == webserver_state); - if (status) { HandleRoot(); } - return status; + if (HTTP_USER == webserver_state) { + HandleRoot(); + return false; + } + if (autorequestauth && !WebAuthenticate()) { + WebServer->requestAuthentication(); + return false; + } + return true; } /*-------------------------------------------------------------------------------------------*/ @@ -768,8 +774,8 @@ boolean HttpUser(void) void HandleConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURATION); String page = FPSTR(HTTP_HEAD); @@ -791,8 +797,7 @@ void HandleConfiguration(void) void HandleModuleConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } if (WebServer->hasArg("save")) { ModuleSaveSettings(); @@ -910,8 +915,7 @@ String htmlEscape(String s) void HandleWifiConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_WIFI); @@ -1038,8 +1042,8 @@ void WifiSaveSettings(void) void HandleLoggingConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_LOGGING); if (WebServer->hasArg("save")) { @@ -1121,8 +1125,8 @@ void LoggingSaveSettings(void) void HandleOtherConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_OTHER); if (WebServer->hasArg("save")) { @@ -1193,8 +1197,8 @@ void OtherSaveSettings(void) void HandleBackupConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_BACKUP_CONFIGURATION)); if (!SettingsBufferAlloc()) { return; } @@ -1237,8 +1241,7 @@ void HandleBackupConfiguration(void) void HandleResetConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } char svalue[33]; @@ -1258,8 +1261,8 @@ void HandleResetConfiguration(void) void HandleRestoreConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_RESTORE_CONFIGURATION); String page = FPSTR(HTTP_HEAD); @@ -1279,8 +1282,8 @@ void HandleRestoreConfiguration(void) void HandleInformation(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_INFORMATION); char stopic[TOPSZ]; @@ -1402,8 +1405,8 @@ void HandleInformation(void) void HandleUpgradeFirmware(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_FIRMWARE_UPGRADE); String page = FPSTR(HTTP_HEAD); @@ -1422,8 +1425,8 @@ void HandleUpgradeFirmware(void) void HandleUpgradeFirmwareStart(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + char svalue[100]; AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_UPGRADE_STARTED)); @@ -1451,8 +1454,8 @@ void HandleUpgradeFirmwareStart(void) void HandleUploadDone(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_UPLOAD_DONE)); char error[100]; @@ -1709,8 +1712,8 @@ void HandlePreflightRequest(void) void HandleHttpCommand(void) { - if (HttpUser()) { return; } -// if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess(false)) { return; } + char svalue[INPUT_BUFFER_SIZE]; // Large to serve Backlog AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_COMMAND)); @@ -1769,8 +1772,8 @@ void HandleHttpCommand(void) void HandleConsole(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONSOLE); String page = FPSTR(HTTP_HEAD); @@ -1785,8 +1788,8 @@ void HandleConsole(void) void HandleAjaxConsoleRefresh(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + char svalue[INPUT_BUFFER_SIZE]; // Large to serve Backlog byte cflg = 1; byte counter = 0; // Initial start, should never be 0 again diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 1a3525cbc..1d3c3a4b8 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -863,8 +863,8 @@ const char HTTP_FORM_MQTT[] PROGMEM = void HandleMqttConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_MQTT); if (WebServer->hasArg("save")) { diff --git a/sonoff/xdrv_07_domoticz.ino b/sonoff/xdrv_07_domoticz.ino index 38f6535b2..b36b3efc2 100644 --- a/sonoff/xdrv_07_domoticz.ino +++ b/sonoff/xdrv_07_domoticz.ino @@ -448,8 +448,8 @@ const char HTTP_FORM_DOMOTICZ_TIMER[] PROGMEM = void HandleDomoticzConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_DOMOTICZ); if (WebServer->hasArg("save")) { diff --git a/sonoff/xdrv_09_timers.ino b/sonoff/xdrv_09_timers.ino index f950c5ce0..ded49e857 100644 --- a/sonoff/xdrv_09_timers.ino +++ b/sonoff/xdrv_09_timers.ino @@ -682,8 +682,8 @@ const char HTTP_FORM_TIMER1[] PROGMEM = void HandleTimerConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_TIMER); if (WebServer->hasArg("save")) { diff --git a/sonoff/xdrv_11_knx.ino b/sonoff/xdrv_11_knx.ino index 1f9d702dd..8286443d8 100644 --- a/sonoff/xdrv_11_knx.ino +++ b/sonoff/xdrv_11_knx.ino @@ -805,8 +805,8 @@ const char HTTP_FORM_KNX_ADD_TABLE_ROW2[] PROGMEM = void HandleKNXConfiguration(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_KNX); char tmp[100]; diff --git a/sonoff/xsns_34_hx711.ino b/sonoff/xsns_34_hx711.ino index e57b49c44..f46e69359 100644 --- a/sonoff/xsns_34_hx711.ino +++ b/sonoff/xsns_34_hx711.ino @@ -393,8 +393,8 @@ const char HTTP_FORM_HX711[] PROGMEM = void HandleHxAction(void) { - if (HttpUser()) { return; } - if (!WebAuthenticate()) { return WebServer->requestAuthentication(); } + if (!HttpCheckPriviledgedAccess()) { return; } + AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_HX711); if (WebServer->hasArg("save")) {