diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index f9fc7f546..693fa13a4 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -119,8 +119,8 @@ FfmpegCamera::FfmpegCamera( hwaccel_name(p_hwaccel_name), hwaccel_device(p_hwaccel_device) { - mMaskedPath = mask_authentication(mPath); - mMaskedSecondPath = mask_authentication(mSecondPath); + mMaskedPath = remove_authentication(mPath); + mMaskedSecondPath = remove_authentication(mSecondPath); if ( capture ) { FFMPEGInit(); } diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index 8b7fb9ecd..d60176b2d 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -471,3 +471,30 @@ std::string mask_authentication(const std::string &url) { } return masked_url; } + +std::string remove_authentication(const std::string &url) { + std::size_t at_at = url.find("@"); + if (at_at == std::string::npos) { + return url; + } + std::string result; + std::size_t password_at = url.rfind(":", at_at); + + if (password_at == std::string::npos) { + // no : means no http:// either so something like username@192.168.1.1 + result = url.substr(at_at+1); + } else if (url[password_at+1] == '/') { + // no password, something like http://username@192.168.1.1 + result = url.substr(0, password_at+3) + url.substr(at_at+1); + } else { + std::size_t username_at = url.rfind("/", password_at); + if (username_at == std::string::npos) { + // Something like username:password@192.168.1.1 + result = url.substr(at_at+1); + } else { + // have username and password, something like http://username:password@192.168.1.1/ + result = url.substr(0, username_at+1) + url.substr(at_at+1); + } + } + return result; +} diff --git a/src/zm_utils.h b/src/zm_utils.h index b9e978238..57b4db4af 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -128,6 +128,7 @@ constexpr std::size_t size(const T(&)[N]) noexcept { return N; } } std::string mask_authentication(const std::string &url); +std::string remove_authentication(const std::string &url); std::string UriDecode(const std::string &encoded); diff --git a/tests/zm_utils.cpp b/tests/zm_utils.cpp index c09d5bdc6..7f0fd370b 100644 --- a/tests/zm_utils.cpp +++ b/tests/zm_utils.cpp @@ -278,3 +278,31 @@ TEST_CASE("mask_authentication") { REQUIRE(result == "http://********:********@192.168.1.1"); } } + +TEST_CASE("remove_authentication") { + SECTION("no authentication") { + std::string url("http://192.168.1.1"); + std::string result = remove_authentication(url); + REQUIRE(url == result); + } + SECTION("has username no password has scheme") { + std::string url("http://username@192.168.1.1"); + std::string result = remove_authentication(url); + REQUIRE(result == "http://192.168.1.1"); + } + SECTION("has username no password no scheme") { + std::string url("username@192.168.1.1"); + std::string result = remove_authentication(url); + REQUIRE(result == "192.168.1.1"); + } + SECTION("has username has password no scheme") { + std::string url("username:password@192.168.1.1"); + std::string result = remove_authentication(url); + REQUIRE(result == "192.168.1.1"); + } + SECTION("has username has password has scheme") { + std::string url("http://username:password@192.168.1.1"); + std::string result = remove_authentication(url); + REQUIRE(result == "http://192.168.1.1"); + } +}