15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_SERVICE_ACCOUNT_CREDENTIALS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_SERVICE_ACCOUNT_CREDENTIALS_H
18 #include "google/cloud/storage/internal/curl_request_builder.h"
19 #include "google/cloud/storage/internal/openssl_util.h"
20 #include "google/cloud/storage/oauth2/credential_constants.h"
21 #include "google/cloud/storage/oauth2/credentials.h"
22 #include "google/cloud/storage/oauth2/refreshing_credentials_wrapper.h"
23 #include "google/cloud/storage/version.h"
24 #include "google/cloud/internal/sha256_hash.h"
25 #include "google/cloud/optional.h"
26 #include "google/cloud/status_or.h"
27 #include "absl/types/optional.h"
29 #include <condition_variable>
50 absl::optional<std::set<std::string>>
scopes;
57 std::string
const& content, std::string
const& source,
70 std::string
const& source,
77 storage::internal::HttpResponse
const& response,
78 std::chrono::system_clock::time_point now);
91 std::chrono::system_clock::time_point now);
99 std::string
const& payload,
100 std::string
const& pem_contents);
107 std::chrono::system_clock::time_point now);
134 template <
typename HttpRequestBuilderType =
135 storage::internal::CurlRequestBuilder,
136 typename ClockType = std::chrono::system_clock>
143 : info_(std::move(info)),
148 std::unique_lock<std::mutex> lock(mu_);
149 return refreshing_creds_.AuthorizationHeader(clock_.now(),
150 [
this] {
return Refresh(); });
166 std::string
const& blob)
const override {
167 if (signing_account.has_value() &&
170 "The current_credentials cannot sign blobs for " +
171 signing_account.value()
);
173 return internal::SignStringWithPem(blob, info_
.private_key,
182 HttpRequestBuilderType builder(
184 storage::internal::GetDefaultCurlHandleFactory(options_));
185 builder.AddHeader(
"Content-Type: application/x-www-form-urlencoded");
188 std::string grant_type(
"grant_type=");
190 builder.MakeEscapedString(
"urn:ietf:params:oauth:grant-type:jwt-bearer")
194 CreateServiceAccountRefreshPayload(info_, grant_type, clock_.now());
195 auto response = std::move(builder).BuildRequest().MakeRequest(payload);
196 if (!response)
return std::move(response).status();
197 if (response->status_code >= 300)
return AsStatus(*response);
198 return ParseServiceAccountRefreshResponse(*response, clock_.now());
203 mutable std::mutex mu_;