diff --git a/tasmota/settings.h b/tasmota/settings.h index 9eaa8605c..08c26db60 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -339,7 +339,7 @@ typedef union { uint32_t gainceiling : 3; uint32_t agc_gain: 5; uint32_t special_effect : 3; - uint32_t spare28 : 1; + uint32_t auth : 1; uint32_t spare29 : 1; uint32_t spare30 : 1; uint32_t upgraded : 1; diff --git a/tasmota/xdrv_81_esp32_webcam.ino b/tasmota/xdrv_81_esp32_webcam.ino index c654d35d5..b49f781d8 100644 --- a/tasmota/xdrv_81_esp32_webcam.ino +++ b/tasmota/xdrv_81_esp32_webcam.ino @@ -729,6 +729,34 @@ pcopy: return _jpg_buf_len; } +//////////////// Handle authentication ///////////////// + + +bool WebcamAuthenticate(void) +{ + if (strlen(SettingsText(SET_WEBPWD)) && (HTTP_MANAGER_RESET_ONLY != Web.state)) { + return Wc.CamServer->authenticate(WEB_USERNAME, SettingsText(SET_WEBPWD)); + } else { + return true; + } +} + +bool WebcamCheckPriviledgedAccess(bool autorequestauth = true) +{ + + if(Settings->webcam_config2.auth == 0){ + return true; + } + + if (autorequestauth && !WebcamAuthenticate()) { + Wc.CamServer->requestAuthentication(); + return false; + } + return true; +} + +/////////////////////////////////////////////////// + void HandleImage(void) { if (!HttpCheckPriviledgedAccess()) { return; } @@ -818,6 +846,10 @@ void HandleImageBasic(void) { } void HandleWebcamMjpeg(void) { + if(!WebcamCheckPriviledgedAccess()){ + Wc.CamServer->send(403,"",""); + return; + } AddLog(LOG_LEVEL_DEBUG, PSTR("CAM: Handle camserver")); // if (!Wc.stream_active) { // always restart stream @@ -910,6 +942,10 @@ void HandleWebcamMjpegTask(void) { } void HandleWebcamRoot(void) { + if(!WebcamCheckPriviledgedAccess()){ + Wc.CamServer->send(403,"",""); + return; + } //CamServer->redirect("http://" + String(ip) + ":81/cam.mjpeg"); Wc.CamServer->sendHeader("Location", "/cam.mjpeg"); Wc.CamServer->send(302, "", ""); @@ -1081,6 +1117,8 @@ void WcInit(void) { #define D_CMND_WC_INIT "Init" #define D_CMND_RTSP "Rtsp" +#define D_CMND_WC_AUTH "Auth" + const char kWCCommands[] PROGMEM = D_PRFX_WEBCAM "|" // Prefix "|" D_CMND_WC_STREAM "|" D_CMND_WC_RESOLUTION "|" D_CMND_WC_MIRROR "|" D_CMND_WC_FLIP "|" D_CMND_WC_SATURATION "|" D_CMND_WC_BRIGHTNESS "|" D_CMND_WC_CONTRAST "|" D_CMND_WC_SPECIALEFFECT "|" @@ -1088,7 +1126,7 @@ const char kWCCommands[] PROGMEM = D_PRFX_WEBCAM "|" // Prefix D_CMND_WC_AEC_VALUE "|" D_CMND_WC_AE_LEVEL "|" D_CMND_WC_AEC2 "|" D_CMND_WC_AGC "|" D_CMND_WC_AGC_GAIN "|" D_CMND_WC_GAINCEILING "|" D_CMND_WC_RAW_GMA "|" D_CMND_WC_LENC "|" D_CMND_WC_WPC "|" D_CMND_WC_DCW "|" D_CMND_WC_BPC "|" D_CMND_WC_COLORBAR "|" D_CMND_WC_FEATURE "|" - D_CMND_WC_SETDEFAULTS "|" D_CMND_WC_STATS "|" D_CMND_WC_INIT + D_CMND_WC_SETDEFAULTS "|" D_CMND_WC_STATS "|" D_CMND_WC_INIT "|" D_CMND_WC_AUTH #ifdef ENABLE_RTSPSERVER "|" D_CMND_RTSP #endif // ENABLE_RTSPSERVER @@ -1101,7 +1139,7 @@ void (* const WCCommand[])(void) PROGMEM = { &CmndWebcamAELevel, &CmndWebcamAEC2, &CmndWebcamAGC, &CmndWebcamAGCGain, &CmndWebcamGainCeiling, &CmndWebcamGammaCorrect, &CmndWebcamLensCorrect, &CmndWebcamWPC, &CmndWebcamDCW, &CmndWebcamBPC, &CmndWebcamColorbar, &CmndWebcamFeature, &CmndWebcamSetDefaults, - &CmndWebcamStats, &CmndWebcamInit + &CmndWebcamStats, &CmndWebcamInit, &CmndWebcamAuth #ifdef ENABLE_RTSPSERVER , &CmndWebRtsp #endif // ENABLE_RTSPSERVER @@ -1116,7 +1154,7 @@ void CmndWebcam(void) { D_CMND_WC_AE_LEVEL "\":%d,\"" D_CMND_WC_AEC2 "\":%d,\"" D_CMND_WC_AGC "\":%d,\"" D_CMND_WC_AGC_GAIN "\":%d,\"" D_CMND_WC_GAINCEILING "\":%d,\"" D_CMND_WC_RAW_GMA "\":%d,\"" D_CMND_WC_LENC "\":%d,\"" D_CMND_WC_WPC "\":%d,\"" D_CMND_WC_DCW "\":%d,\"" D_CMND_WC_BPC "\":%d,\"" - D_CMND_WC_COLORBAR "\":%d,\"" D_CMND_WC_FEATURE "\":%d" + D_CMND_WC_COLORBAR "\":%d,\"" D_CMND_WC_FEATURE "\":%d,\"" D_CMND_WC_AUTH "\":%d" #ifdef ENABLE_RTSPSERVER ",\"" D_CMND_RTSP "\":%d" #endif // ENABLE_RTSPSERVER @@ -1129,7 +1167,7 @@ void CmndWebcam(void) { Settings->webcam_config2.ae_level -2, Settings->webcam_config.aec2, Settings->webcam_config.agc, Settings->webcam_config2.agc_gain, Settings->webcam_config2.gainceiling, Settings->webcam_config.raw_gma, Settings->webcam_config.lenc, Settings->webcam_config.wpc, Settings->webcam_config.dcw, Settings->webcam_config.bpc, - Settings->webcam_config.colorbar, Settings->webcam_config.feature + Settings->webcam_config.colorbar, Settings->webcam_config.feature, Settings->webcam_config2.auth #ifdef ENABLE_RTSPSERVER , Settings->webcam_config.rtsp #endif // ENABLE_RTSPSERVER @@ -1336,6 +1374,13 @@ void CmndWebcamFeature(void) { ResponseCmndNumber(Settings->webcam_config.feature); } +void CmndWebcamAuth(void){ + if((XdrvMailbox.payload >=0) && (XdrvMailbox.payload <= 1)){ + Settings->webcam_config2.auth = XdrvMailbox.payload; + } + ResponseCmndNumber(Settings->webcam_config2.auth); +} + void CmndWebcamInit(void) { WcInterruptControl(); ResponseCmndDone();