Skip to content

Commit f35ebf5

Browse files
committed
Merge branch 'basic_auth_asio' of https://github.com/ritzk/cpprestsdk into ritzk-basic_auth_asio
2 parents 283ec85 + 526e997 commit f35ebf5

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

Release/src/http/client/http_client_asio.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,12 @@ class asio_context : public request_context, public std::enable_shared_from_this
774774
{
775775
extra_headers.append(ctx->generate_basic_proxy_auth_header());
776776
}
777-
777+
778+
if (ctx->m_http_client->client_config().credentials().is_set())
779+
{
780+
extra_headers.append(ctx->generate_basic_auth_header());
781+
}
782+
778783
// Check user specified transfer-encoding.
779784
std::string transferencoding;
780785
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
873878
}
874879

875880
private:
881+
utility::string_t generate_basic_auth_header()
882+
{
883+
utility::string_t header;
884+
885+
header.append(header_names::authorization);
886+
header.append(": Basic ");
887+
888+
auto credential_str = web::details::plaintext_string(new ::utility::string_t(m_http_client->client_config().credentials().username()));
889+
credential_str->append(":");
890+
credential_str->append(*m_http_client->client_config().credentials().decrypt());
891+
892+
std::vector<unsigned char> credentials_buffer(credential_str->begin(), credential_str->end());
893+
894+
header.append(utility::conversions::to_base64(credentials_buffer));
895+
header.append(CRLF);
896+
return header;
897+
}
876898

877899
utility::string_t generate_basic_proxy_auth_header()
878900
{

Release/tests/functional/http/client/authentication_tests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,37 @@ TEST_FIXTURE(uri_address, failed_authentication_attempt, "Ignore:Linux", "89", "
620620

621621
#if !defined(_WIN32)
622622

623+
// http_server does not support auth
624+
void auth_test_impl(bool fail)
625+
{
626+
std::string user("user1"), password("user1");
627+
auto return_code = status_codes::NotFound; // return 404 if successful auth
628+
629+
if (fail)
630+
{
631+
password = "invalid";
632+
return_code = status_codes::Unauthorized;
633+
}
634+
635+
http_client_config client_config;
636+
web::credentials cred(U(user), U(password));
637+
client_config.set_credentials(cred);
638+
http_client client(U("http://test.webdav.org/auth-basic/"), client_config);
639+
640+
http_response response = client.request(methods::GET).get();
641+
VERIFY_ARE_EQUAL(return_code, response.status_code());
642+
}
643+
644+
TEST(auth_no_data)
645+
{
646+
auth_test_impl(false);
647+
}
648+
649+
TEST(unsuccessful_auth_with_basic_cred)
650+
{
651+
auth_test_impl(true);
652+
}
653+
623654
TEST_FIXTURE(uri_address, set_user_options_asio_http)
624655
{
625656
test_http_server::scoped_server scoped(m_uri);

0 commit comments

Comments
 (0)