Files
@ f2a6ba12fc29
Branch filter:
Location: libtransport.git/3rdparty/cpprestsdk/tests/functional/http/client/oauth2_tests.cpp
f2a6ba12fc29
14.4 KiB
text/x-c++hdr
Slack frontend stub
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | /***
* ==++==
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ==--==
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* Test cases for oauth2.
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
#include "stdafx.h"
#include "cpprest/details/http_helpers.h"
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace web::http::details;
using namespace web::http::oauth2::experimental;
using namespace utility;
using namespace concurrency;
using namespace tests::functional::http::utilities;
extern utility::string_t _to_base64(const unsigned char *ptr, size_t size);
namespace tests { namespace functional { namespace http { namespace client
{
static std::vector<unsigned char> to_body_data(utility::string_t str)
{
const std::string utf8(conversions::to_utf8string(std::move(str)));
return std::vector<unsigned char>(utf8.data(), utf8.data() + utf8.size());
}
static bool is_application_x_www_form_urlencoded(test_request *request)
{
const auto content_type(request->m_headers[header_names::content_type]);
return (0 == content_type.find(mime_types::application_x_www_form_urlencoded));
}
SUITE(oauth2_tests)
{
struct oauth2_test_setup
{
oauth2_test_setup() :
m_uri(U("http://localhost:16743/")),
m_oauth2_config(U("123ABC"), U("456DEF"), U("https://test1"), m_uri.to_string(), U("https://bar")),
m_scoped(m_uri)
{}
web::http::uri m_uri;
oauth2_config m_oauth2_config;
test_http_server::scoped_server m_scoped;
};
#define TEST_ACCESSOR(value_, name_) \
t.set_ ## name_(value_); \
VERIFY_ARE_EQUAL(value_, t.name_());
TEST(oauth2_token_accessors)
{
oauth2_token t;
TEST_ACCESSOR(U("b%20456"), access_token)
TEST_ACCESSOR(U("a%123"), refresh_token)
TEST_ACCESSOR(U("b%20456"), token_type)
TEST_ACCESSOR(U("ad.ww xyz"), scope)
TEST_ACCESSOR(0, expires_in)
TEST_ACCESSOR(123, expires_in)
}
TEST(oauth2_config_accessors)
{
oauth2_config t(U(""), U(""), U(""), U(""), U(""));
TEST_ACCESSOR(U("ABC123abc"), client_key)
TEST_ACCESSOR(U("123abcABC"), client_secret)
TEST_ACCESSOR(U("x:/t/a?q=c&a#3"), auth_endpoint)
TEST_ACCESSOR(U("y:///?a=21#1=2"), token_endpoint)
TEST_ACCESSOR(U("z://?=#"), redirect_uri)
TEST_ACCESSOR(U("xyzw=stuv"), scope)
TEST_ACCESSOR(U("1234567890"), state)
TEST_ACCESSOR(U("keyx"), access_token_key)
TEST_ACCESSOR(true, implicit_grant)
TEST_ACCESSOR(false, implicit_grant)
TEST_ACCESSOR(true, bearer_auth)
TEST_ACCESSOR(false, bearer_auth)
TEST_ACCESSOR(true, http_basic_auth)
TEST_ACCESSOR(false, http_basic_auth)
}
#undef TEST_ACCESSOR
TEST(oauth2_build_authorization_uri)
{
oauth2_config config(U(""), U(""), U(""), U(""), U(""));
config.set_state(U("xyzzy"));
config.set_implicit_grant(false);
// Empty authorization URI.
{
VERIFY_ARE_EQUAL(U("/?response_type=code&client_id=&redirect_uri=&state=xyzzy"),
config.build_authorization_uri(false));
}
// Authorization URI with scope parameter.
{
config.set_scope(U("testing_123"));
VERIFY_ARE_EQUAL(U("/?response_type=code&client_id=&redirect_uri=&state=xyzzy&scope=testing_123"),
config.build_authorization_uri(false));
}
// Full authorization URI with scope.
{
config.set_client_key(U("4567abcd"));
config.set_auth_endpoint(U("https://test1"));
config.set_redirect_uri(U("http://localhost:8080"));
VERIFY_ARE_EQUAL(U("https://test1/?response_type=code&client_id=4567abcd&redirect_uri=http://localhost:8080&state=xyzzy&scope=testing_123"),
config.build_authorization_uri(false));
}
// Verify again with implicit grant.
{
config.set_implicit_grant(true);
VERIFY_ARE_EQUAL(U("https://test1/?response_type=token&client_id=4567abcd&redirect_uri=http://localhost:8080&state=xyzzy&scope=testing_123"),
config.build_authorization_uri(false));
}
// Verify that a new state() will be generated.
{
const uri auth_uri(config.build_authorization_uri(true));
auto params = uri::split_query(auth_uri.query());
VERIFY_ARE_NOT_EQUAL(params[U("state")], U("xyzzy"));
}
}
TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_code)
{
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());
// Fetch using HTTP Basic authentication.
{
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=authorization_code&code=789GHI&redirect_uri=https%3A%2F%2Fbar")),
request->m_body);
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\"}");
});
m_oauth2_config.token_from_code(U("789GHI")).wait();
VERIFY_ARE_EQUAL(U("xyzzy123"), m_oauth2_config.token().access_token());
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
}
// Fetch using client key & secret in request body (x-www-form-urlencoded).
{
m_scoped.server()->next_request().then([](test_request *request)
{
VERIFY_IS_TRUE(is_application_x_www_form_urlencoded(request));
VERIFY_ARE_EQUAL(U(""), request->m_headers[header_names::authorization]);
VERIFY_ARE_EQUAL(to_body_data(
U("grant_type=authorization_code&code=789GHI&redirect_uri=https%3A%2F%2Fbar&client_id=123ABC&client_secret=456DEF")),
request->m_body);
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\"}");
});
m_oauth2_config.set_token(oauth2_token()); // Clear token.
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());
m_oauth2_config.set_http_basic_auth(false);
m_oauth2_config.token_from_code(U("789GHI")).wait();
VERIFY_ARE_EQUAL(U("xyzzy123"), m_oauth2_config.token().access_token());
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
}
}
TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_redirected_uri)
{
// Authorization code grant.
{
m_scoped.server()->next_request().then([](test_request *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\":\"test1\",\"token_type\":\"bearer\"}");
});
m_oauth2_config.set_implicit_grant(false);
m_oauth2_config.set_state(U("xyzzy"));
const web::http::uri redirected_uri(m_uri.to_string() + U("?code=sesame&state=xyzzy"));
m_oauth2_config.token_from_redirected_uri(redirected_uri).wait();
VERIFY_IS_TRUE(m_oauth2_config.token().is_valid_access_token());
VERIFY_ARE_EQUAL(m_oauth2_config.token().access_token(), U("test1"));
}
// Implicit grant.
{
m_oauth2_config.set_implicit_grant(true);
const web::http::uri redirected_uri(m_uri.to_string() + U("#access_token=abcd1234&state=xyzzy"));
m_oauth2_config.token_from_redirected_uri(redirected_uri).wait();
VERIFY_IS_TRUE(m_oauth2_config.token().is_valid_access_token());
VERIFY_ARE_EQUAL(m_oauth2_config.token().access_token(), U("abcd1234"));
}
}
TEST_FIXTURE(oauth2_test_setup, oauth2_token_from_refresh)
{
oauth2_token token(U("accessing"));
token.set_refresh_token(U("refreshing"));
m_oauth2_config.set_token(token);
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
// Verify token refresh without scope.
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=refresh_token&refresh_token=refreshing")),
request->m_body);
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\":\"ABBA\",\"refresh_token\":\"BAZ\",\"token_type\":\"bearer\"}");
});
m_oauth2_config.token_from_refresh().wait();
VERIFY_ARE_EQUAL(U("ABBA"), m_oauth2_config.token().access_token());
VERIFY_ARE_EQUAL(U("BAZ"), m_oauth2_config.token().refresh_token());
// Verify chaining refresh tokens and refresh with scope.
m_scoped.server()->next_request().then([](test_request *request)
{
VERIFY_IS_TRUE(is_application_x_www_form_urlencoded(request));
VERIFY_ARE_EQUAL(to_body_data(
U("grant_type=refresh_token&refresh_token=BAZ&scope=xyzzy")),
request->m_body);
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\":\"done\",\"token_type\":\"bearer\"}");
});
m_oauth2_config.set_scope(U("xyzzy"));
m_oauth2_config.token_from_refresh().wait();
VERIFY_ARE_EQUAL(U("done"), m_oauth2_config.token().access_token());
}
TEST_FIXTURE(oauth2_test_setup, oauth2_bearer_token)
{
m_oauth2_config.set_token(oauth2_token(U("12345678")));
http_client_config config;
// Default, bearer token in "Authorization" header (bearer_auth() == true)
{
config.set_oauth2(m_oauth2_config);
http_client client(m_uri, config);
m_scoped.server()->next_request().then([](test_request *request)
{
VERIFY_ARE_EQUAL(U("Bearer 12345678"), request->m_headers[header_names::authorization]);
VERIFY_ARE_EQUAL(U("/"), request->m_path);
request->reply(status_codes::OK);
});
http_response response = client.request(methods::GET).get();
VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
}
// Bearer token in query, default access token key (bearer_auth() == false)
{
m_oauth2_config.set_bearer_auth(false);
config.set_oauth2(m_oauth2_config);
http_client client(m_uri, config);
m_scoped.server()->next_request().then([](test_request *request)
{
VERIFY_ARE_EQUAL(U(""), request->m_headers[header_names::authorization]);
VERIFY_ARE_EQUAL(U("/?access_token=12345678"), request->m_path);
request->reply(status_codes::OK);
});
http_response response = client.request(methods::GET).get();
VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
}
// Bearer token in query, updated token, custom access token key (bearer_auth() == false)
{
m_oauth2_config.set_bearer_auth(false);
m_oauth2_config.set_access_token_key(U("open"));
m_oauth2_config.set_token(oauth2_token(U("Sesame")));
config.set_oauth2(m_oauth2_config);
http_client client(m_uri, config);
m_scoped.server()->next_request().then([](test_request *request)
{
VERIFY_ARE_EQUAL(U(""), request->m_headers[header_names::authorization]);
VERIFY_ARE_EQUAL(U("/?open=Sesame"), request->m_path);
request->reply(status_codes::OK);
});
http_response response = client.request(methods::GET).get();
VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
}
}
TEST_FIXTURE(oauth2_test_setup, oauth2_token_parsing)
{
VERIFY_IS_FALSE(m_oauth2_config.is_enabled());
// Verify reply JSON 'access_token', 'refresh_token', 'expires_in' and 'scope'.
{
m_scoped.server()->next_request().then([](test_request *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\":\"123\",\"refresh_token\":\"ABC\",\"token_type\":\"bearer\",\"expires_in\":12345678,\"scope\":\"baz\"}");
});
m_oauth2_config.token_from_code(U("")).wait();
VERIFY_ARE_EQUAL(U("123"), m_oauth2_config.token().access_token());
VERIFY_ARE_EQUAL(U("ABC"), m_oauth2_config.token().refresh_token());
VERIFY_ARE_EQUAL(12345678, m_oauth2_config.token().expires_in());
VERIFY_ARE_EQUAL(U("baz"), m_oauth2_config.token().scope());
VERIFY_IS_TRUE(m_oauth2_config.is_enabled());
}
// Verify undefined 'expires_in' and 'scope'.
{
m_scoped.server()->next_request().then([](test_request *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\":\"123\",\"token_type\":\"bearer\"}");
});
const utility::string_t test_scope(U("wally world"));
m_oauth2_config.set_scope(test_scope);
m_oauth2_config.token_from_code(U("")).wait();
VERIFY_ARE_EQUAL(oauth2_token::undefined_expiration, m_oauth2_config.token().expires_in());
VERIFY_ARE_EQUAL(test_scope, m_oauth2_config.token().scope());
}
}
} // SUITE(oauth2_tests)
}}}}
|