Skip to content

Commit c0613af

Browse files
authored
Merge pull request #398 from deeringc/oauth2_user_agent
Add the ability to override the user agent for the oauth2 flow
2 parents acd8129 + 83e6b2a commit c0613af

File tree

3 files changed

+54
-19
lines changed

3 files changed

+54
-19
lines changed

Release/include/cpprest/oauth2.h

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,15 @@ class oauth2_config
204204

205205
oauth2_config(utility::string_t client_key, utility::string_t client_secret,
206206
utility::string_t auth_endpoint, utility::string_t token_endpoint,
207-
utility::string_t redirect_uri, utility::string_t scope=utility::string_t()) :
207+
utility::string_t redirect_uri, utility::string_t scope=utility::string_t(),
208+
utility::string_t user_agent=utility::string_t()) :
208209
m_client_key(std::move(client_key)),
209210
m_client_secret(std::move(client_secret)),
210211
m_auth_endpoint(std::move(auth_endpoint)),
211212
m_token_endpoint(std::move(token_endpoint)),
212213
m_redirect_uri(std::move(redirect_uri)),
213214
m_scope(std::move(scope)),
215+
m_user_agent(std::move(user_agent)),
214216
m_implicit_grant(false),
215217
m_bearer_auth(true),
216218
m_http_basic_auth(true),
@@ -436,23 +438,34 @@ class oauth2_config
436438
/// </summary>
437439
void set_access_token_key(utility::string_t access_token_key) { m_access_token_key = std::move(access_token_key); }
438440

439-
/// <summary>
440-
/// Get the web proxy object
441-
/// </summary>
442-
/// <returns>A reference to the web proxy object.</returns>
443-
const web_proxy& proxy() const
444-
{
445-
return m_proxy;
446-
}
447-
448-
/// <summary>
449-
/// Set the web proxy object that will be used by token_from_code and token_from_refresh
450-
/// </summary>
451-
/// <param name="proxy">A reference to the web proxy object.</param>
452-
void set_proxy(const web_proxy& proxy)
453-
{
454-
m_proxy = proxy;
455-
}
441+
/// <summary>
442+
/// Get the web proxy object
443+
/// </summary>
444+
/// <returns>A reference to the web proxy object.</returns>
445+
const web_proxy& proxy() const
446+
{
447+
return m_proxy;
448+
}
449+
450+
/// <summary>
451+
/// Set the web proxy object that will be used by token_from_code and token_from_refresh
452+
/// </summary>
453+
/// <param name="proxy">A reference to the web proxy object.</param>
454+
void set_proxy(const web_proxy& proxy)
455+
{
456+
m_proxy = proxy;
457+
}
458+
459+
/// <summary>
460+
/// Get user agent to be used in oauth2 flows.
461+
/// </summary>
462+
/// <returns>User agent string.</returns>
463+
const utility::string_t& user_agent() const { return m_user_agent; }
464+
/// <summary>
465+
/// Set user agent to be used in oauth2 flows.
466+
/// If none is provided a default user agent is provided.
467+
/// </summary>
468+
void set_user_agent(utility::string_t user_agent) { m_user_agent = std::move(user_agent); }
456469

457470
private:
458471
friend class web::http::client::http_client_config;
@@ -489,8 +502,9 @@ class oauth2_config
489502
utility::string_t m_redirect_uri;
490503
utility::string_t m_scope;
491504
utility::string_t m_state;
505+
utility::string_t m_user_agent;
492506

493-
web::web_proxy m_proxy;
507+
web::web_proxy m_proxy;
494508

495509
bool m_implicit_grant;
496510
bool m_bearer_auth;

Release/src/http/oauth/oauth2.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ pplx::task<void> oauth2_config::_request_token(uri_builder& request_body_ub)
101101
http_request request;
102102
request.set_method(methods::POST);
103103
request.set_request_uri(utility::string_t());
104+
105+
if(!user_agent().empty())
106+
{
107+
request.headers().add(web::http::header_names::user_agent, user_agent());
108+
}
104109

105110
if (!scope().empty())
106111
{

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ static bool is_application_x_www_form_urlencoded(test_request *request)
3838
return (0 == content_type.find(mime_types::application_x_www_form_urlencoded));
3939
}
4040

41+
static utility::string_t get_request_user_agent(test_request *request)
42+
{
43+
if (request->m_headers.find(header_names::user_agent) != request->m_headers.end())
44+
{
45+
return request->m_headers[header_names::user_agent];
46+
}
47+
48+
return utility::string_t();
49+
}
50+
4151
SUITE(oauth2_tests)
4252
{
4353

@@ -137,6 +147,8 @@ TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_code)
137147
{
138148
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());
139149

150+
m_oauth2_config.set_user_agent(U("test_user_agent"));
151+
140152
// Fetch using HTTP Basic authentication.
141153
{
142154
m_scoped.server()->next_request().then([](test_request *request)
@@ -151,6 +163,8 @@ TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_code)
151163
U("grant_type=authorization_code&code=789GHI&redirect_uri=https%3A%2F%2Fbar")),
152164
request->m_body);
153165

166+
VERIFY_ARE_EQUAL(U("test_user_agent"), get_request_user_agent(request));
167+
154168
std::map<utility::string_t, utility::string_t> headers;
155169
headers[header_names::content_type] = mime_types::application_json;
156170
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)
173187
U("grant_type=authorization_code&code=789GHI&redirect_uri=https%3A%2F%2Fbar&client_id=123ABC&client_secret=456DEF")),
174188
request->m_body);
175189

190+
VERIFY_ARE_EQUAL(U("test_user_agent"), get_request_user_agent(request));
191+
176192
std::map<utility::string_t, utility::string_t> headers;
177193
headers[header_names::content_type] = mime_types::application_json;
178194
request->reply(status_codes::OK, U(""), headers, "{\"access_token\":\"xyzzy123\",\"token_type\":\"bearer\"}");

0 commit comments

Comments
 (0)