Go to the documentation of this file.
32 #include "absl/status/status.h"
33 #include "absl/strings/str_cat.h"
34 #include "absl/strings/str_format.h"
35 #include "absl/strings/str_join.h"
36 #include "absl/strings/string_view.h"
75 return (refresh_token !=
nullptr) &&
82 const char* prop_value;
88 if (json.
type() != Json::Type::OBJECT) {
95 if (prop_value ==
nullptr ||
116 const char* json_string) {
128 if (refresh_token ==
nullptr)
return;
130 if (refresh_token->
client_id !=
nullptr) {
159 char* null_terminated_body =
nullptr;
170 null_terminated_body =
172 null_terminated_body[
response->body_length] =
'\0';
179 null_terminated_body !=
nullptr ? null_terminated_body :
"");
183 const char* access_token =
nullptr;
184 const char* token_type =
nullptr;
185 const char* expires_in =
nullptr;
186 Json::Object::const_iterator
it;
196 if (json.
type() != Json::Type::OBJECT) {
203 it->second.type() != Json::Type::STRING) {
208 access_token =
it->second.string_value().c_str();
211 it->second.type() != Json::Type::STRING) {
216 token_type =
it->second.string_value().c_str();
219 it->second.type() != Json::Type::NUMBER) {
224 expires_in =
it->second.string_value().c_str();
255 &
r->response, &access_token_value, &token_lifetime)
273 while (pending_request !=
nullptr) {
281 "Error occurred when fetching oauth2 token.", &
error, 1);
285 pending_request->
done.store(
true, std::memory_order_release);
290 pending_request = pending_request->
next;
310 if (cached_access_token_value.
has_value()) {
321 auto pending_request =
322 grpc_core::MakeRefCounted<grpc_oauth2_pending_get_request_metadata>();
323 pending_request->pollent = grpc_core::GetContext<grpc_polling_entity>();
330 bool start_fetch =
false;
344 if (!pending_request->done.load(std::memory_order_acquire)) {
347 return std::move(pending_request->result);
359 return "OAuth2TokenFetcherCredentials";
373 class grpc_compute_engine_token_fetcher_credentials
376 grpc_compute_engine_token_fetcher_credentials() =
default;
377 ~grpc_compute_engine_token_fetcher_credentials()
override =
default;
385 const_cast<char*
>(
"Google")};
401 grpc_schedule_on_exec_ctx),
410 "GoogleComputeEngineTokenFetcherCredentials{%s}",
423 GRPC_API_TRACE(
"grpc_compute_engine_credentials_create(reserved=%p)", 1,
427 grpc_compute_engine_token_fetcher_credentials>()
445 const_cast<char*
>(
"Content-Type"),
446 const_cast<char*
>(
"application/x-www-form-urlencoded")};
454 request.body =
const_cast<char*
>(body.c_str());
455 request.body_length = body.size();
466 grpc_schedule_on_exec_ctx),
473 : refresh_token_(refresh_token) {}
482 return grpc_core::MakeRefCounted<grpc_google_refresh_token_credentials>(
500 return "<Invalid json token>";
503 "{\n type: %s\n client_id: %s\n client_secret: "
504 "<redacted>\n refresh_token: <redacted>\n}",
509 const char* json_refresh_token,
void* reserved) {
514 "grpc_refresh_token_credentials_create(json_refresh_token=%s, "
531 void MaybeAddToBody(
const char* field_name,
const char*
field,
532 std::vector<std::string>* body) {
533 if (
field ==
nullptr || strlen(
field) == 0)
return;
547 class StsTokenFetcherCredentials
550 StsTokenFetcherCredentials(URI sts_url,
564 "StsTokenFetcherCredentials{Path:%s,Authority:%s,%s}",
sts_url_.path(),
578 response_cb(metadata_req,
err);
583 const_cast<char*
>(
"Content-Type"),
584 const_cast<char*
>(
"application/x-www-form-urlencoded")};
590 RefCountedPtr<grpc_channel_credentials> http_request_creds;
592 http_request_creds = RefCountedPtr<grpc_channel_credentials>(
600 grpc_schedule_on_exec_ctx),
608 std::vector<std::string> body_parts;
613 auto cleanup = [&body, &body_length, &body_parts, &subject_token,
614 &actor_token, &
err]() {
618 *body_length = body_str.size();
631 MaybeAddToBody(
"resource",
resource_.get(), &body_parts);
632 MaybeAddToBody(
"audience",
audience_.get(), &body_parts);
633 MaybeAddToBody(
"scope",
scope_.get(), &body_parts);
665 std::vector<grpc_error_handle> error_list;
669 :
options->token_exchange_service_uri);
673 sts_url.
status().ToString())));
674 }
else if (sts_url->
scheme() !=
"https" && sts_url->
scheme() !=
"http") {
676 "Invalid URI scheme, must be https to http."));
678 if (
options->subject_token_path ==
nullptr ||
679 strlen(
options->subject_token_path) == 0) {
681 "subject_token needs to be specified"));
683 if (
options->subject_token_type ==
nullptr ||
684 strlen(
options->subject_token_type) == 0) {
686 "subject_token_type needs to be specified"));
688 if (error_list.empty()) {
692 "Invalid STS Credentials Options", &error_list);
708 sts_url.
status().ToString().c_str());
711 return grpc_core::MakeRefCounted<grpc_core::StsTokenFetcherCredentials>(
736 const char* access_token)
737 : access_token_value_(
grpc_core::Slice::FromCopiedString(
745 const char* access_token,
void* reserved) {
747 "grpc_access_token_credentials_create(access_token=<redacted>, "
751 return grpc_core::MakeRefCounted<grpc_access_token_credentials>(access_token)
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Status InvalidArgumentError(absl::string_view message)
static grpc_core::UniqueTypeName Type()
virtual Waker MakeNonOwningWaker()=0
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
RefCountedPtr< T > MakeRefCounted(Args &&... args)
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
return memset(p, 0, total)
grpc_auth_refresh_token refresh_token_
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
~grpc_google_refresh_token_credentials() override
#define GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH
#define GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING
static Activity * current()
grpc_pollset_set * grpc_pollset_set_create()
grpc_core::RefCountedPtr< grpc_call_credentials > grpc_refresh_token_credentials_create_from_auth_refresh_token(grpc_auth_refresh_token refresh_token)
GPRAPI void gpr_free(void *ptr)
grpc_access_token_credentials(const char *access_token)
const Object & object_value() const
void fetch_oauth2(grpc_credentials_metadata_request *req, grpc_polling_entity *pollent, grpc_iomgr_cb_func cb, grpc_core::Timestamp deadline) override
GPRAPI void * gpr_malloc(size_t size)
#define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count)
std::string debug_string() override
grpc_core::ArenaPromise< absl::StatusOr< grpc_core::ClientMetadataHandle > > GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata, const GetRequestMetadataArgs *args) override
grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json(const Json &json)
const char * grpc_json_get_string_property(const grpc_core::Json &json, const char *prop_name, grpc_error_handle *error)
absl::StatusOr< URI > ValidateStsCredentialsOptions(const grpc_sts_credentials_options *options)
#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
grpc_call_credentials * grpc_google_compute_engine_credentials_create(void *reserved)
RefCountedPtr< grpc_channel_credentials > CreateHttpRequestSSLCredentials()
static absl::StatusOr< URI > Parse(absl::string_view uri_text)
grpc_call_credentials * grpc_access_token_credentials_create(const char *access_token, void *reserved)
#define GRPC_TRACE_FLAG_ENABLED(f)
void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token *refresh_token)
Destructs the object.
#define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list)
grpc_core::ArenaPromise< absl::StatusOr< grpc_core::ClientMetadataHandle > > GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata, const GetRequestMetadataArgs *args) override
#define GRPC_AUTH_JSON_TYPE_AUTHORIZED_USER
UniquePtr< char > actor_token_path_
UniquePtr< char > audience_
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static Slice FromCopiedString(const char *s)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
grpc_credentials_status grpc_oauth2_token_fetcher_credentials_parse_server_response(const grpc_http_response *response, absl::optional< grpc_core::Slice > *token_value, grpc_core::Duration *token_lifetime)
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
grpc_pollset_set * grpc_polling_entity_pollset_set(grpc_polling_entity *pollent)
constexpr bool has_value() const noexcept
def c_str(s, encoding='ascii')
JSON (JavaScript Object Notation).
#define GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GPRAPI void gpr_mu_init(gpr_mu *mu)
void on_http_response(grpc_credentials_metadata_request *r, grpc_error_handle error)
grpc_core::OrphanablePtr< grpc_core::HttpRequest > http_request_
grpc_google_refresh_token_credentials(grpc_auth_refresh_token refresh_token)
#define GRPC_STS_POST_MINIMAL_BODY_FORMAT_STRING
grpc_core::UniqueTypeName type() const override
void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set)
grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string(const char *json_string)
#define GRPC_AUTHORIZATION_METADATA_KEY
struct absl::base_internal::@2940::AllocList::Header header
#define GRPC_COMPUTE_ENGINE_METADATA_TOKEN_PATH
std::string debug_string() override
GRPCAPI grpc_channel_credentials * grpc_insecure_credentials_create()
gpr_timespec token_expiration_
#define GRPC_SLICE_START_PTR(slice)
void grpc_polling_entity_del_from_pollset_set(grpc_polling_entity *pollent, grpc_pollset_set *pss_dst)
#define GRPC_GOOGLE_OAUTH2_SERVICE_HOST
GPRAPI grpc_slice grpc_empty_slice(void)
~grpc_oauth2_token_fetcher_credentials() override
GPRAPI void gpr_mu_lock(gpr_mu *mu)
bool Parse(FlagOpFn op, absl::string_view text, void *dst, std::string *error)
bool grpc_copy_json_string_property(const grpc_core::Json &json, const char *prop_name, char **copied_value)
grpc_polling_entity pollent_
grpc_oauth2_pending_get_request_metadata * pending_requests_
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
absl::optional< grpc_core::Slice > access_token_value_
bool token_fetch_pending_
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
#define GRPC_SLICE_LENGTH(slice)
UniquePtr< char > subject_token_type_
#define GRPC_COMPUTE_ENGINE_METADATA_HOST
UniquePtr< char > resource_
GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type)
static std::string create_loggable_refresh_token(grpc_auth_refresh_token *token)
const std::string & scheme() const
ABSL_NAMESPACE_BEGIN Time Now()
ABSL_MUST_USE_RESULT bool ok() const
grpc_core::TraceFlag grpc_api_trace(false, "api")
#define GRPC_ERROR_REF(err)
const FieldDescriptor * field
static OrphanablePtr< HttpRequest > Get(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
UniquePtr< char > actor_token_type_
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
std::string grpc_error_std_string(grpc_error_handle error)
gpr_timespec as_timespec() const
const grpc_core::Slice access_token_value_
static absl::StatusOr< URI > Create(std::string scheme, std::string authority, std::string path, std::vector< QueryParam > query_parameter_pairs, std::string fragment)
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
virtual void fetch_oauth2(grpc_credentials_metadata_request *req, grpc_polling_entity *pollent, grpc_iomgr_cb_func cb, grpc_core::Timestamp deadline)=0
std::unique_ptr< T, Deleter > OrphanablePtr
void(* grpc_iomgr_cb_func)(void *arg, grpc_error_handle error)
void grpc_polling_entity_add_to_pollset_set(grpc_polling_entity *pollent, grpc_pollset_set *pss_dst)
OrphanablePtr< HttpRequest > http_request_
static constexpr Duration Seconds(int64_t seconds)
std::string debug_string() override
grpc_closure http_post_cb_closure_
static void on_oauth2_token_fetcher_http_response(void *user_data, grpc_error_handle error)
#define GRPC_ERROR_UNREF(err)
grpc_core::UniqueTypeName type() const override
GPRAPI char * gpr_strdup(const char *src)
promise_detail::Immediate< T > Immediate(T value)
grpc_closure http_post_cb_closure_
grpc_oauth2_token_fetcher_credentials()
grpc_polling_entity grpc_polling_entity_create_from_pollset_set(grpc_pollset_set *pollset_set)
absl::Status grpc_error_to_absl_status(grpc_error_handle error)
grpc_call_credentials * grpc_google_refresh_token_credentials_create(const char *json_refresh_token, void *reserved)
grpc_call_credentials * grpc_sts_credentials_create(const grpc_sts_credentials_options *options, void *reserved)
UniquePtr< char > requested_token_type_
int grpc_auth_refresh_token_is_valid(const grpc_auth_refresh_token *refresh_token)
Returns 1 if the object is valid, 0 otherwise.
UniquePtr< char > subject_token_path_
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
#define GRPC_API_TRACE(fmt, nargs, args)
void grpc_slice_unref_internal(const grpc_slice &slice)
grpc_polling_entity pollent_
const Status & status() const &
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
#define GRPC_ERROR_IS_NONE(err)
#define GRPC_AUTH_JSON_TYPE_INVALID
grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:43