Rough in remove_password function + tests

This commit is contained in:
Isaac Connor
2022-07-04 09:53:40 -04:00
parent d958c6fee0
commit 365830a9ae
3 changed files with 56 additions and 0 deletions

View File

@@ -471,3 +471,30 @@ std::string mask_authentication(const std::string &url) {
} }
return masked_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;
}

View File

@@ -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 mask_authentication(const std::string &url);
std::string remove_authentication(const std::string &url);
std::string UriDecode(const std::string &encoded); std::string UriDecode(const std::string &encoded);

View File

@@ -278,3 +278,31 @@ TEST_CASE("mask_authentication") {
REQUIRE(result == "http://********:********@192.168.1.1"); 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");
}
}