Skip to content
Merged
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
52 changes: 33 additions & 19 deletions Release/include/cpprest/oauth2.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,15 @@ class oauth2_config

oauth2_config(utility::string_t client_key, utility::string_t client_secret,
utility::string_t auth_endpoint, utility::string_t token_endpoint,
utility::string_t redirect_uri, utility::string_t scope=utility::string_t()) :
utility::string_t redirect_uri, utility::string_t scope=utility::string_t(),
utility::string_t user_agent=utility::string_t()) :
m_client_key(std::move(client_key)),
m_client_secret(std::move(client_secret)),
m_auth_endpoint(std::move(auth_endpoint)),
m_token_endpoint(std::move(token_endpoint)),
m_redirect_uri(std::move(redirect_uri)),
m_scope(std::move(scope)),
m_user_agent(std::move(user_agent)),
m_implicit_grant(false),
m_bearer_auth(true),
m_http_basic_auth(true),
Expand Down Expand Up @@ -436,23 +438,34 @@ class oauth2_config
/// </summary>
void set_access_token_key(utility::string_t access_token_key) { m_access_token_key = std::move(access_token_key); }

/// <summary>
/// Get the web proxy object
/// </summary>
/// <returns>A reference to the web proxy object.</returns>
const web_proxy& proxy() const
{
return m_proxy;
}

/// <summary>
/// Set the web proxy object that will be used by token_from_code and token_from_refresh
/// </summary>
/// <param name="proxy">A reference to the web proxy object.</param>
void set_proxy(const web_proxy& proxy)
{
m_proxy = proxy;
}
/// <summary>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just changed tabs to spaces for this, the rest of the file is all spaces.

/// Get the web proxy object
/// </summary>
/// <returns>A reference to the web proxy object.</returns>
const web_proxy& proxy() const
{
return m_proxy;
}

/// <summary>
/// Set the web proxy object that will be used by token_from_code and token_from_refresh
/// </summary>
/// <param name="proxy">A reference to the web proxy object.</param>
void set_proxy(const web_proxy& proxy)
{
m_proxy = proxy;
}

/// <summary>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new accessors.

/// Get user agent to be used in oauth2 flows.
/// </summary>
/// <returns>User agent string.</returns>
const utility::string_t& user_agent() const { return m_user_agent; }
/// <summary>
/// Set user agent to be used in oauth2 flows.
/// If none is provided a default user agent is provided.
/// </summary>
void set_user_agent(utility::string_t user_agent) { m_user_agent = std::move(user_agent); }

private:
friend class web::http::client::http_client_config;
Expand Down Expand Up @@ -489,8 +502,9 @@ class oauth2_config
utility::string_t m_redirect_uri;
utility::string_t m_scope;
utility::string_t m_state;
utility::string_t m_user_agent;

web::web_proxy m_proxy;
web::web_proxy m_proxy;

bool m_implicit_grant;
bool m_bearer_auth;
Expand Down
5 changes: 5 additions & 0 deletions Release/src/http/oauth/oauth2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ pplx::task<void> oauth2_config::_request_token(uri_builder& request_body_ub)
http_request request;
request.set_method(methods::POST);
request.set_request_uri(utility::string_t());

if(!user_agent().empty())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, the default user agent will be used

{
request.headers().add(web::http::header_names::user_agent, user_agent());
}

if (!scope().empty())
{
Expand Down
16 changes: 16 additions & 0 deletions Release/tests/functional/http/client/oauth2_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ static bool is_application_x_www_form_urlencoded(test_request *request)
return (0 == content_type.find(mime_types::application_x_www_form_urlencoded));
}

static utility::string_t get_request_user_agent(test_request *request)
{
if (request->m_headers.find(header_names::user_agent) != request->m_headers.end())
{
return request->m_headers[header_names::user_agent];
}

return utility::string_t();
}

SUITE(oauth2_tests)
{

Expand Down Expand Up @@ -137,6 +147,8 @@ TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_code)
{
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());

m_oauth2_config.set_user_agent(U("test_user_agent"));

// Fetch using HTTP Basic authentication.
{
m_scoped.server()->next_request().then([](test_request *request)
Expand All @@ -151,6 +163,8 @@ TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_code)
U("grant_type=authorization_code&code=789GHI&redirect_uri=https%3A%2F%2Fbar")),
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\":\"xyzzy123\",\"token_type\":\"bearer\"}");
Expand All @@ -173,6 +187,8 @@ TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_code)
U("grant_type=authorization_code&code=789GHI&redirect_uri=https%3A%2F%2Fbar&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\":\"xyzzy123\",\"token_type\":\"bearer\"}");
Expand Down