mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2025-12-23 14:27:59 -05:00
Rough in remove_password function + tests
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user