diff --git a/Release/src/http/client/http_client_asio.cpp b/Release/src/http/client/http_client_asio.cpp index b0b7a4ce18..9bd085ab69 100644 --- a/Release/src/http/client/http_client_asio.cpp +++ b/Release/src/http/client/http_client_asio.cpp @@ -774,7 +774,12 @@ class asio_context : public request_context, public std::enable_shared_from_this { extra_headers.append(ctx->generate_basic_proxy_auth_header()); } - + + if (ctx->m_http_client->client_config().credentials().is_set()) + { + extra_headers.append(ctx->generate_basic_auth_header()); + } + // Check user specified transfer-encoding. std::string transferencoding; if (ctx->m_request.headers().match(header_names::transfer_encoding, transferencoding) && transferencoding == "chunked") @@ -873,6 +878,23 @@ class asio_context : public request_context, public std::enable_shared_from_this } private: + utility::string_t generate_basic_auth_header() + { + utility::string_t header; + + header.append(header_names::authorization); + header.append(": Basic "); + + auto credential_str = web::details::plaintext_string(new ::utility::string_t(m_http_client->client_config().credentials().username())); + credential_str->append(":"); + credential_str->append(*m_http_client->client_config().credentials().decrypt()); + + std::vector credentials_buffer(credential_str->begin(), credential_str->end()); + + header.append(utility::conversions::to_base64(credentials_buffer)); + header.append(CRLF); + return header; + } utility::string_t generate_basic_proxy_auth_header() { diff --git a/Release/tests/functional/http/client/authentication_tests.cpp b/Release/tests/functional/http/client/authentication_tests.cpp index f339c64477..d5cdd4605a 100644 --- a/Release/tests/functional/http/client/authentication_tests.cpp +++ b/Release/tests/functional/http/client/authentication_tests.cpp @@ -620,6 +620,37 @@ TEST_FIXTURE(uri_address, failed_authentication_attempt, "Ignore:Linux", "89", " #if !defined(_WIN32) +// http_server does not support auth +void auth_test_impl(bool fail) +{ + std::string user("user1"), password("user1"); + auto return_code = status_codes::NotFound; // return 404 if successful auth + + if (fail) + { + password = "invalid"; + return_code = status_codes::Unauthorized; + } + + http_client_config client_config; + web::credentials cred(U(user), U(password)); + client_config.set_credentials(cred); + http_client client(U("http://test.webdav.org/auth-basic/"), client_config); + + http_response response = client.request(methods::GET).get(); + VERIFY_ARE_EQUAL(return_code, response.status_code()); +} + +TEST(auth_no_data) +{ + auth_test_impl(false); +} + +TEST(unsuccessful_auth_with_basic_cred) +{ + auth_test_impl(true); +} + TEST_FIXTURE(uri_address, set_user_options_asio_http) { test_http_server::scoped_server scoped(m_uri);