diff options
author | tastytea | 2020-01-10 14:26:50 +0100 |
---|---|---|
committer | tastytea | 2020-01-10 14:26:50 +0100 |
commit | c3bb9a20c4c630f65120902e77a8b2fd6c711967 (patch) | |
tree | 6ce4a5c9c45d094794a1b339af59b7d0400f72a5 /src | |
parent | 1910e780b0fd3b3a57444537ab21cc263532ff86 (diff) | |
download | mastodonpp-c3bb9a20c4c630f65120902e77a8b2fd6c711967.tar mastodonpp-c3bb9a20c4c630f65120902e77a8b2fd6c711967.tar.gz mastodonpp-c3bb9a20c4c630f65120902e77a8b2fd6c711967.zip |
Send the access token.
Diffstat (limited to 'src')
-rw-r--r-- | src/connection.cpp | 5 | ||||
-rw-r--r-- | src/curl_wrapper.cpp | 27 |
2 files changed, 32 insertions, 0 deletions
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) |