From 83e6b2a39f096c1ce86418e4fb4ff870b9fe63b0 Mon Sep 17 00:00:00 2001 From: Chris Deering Date: Tue, 28 Mar 2017 15:47:08 +0100 Subject: [PATCH] Adding the ability to override the user agent for the oauth2 flow, as well as a scoped server test to verify. --- Release/include/cpprest/oauth2.h | 52 ++++++++++++------- Release/src/http/oauth/oauth2.cpp | 5 ++ .../functional/http/client/oauth2_tests.cpp | 16 ++++++ 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Release/include/cpprest/oauth2.h b/Release/include/cpprest/oauth2.h index 39b77e643c..7afccdc3e1 100644 --- a/Release/include/cpprest/oauth2.h +++ b/Release/include/cpprest/oauth2.h @@ -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), @@ -436,23 +438,34 @@ class oauth2_config /// void set_access_token_key(utility::string_t access_token_key) { m_access_token_key = std::move(access_token_key); } - /// - /// Get the web proxy object - /// - /// A reference to the web proxy object. - const web_proxy& proxy() const - { - return m_proxy; - } - - /// - /// Set the web proxy object that will be used by token_from_code and token_from_refresh - /// - /// A reference to the web proxy object. - void set_proxy(const web_proxy& proxy) - { - m_proxy = proxy; - } + /// + /// Get the web proxy object + /// + /// A reference to the web proxy object. + const web_proxy& proxy() const + { + return m_proxy; + } + + /// + /// Set the web proxy object that will be used by token_from_code and token_from_refresh + /// + /// A reference to the web proxy object. + void set_proxy(const web_proxy& proxy) + { + m_proxy = proxy; + } + + /// + /// Get user agent to be used in oauth2 flows. + /// + /// User agent string. + const utility::string_t& user_agent() const { return m_user_agent; } + /// + /// Set user agent to be used in oauth2 flows. + /// If none is provided a default user agent is provided. + /// + 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; @@ -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; diff --git a/Release/src/http/oauth/oauth2.cpp b/Release/src/http/oauth/oauth2.cpp index 2ebdd14a26..09a453835c 100644 --- a/Release/src/http/oauth/oauth2.cpp +++ b/Release/src/http/oauth/oauth2.cpp @@ -101,6 +101,11 @@ pplx::task 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()) + { + request.headers().add(web::http::header_names::user_agent, user_agent()); + } if (!scope().empty()) { diff --git a/Release/tests/functional/http/client/oauth2_tests.cpp b/Release/tests/functional/http/client/oauth2_tests.cpp index cdc01f94df..d641631ad9 100644 --- a/Release/tests/functional/http/client/oauth2_tests.cpp +++ b/Release/tests/functional/http/client/oauth2_tests.cpp @@ -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) { @@ -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) @@ -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 headers; headers[header_names::content_type] = mime_types::application_json; request->reply(status_codes::OK, U(""), headers, "{\"access_token\":\"xyzzy123\",\"token_type\":\"bearer\"}"); @@ -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 headers; headers[header_names::content_type] = mime_types::application_json; request->reply(status_codes::OK, U(""), headers, "{\"access_token\":\"xyzzy123\",\"token_type\":\"bearer\"}");