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 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
28 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_URL, (server_ +
"/token").c_str());
29 if (!ca_certs_.empty()) {
30 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_CAINFO, ca_certs_.c_str());
31 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_CAPATH, NULL);
34 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
35 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_USERNAME, client_id_.c_str());
36 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_PASSWORD, client_secret_.c_str());
37 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_POST, 1);
38 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_COPYPOSTFIELDS,
"grant_type=client_credentials");
41 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, &curl_handle_write_sstream);
42 curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA, &body);
44 curl_easy_perform(curl_handle.get());
47 curl_easy_getinfo(curl_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
52 token_ = pt.get(
"access_token",
"");
53 LOG_TRACE <<
"Got OAuth2 access token:" << token_;
54 return AuthenticationResult::kSuccess;
55 }
catch (
const json_parser_error &e) {
57 return AuthenticationResult::kFailure;
60 return AuthenticationResult::kFailure;