Go to the documentation of this file.
28 #include "absl/status/status.h"
29 #include "absl/status/statusor.h"
30 #include "absl/strings/match.h"
31 #include "absl/strings/str_format.h"
32 #include "absl/strings/str_join.h"
33 #include "absl/strings/str_split.h"
34 #include "absl/strings/strip.h"
35 #include "absl/time/clock.h"
36 #include "absl/time/time.h"
54 #define EXTERNAL_ACCOUNT_CREDENTIALS_GRANT_TYPE \
55 "urn:ietf:params:oauth:grant-type:token-exchange"
56 #define EXTERNAL_ACCOUNT_CREDENTIALS_REQUESTED_TOKEN_TYPE \
57 "urn:ietf:params:oauth:token-type:access_token"
58 #define GOOGLE_CLOUD_PLATFORM_DEFAULT_SCOPE \
59 "https://www.googleapis.com/auth/cloud-platform"
66 const char* hex =
"0123456789ABCDEF";
68 result.reserve(s.length());
70 if ((c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'Z') ||
71 (c >=
'a' && c <=
'z') || c ==
'-' || c ==
'_' || c ==
'!' ||
72 c ==
'\'' || c ==
'(' || c ==
')' || c ==
'*' || c ==
'~' || c ==
'.') {
76 result.push_back(hex[
static_cast<unsigned char>(c) >> 4]);
77 result.push_back(hex[
static_cast<unsigned char>(c) & 15]);
90 std::pair<absl::string_view, absl::string_view> workforce_pools_split_result =
94 std::pair<absl::string_view, absl::string_view> providers_split_result =
103 const Json& json, std::vector<std::string> scopes,
110 "Invalid json to construct credentials options.");
137 "audience field must be a string.");
140 options.audience =
it->second.string_value();
144 "subject_token_type field not present.");
149 "subject_token_type field must be a string.");
152 options.subject_token_type =
it->second.string_value();
153 it = json.
object_value().find(
"service_account_impersonation_url");
155 options.service_account_impersonation_url =
it->second.string_value();
165 "token_url field must be a string.");
168 options.token_url =
it->second.string_value();
171 options.token_info_url =
it->second.string_value();
176 "credential_source field not present.");
182 options.quota_project_id =
it->second.string_value();
186 options.client_id =
it->second.string_value();
190 options.client_secret =
it->second.string_value();
194 if (MatchWorkforcePoolAudience(
options.audience)) {
195 options.workforce_pool_user_project =
it->second.string_value();
198 "workforce_pool_user_project should not be set for non-workforce "
204 if (
options.credential_source.object_value().find(
"environment_id") !=
205 options.credential_source.object_value().end()) {
206 creds = MakeRefCounted<AwsExternalAccountCredentials>(
208 }
else if (
options.credential_source.object_value().find(
"file") !=
209 options.credential_source.object_value().end()) {
210 creds = MakeRefCounted<FileExternalAccountCredentials>(
212 }
else if (
options.credential_source.object_value().find(
"url") !=
213 options.credential_source.object_value().end()) {
214 creds = MakeRefCounted<UrlExternalAccountCredentials>(
218 "Invalid options credential source to create "
219 "ExternalAccountCredentials.");
231 if (scopes.empty()) {
286 uri.
status().ToString())));
314 std::vector<std::string> body_parts;
315 body_parts.push_back(
321 "requested_token_type=%s",
325 body_parts.push_back(
334 addtional_options_json_object[
"userProject"] =
337 Json addtional_options_json(
std::move(addtional_options_json_object));
339 "options=%s", UrlEncode(addtional_options_json.
Dump()).c_str()));
341 request.body =
const_cast<char*
>(body.c_str());
342 request.body_length = body.size();
348 if (uri->scheme() ==
"http") {
402 "Invalid token exchange response.", &
error, 1));
410 "Missing or invalid access_token in %s.", response_body)));
418 "Invalid service account impersonation url: %s. Error: %s",
435 request.body =
const_cast<char*
>(body.c_str());
436 request.body_length = body.size();
443 if (uri->scheme() ==
"http") {
476 "Invalid service account impersonation response.", &
error, 1));
484 "Missing or invalid accessToken in %s.", response_body)));
492 "Missing or invalid expireTime in %s.", response_body)));
499 "Invalid expire time of service account impersonation response."));
504 "{\"access_token\":\"%s\",\"expires_in\":%d,\"token_type\":\"Bearer\"}",
505 access_token, expire_in);
540 const char* json_string,
const char* scopes_string) {
545 "External account credentials creation failed. Error: %s.",
550 std::vector<std::string> scopes =
absl::StrSplit(scopes_string,
',');
556 "External account credentials creation failed. Error: %s.",
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty, absl::string_view > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
strings_internal::MaxSplitsImpl< typename strings_internal::SelectDelimiter< Delimiter >::type > MaxSplits(Delimiter delimiter, int limit)
return memset(p, 0, total)
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
~ExternalAccountCredentials() override
#define EXTERNAL_ACCOUNT_CREDENTIALS_GRANT_TYPE
static void OnImpersenateServiceAccount(void *arg, grpc_error_handle error)
static void OnExchangeToken(void *arg, grpc_error_handle error)
grpc_iomgr_cb_func response_cb_
grpc_polling_entity * pollent
GPRAPI void gpr_free(void *ptr)
const Object & object_value() const
GPRAPI void * gpr_malloc(size_t size)
#define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count)
std::vector< std::string > scopes_
static struct test_ctx ctx
#define GRPC_LOG_IF_ERROR(what, error)
static OrphanablePtr< HttpRequest > Post(URI uri, const grpc_channel_args *args, grpc_polling_entity *pollent, const grpc_http_request *request, Timestamp deadline, grpc_closure *on_done, grpc_http_response *response, RefCountedPtr< grpc_channel_credentials > channel_creds) GRPC_MUST_USE_RESULT
RefCountedPtr< grpc_channel_credentials > CreateHttpRequestSSLCredentials()
virtual void RetrieveSubjectToken(HTTPRequestContext *ctx, const Options &options, std::function< void(std::string, grpc_error_handle)> cb)=0
static absl::StatusOr< URI > Parse(absl::string_view uri_text)
#define GRPC_AUTH_JSON_TYPE_EXTERNAL_ACCOUNT
void grpc_http_request_destroy(grpc_http_request *request)
void ImpersenateServiceAccount()
grpc_call_credentials * grpc_external_account_credentials_create(const char *json_string, const char *scopes_string)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
def c_str(s, encoding='ascii')
JSON (JavaScript Object Notation).
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
bool ParseTime(absl::string_view format, absl::string_view input, absl::Time *time, std::string *err)
ABSL_NAMESPACE_BEGIN const ABSL_DLL char RFC3339_full[]
std::string debug_string() override
GRPCAPI grpc_channel_credentials * grpc_insecure_credentials_create()
ExternalAccountCredentials(Options options, std::vector< std::string > scopes)
ABSL_NAMESPACE_BEGIN bool StrContains(absl::string_view haystack, absl::string_view needle) noexcept
void grpc_http_response_destroy(grpc_http_response *response)
void FinishTokenFetch(grpc_error_handle error)
HTTPRequestContext * ctx_
char * grpc_base64_encode(const void *vdata, size_t data_size, int url_safe, int multiline)
std::string service_account_impersonation_url
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
void fetch_oauth2(grpc_credentials_metadata_request *req, grpc_polling_entity *pollent, grpc_iomgr_cb_func cb, Timestamp deadline) override
static Json Parse(absl::string_view json_str, grpc_error_handle *error)
grpc_http_response response
#define GOOGLE_CLOUD_PLATFORM_DEFAULT_SCOPE
constexpr Duration Seconds(T n)
std::map< std::string, Json > Object
void OnRetrieveSubjectTokenInternal(absl::string_view subject_token, grpc_error_handle error)
ABSL_NAMESPACE_BEGIN Time Now()
ABSL_MUST_USE_RESULT bool ok() const
std::string debug_string() override
#define GRPC_ERROR_REF(err)
OrphanablePtr< HttpRequest > http_request_
DebugStringOptions options_
std::string grpc_error_std_string(grpc_error_handle error)
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
void(* grpc_iomgr_cb_func)(void *arg, grpc_error_handle error)
#define GRPC_ERROR_UNREF(err)
#define EXTERNAL_ACCOUNT_CREDENTIALS_REQUESTED_TOKEN_TYPE
GPRAPI char * gpr_strdup(const char *src)
grpc_credentials_metadata_request * metadata_req_
void OnImpersenateServiceAccountInternal(grpc_error_handle error)
std::string workforce_pool_user_project
void ExchangeToken(absl::string_view subject_token)
static RefCountedPtr< ExternalAccountCredentials > Create(const Json &json, std::vector< std::string > scopes, grpc_error_handle *error)
std::string Dump(int indent=0) const
OPENSSL_EXPORT pem_password_cb * cb
std::string subject_token_type
const Status & status() const &
std::string client_secret
void OnExchangeTokenInternal(grpc_error_handle error)
#define GRPC_ERROR_IS_NONE(err)
#define GRPC_AUTH_JSON_TYPE_INVALID
ABSL_NAMESPACE_BEGIN bool ConsumePrefix(absl::string_view *str, absl::string_view expected)
grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:21