Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Release/include/cpprest/details/http_constants.dat
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ DAT(grant_type, "grant_type")
DAT(redirect_uri, "redirect_uri")
DAT(refresh_token, "refresh_token")
DAT(client_credentials, "client_credentials")
DAT(password, "password")
DAT(username, "username")
DAT(response_type, "response_type")
DAT(scope, "scope")
DAT(state, "state")
Expand Down
16 changes: 16 additions & 0 deletions Release/include/cpprest/oauth2.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,22 @@ class oauth2_config
return _request_token(ub);
}

/// <summary>
/// Fetch an access token from the token endpoint using password grant type.
/// The task creates an HTTP request to the token_endpoint()
/// If successful, resulting access token is set as active via set_token()
/// See: https://datatracker.ietf.org/doc/html/rfc6749#section-1.3.3
/// </summary>
/// <returns>Task that fetches token(s) using a username and password.</returns>
pplx::task<void> token_from_password(const utility::string_t username, const utility::string_t password)
{
uri_builder ub;
ub.append_query(details::oauth2_strings::grant_type, details::oauth2_strings::password, false);
ub.append_query(details::oauth2_strings::username, username, false);
ub.append_query(details::oauth2_strings::password, password, false);
return _request_token(ub);
}

/// <summary>
/// Returns enabled state of the configuration.
/// The oauth2_handler will perform OAuth 2.0 authentication only if
Expand Down
59 changes: 59 additions & 0 deletions Release/tests/functional/http/client/oauth2_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,65 @@ SUITE(oauth2_tests)
}
}

TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_password)
{
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());
m_oauth2_config.set_user_agent(U("test_user_agent"));

{
m_scoped.server()->next_request().then([](test_request* request) {
VERIFY_ARE_EQUAL(request->m_method, methods::POST);

VERIFY_IS_TRUE(is_application_x_www_form_urlencoded(request));

VERIFY_ARE_EQUAL(
U("Basic MTIzQUJDOjQ1NkRFRg=="),
request->m_headers[header_names::authorization]);

VERIFY_ARE_EQUAL(
to_body_data(U("grant_type=password&username=Kappa&password=OMEGALUL")),
request->m_body);

VERIFY_ARE_EQUAL(
U("test_user_agent"),
get_request_user_agent(request));

std::map<utility::string_t, utility::string_t> headers;
headers[header_names::content_type] = mime_types::application_json;
request->reply(
status_codes::OK, U(""), headers, "{\"access_token\":\"super\",\"token_type\":\"bearer\"}");

});
m_oauth2_config.token_from_password(U("Kappa"), U("OMEGALUL")).wait();
VERIFY_ARE_EQUAL(U("super"), m_oauth2_config.token().access_token());
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
}

{
m_scoped.server()->next_request().then([](test_request* request) {
VERIFY_ARE_EQUAL(request->m_method, methods::POST);

VERIFY_ARE_EQUAL(
to_body_data(U("grant_type=password&username=Kappa&password=OMEGALUL&client_id=123ABC&client_secret=456DEF")),
request->m_body);

VERIFY_ARE_EQUAL(
U("test_user_agent"),
get_request_user_agent(request));

std::map<utility::string_t, utility::string_t> headers;
headers[header_names::content_type] = mime_types::application_json;
request->reply(
status_codes::OK, U(""), headers, "{\"access_token\":\"super\",\"token_type\":\"bearer\"}");

});
m_oauth2_config.set_http_basic_auth(false);
m_oauth2_config.token_from_password(U("Kappa"), U("OMEGALUL")).wait();
VERIFY_ARE_EQUAL(U("super"), m_oauth2_config.token().access_token());
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
}
}

TEST_FIXTURE(oauth2_test_setup, oauth2_bearer_token)
{
m_oauth2_config.set_token(oauth2_token(U("12345678")));
Expand Down