diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp index 0daf771..73c748a 100644 --- a/include/curl_wrapper.hpp +++ b/include/curl_wrapper.hpp @@ -199,6 +199,13 @@ protected: return _curl_buffer_body; } + /*! + * @brief Set OAuth 2.0 Bearer Access Token. + * + * @since 0.1.0 + */ + void set_access_token(const string_view access_token); + private: CURL *_connection; char _curl_buffer_error[CURL_ERROR_SIZE]; diff --git a/src/connection.cpp b/src/connection.cpp index 498384d..f74a816 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -30,6 +30,11 @@ Connection::Connection(Instance &instance) { CURLWrapper::set_proxy(proxy); } + + if (!_instance.get_access_token().empty()) + { + CURLWrapper::set_access_token(_instance.get_access_token()); + } } string Connection::endpoint_to_uri(const endpoint_variant &endpoint) const diff --git a/src/curl_wrapper.cpp b/src/curl_wrapper.cpp index 86d3073..d7e152f 100644 --- a/src/curl_wrapper.cpp +++ b/src/curl_wrapper.cpp @@ -166,6 +166,33 @@ answer_type CURLWrapper::make_request(const http_method &method, string uri, return answer; } +void CURLWrapper::set_access_token(const string_view access_token) +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + CURLcode code{curl_easy_setopt(_connection, CURLOPT_XOAUTH2_BEARER, + access_token.data())}; + if (code != CURLE_OK) + { + throw CURLException{code, "Could not set authorization token.", + _curl_buffer_error}; + } + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); + if (code == CURLE_NOT_BUILT_IN) // libcurl < 7.61.0. + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + code = curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + } + if (code != CURLE_OK) + { + throw CURLException{code, "Could not set authorization token.", + _curl_buffer_error}; + } + + debuglog << "Set authorization token.\n"; +} + size_t CURLWrapper::writer_body(char *data, size_t size, size_t nmemb) { if(data == nullptr)