5 #include <boost/property_tree/json_parser.hpp>
6 #include <boost/property_tree/ptree.hpp>
8 #include "logging/logging.h"
10 #include "utilities/utils.h"
12 using boost::property_tree::ptree;
13 using boost::property_tree::json_parser::json_parser_error;
14 using std::stringstream;
19 size_t curl_handle_write_sstream(
void *buffer,
size_t size,
size_t nmemb,
void *userp) {
20 auto *body =
static_cast<stringstream *
>(userp);
21 body->write(
static_cast<const char *
>(buffer),
static_cast<std::streamsize
>(size * nmemb));
27 std::string token_suffix =
"/token";
28 std::string post_data =
"grant_type=client_credentials";
29 auto use_cognito =
false;
30 if (server_.length() >= token_suffix.length()) {
31 use_cognito = (0 == server_.compare(server_.length() - token_suffix.length(), token_suffix.length(), token_suffix));
34 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
46 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_URL, (server_).c_str());
48 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_URL, (server_ + token_suffix).c_str());
50 if (!ca_certs_.empty()) {
51 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_CAINFO, ca_certs_.c_str());
52 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_CAPATH, NULL);
55 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
56 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_USERNAME, client_id_.c_str());
57 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_PASSWORD, client_secret_.c_str());
58 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_POST, 1);
60 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_COPYPOSTFIELDS, (post_data +
"&scope=" + scope_).c_str());
62 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_COPYPOSTFIELDS, post_data.c_str());
66 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, &curl_handle_write_sstream);
67 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA, &body);
69 curl_easy_perform(curl_handle.get());
72 curl_easy_getinfo(curl_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
77 token_ = pt.get(
"access_token",
"");
78 LOG_TRACE <<
"Got OAuth2 access token:" << token_;
79 return AuthenticationResult::kSuccess;
80 }
catch (
const json_parser_error &e) {
82 return AuthenticationResult::kFailure;
85 return AuthenticationResult::kFailure;