1 #include "treehub_server.h"
7 #include <boost/algorithm/string.hpp>
11 TreehubServer::TreehubServer() {
12 auth_header_.data = const_cast<char*>(auth_header_contents_.c_str());
13 auth_header_.next = &force_header_;
14 force_header_contents_ =
"x-ats-ostree-force: true";
15 force_header_.data = const_cast<char*>(force_header_contents_.c_str());
16 force_header_.next = &content_type_header_;
17 content_type_header_.data = const_cast<char*>(content_type_header_contents_.c_str());
18 content_type_header_.next =
nullptr;
21 void TreehubServer::SetToken(
const string& token) {
22 assert(auth_header_.next == &force_header_);
23 assert(force_header_.next == &content_type_header_);
24 assert(content_type_header_.next ==
nullptr);
26 auth_header_contents_ =
"Authorization: Bearer " + token;
27 auth_header_.data = const_cast<char*>(auth_header_contents_.c_str());
28 method_ = AuthMethod::kOauth2;
31 void TreehubServer::SetContentType(
const string& content_type) {
32 assert(auth_header_.next == &force_header_);
33 assert(force_header_.next == &content_type_header_);
34 assert(content_type_header_.next ==
nullptr);
36 content_type_header_contents_ = content_type;
37 content_type_header_.data = const_cast<char*>(content_type_header_contents_.c_str());
40 void TreehubServer::SetCerts(
const std::string& client_p12) {
41 method_ = AuthMethod::kTls;
42 client_p12_path_.PutContents(client_p12);
45 void TreehubServer::SetAuthBasic(
const std::string& username,
const std::string& password) {
46 method_ = AuthMethod::kBasic;
53 void TreehubServer::InjectIntoCurl(
const string& url_suffix, CURL* curl_handle,
const bool tufrepo)
const {
54 std::string url = (tufrepo ? repo_url_ : root_url_);
56 if (*url.rbegin() !=
'/' && *url_suffix.begin() !=
'/') {
58 }
else if (*url.rbegin() ==
'/' && *url_suffix.begin() ==
'/') {
59 url.erase(url.length() - 1);
62 boost::trim_if(url, boost::is_any_of(
" \t\r\n"));
64 curlEasySetoptWrapper(curl_handle, CURLOPT_URL, (url + url_suffix).c_str());
66 curlEasySetoptWrapper(curl_handle, CURLOPT_HTTPHEADER, &auth_header_);
69 if (method_ == AuthMethod::kBasic) {
70 curlEasySetoptWrapper(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
71 curlEasySetoptWrapper(curl_handle, CURLOPT_USERNAME, username_.c_str());
72 curlEasySetoptWrapper(curl_handle, CURLOPT_PASSWORD, password_.c_str());
75 if (method_ == AuthMethod::kTls) {
76 curlEasySetoptWrapper(curl_handle, CURLOPT_SSLCERT, client_p12_path_.PathString().c_str());
77 curlEasySetoptWrapper(curl_handle, CURLOPT_SSLCERTTYPE,
"P12");
78 curlEasySetoptWrapper(curl_handle, CURLOPT_SSL_VERIFYPEER, 1);
79 curlEasySetoptWrapper(curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
80 curlEasySetoptWrapper(curl_handle, CURLOPT_USE_SSL, CURLUSESSL_ALL);
83 if (!ca_certs_.empty()) {
84 curlEasySetoptWrapper(curl_handle, CURLOPT_CAINFO, ca_certs_.c_str());
85 curlEasySetoptWrapper(curl_handle, CURLOPT_CAPATH, NULL);
92 void TreehubServer::root_url(
const std::string& _root_url) {
93 root_url_ = _root_url;
94 if (root_url_.size() > 0 && root_url_[root_url_.size() - 1] !=
'/') {
95 root_url_.append(
"/");
99 void TreehubServer::repo_url(
const std::string& _repo_url) {
100 repo_url_ = _repo_url;
101 if (repo_url_.size() > 0 && repo_url_[repo_url_.size() - 1] !=
'/') {
102 repo_url_.append(
"/");