Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ca_bundle_file and ca_certs_dir config options #39

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Config {
bool log_full_trace_on_failure() const { return log_full_trace_on_failure_; }
std::string token_endpoint() const { return token_endpoint_; }
std::string proxy() const { return proxy_; }
std::string ca_bundle_file() const { return ca_bundle_file_; }
std::string ca_certs_dir() const { return ca_certs_dir_; }

private:
Config() = default;
Expand All @@ -50,6 +52,8 @@ class Config {
bool log_full_trace_on_failure_ = false;
std::string token_endpoint_ = "https://accounts.google.com/o/oauth2/token";
std::string proxy_ = "";
std::string ca_bundle_file_ = "";
std::string ca_certs_dir_ = "";
};

} // namespace sasl_xoauth2
Expand Down
14 changes: 13 additions & 1 deletion src/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ void SetHttpInterceptForTesting(HttpIntercept intercept) {
}

int HttpPost(const std::string &url, const std::string &data,
const std::string &proxy, long *response_code,
const std::string &proxy, const std::string &ca_bundle_file,
const std::string &ca_certs_dir, long *response_code,
std::string *response, std::string *error) {
if (s_intercept)
return s_intercept(url, data, response_code, response, error);
Expand Down Expand Up @@ -117,6 +118,17 @@ int HttpPost(const std::string &url, const std::string &data,
// Network.
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

// Certs.
if (ca_certs_dir.empty()) {
if (ca_bundle_file.empty()) {
// Use default CA location.
} else {
curl_easy_setopt(curl, CURLOPT_CAINFO, ca_bundle_file.c_str());
funilrys marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
curl_easy_setopt(curl, CURLOPT_CAPATH, ca_certs_dir.c_str());
}

// HTTP.
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_USERAGENT, kUserAgent);
Expand Down
3 changes: 2 additions & 1 deletion src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ using HttpIntercept = std::function<int(
void SetHttpInterceptForTesting(HttpIntercept intercept);

int HttpPost(const std::string &url, const std::string &data,
const std::string &proxy, long *response_code,
const std::string &proxy, const std::string &ca_bundle_file,
const std::string &ca_certs_dir, long *response_code,
std::string *response, std::string *error);

} // namespace sasl_xoauth2
Expand Down
16 changes: 14 additions & 2 deletions src/token_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ int TokenStore::Refresh() {
const std::string proxy =
(override_proxy_.empty() ? Config::Get()->proxy() : override_proxy_);

const std::string ca_bundle_file =
(override_ca_bundle_file_.empty() ? Config::Get()->ca_bundle_file()
: override_ca_bundle_file_);

const std::string ca_certs_dir =
(override_ca_certs_dir_.empty() ? Config::Get()->ca_certs_dir()
: override_ca_certs_dir_);

const std::string request =
std::string("client_id=") + client_id +
"&client_secret=" + client_secret +
Expand All @@ -115,8 +123,8 @@ int TokenStore::Refresh() {
log_->Write("TokenStore::Refresh: request: %s", request.c_str());

std::string http_error;
int err = HttpPost(token_endpoint, request, proxy, &response_code, &response,
&http_error);
int err = HttpPost(token_endpoint, request, proxy, ca_bundle_file,
ca_certs_dir, &response_code, &response, &http_error);
if (err != SASL_OK) {
log_->Write("TokenStore::Refresh: http error: %s", http_error.c_str());
return err;
Expand Down Expand Up @@ -190,6 +198,8 @@ int TokenStore::Read() {
ReadOverride(root, "client_secret", &override_client_secret_);
ReadOverride(root, "token_endpoint", &override_token_endpoint_);
ReadOverride(root, "proxy", &override_proxy_);
ReadOverride(root, "ca_bundle_file", &override_ca_bundle_file_);
ReadOverride(root, "ca_certs_dir", &override_ca_certs_dir_);

refresh_ = root["refresh_token"].asString();
if (root.isMember("access_token"))
Expand Down Expand Up @@ -224,6 +234,8 @@ int TokenStore::Write() {
WriteOverride("client_secret", override_client_secret_, &root);
WriteOverride("token_endpoint", override_token_endpoint_, &root);
WriteOverride("proxy", override_proxy_, &root);
WriteOverride("ca_bundle_file", override_ca_bundle_file_, &root);
WriteOverride("ca_certs_dir", override_ca_certs_dir_, &root);

std::ofstream file(new_path);
if (!file.good()) {
Expand Down
2 changes: 2 additions & 0 deletions src/token_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class TokenStore {
std::string override_client_secret_;
std::string override_token_endpoint_;
std::string override_proxy_;
std::string override_ca_bundle_file_;
std::string override_ca_certs_dir_;

std::string access_;
std::string refresh_;
Expand Down