oauth2_utils.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <string.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security.h>
25 #include <grpc/slice.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
29 #include <grpc/support/sync.h>
30 
37 
39  grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
40  "test"));
41 
43  grpc_call_credentials* creds) {
45  grpc_call_credentials::GetRequestMetadataArgs get_request_metadata_args;
46 
47  grpc_pollset* pollset =
48  static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
49  gpr_mu* mu = nullptr;
50  grpc_pollset_init(pollset, &mu);
51  auto pops = grpc_polling_entity_create_from_pollset(pollset);
52  bool is_done = false;
53 
55  grpc_metadata_batch initial_metadata{arena.get()};
56  char* token = nullptr;
57 
58  auto activity = grpc_core::MakeActivity(
59  [creds, &initial_metadata, &get_request_metadata_args]() {
60  return grpc_core::Map(
61  creds->GetRequestMetadata(
63  &initial_metadata),
64  &get_request_metadata_args),
66  return s.status();
67  });
68  },
70  [&is_done, &token, &initial_metadata](absl::Status result) {
71  is_done = true;
72  if (!result.ok()) {
73  gpr_log(GPR_ERROR, "Fetching token failed: %s",
74  result.ToString().c_str());
75  } else {
77  token = gpr_strdup(
79  initial_metadata
80  .GetStringValue(GRPC_AUTHORIZATION_METADATA_KEY, &buffer)
81  .value_or(""))
82  .c_str());
83  }
84  },
85  arena.get(), &pops);
86 
88 
89  gpr_mu_lock(mu);
90  while (!is_done) {
91  grpc_pollset_worker* worker = nullptr;
92  if (!GRPC_LOG_IF_ERROR(
93  "pollset_work",
96  is_done = true;
97  }
98  }
100 
103  GRPC_CLOSURE_CREATE([](void*, grpc_error_handle) {}, nullptr, nullptr));
106  gpr_free(pollset);
107 
108  return token;
109 }
grpc_pollset_worker
struct grpc_pollset_worker grpc_pollset_worker
Definition: pollset.h:39
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc_core::MakeScopedArena
ScopedArenaPtr MakeScopedArena(size_t initial_size, MemoryAllocator *memory_allocator)
Definition: src/core/lib/resource_quota/arena.h:130
grpc_pollset_size
size_t grpc_pollset_size(void)
Definition: pollset.cc:56
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
grpc_polling_entity_pollset
grpc_pollset * grpc_polling_entity_pollset(grpc_polling_entity *pollent)
Definition: polling_entity.cc:42
grpc_call_credentials::GetRequestMetadataArgs
Definition: src/core/lib/security/credentials/credentials.h:196
log.h
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
grpc_call_credentials::GetRequestMetadata
virtual grpc_core::ArenaPromise< absl::StatusOr< grpc_core::ClientMetadataHandle > > GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata, const GetRequestMetadataArgs *args)=0
slice.h
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_call_credentials
Definition: src/core/lib/security/credentials/credentials.h:189
grpc_pollset_work
grpc_error_handle grpc_pollset_work(grpc_pollset *pollset, grpc_pollset_worker **worker, grpc_core::Timestamp deadline)
Definition: pollset.cc:45
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
GRPC_CLOSURE_CREATE
#define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler)
Definition: closure.h:160
grpc_security.h
credentials.h
grpc_pollset_init
void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu)
Definition: pollset.cc:33
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
grpc_core::MakeActivity
ActivityPtr MakeActivity(Factory promise_factory, WakeupScheduler wakeup_scheduler, OnDone on_done, Contexts &&... contexts)
Definition: activity.h:522
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
map.h
string_util.h
exec_ctx_wakeup_scheduler.h
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
worker
Definition: worker.py:1
grpc_polling_entity_create_from_pollset
grpc_polling_entity grpc_polling_entity_create_from_pollset(grpc_pollset *pollset)
Definition: polling_entity.cc:34
grpc.h
GRPC_AUTHORIZATION_METADATA_KEY
#define GRPC_AUTHORIZATION_METADATA_KEY
Definition: src/core/lib/security/credentials/credentials.h:55
oauth2_utils.h
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition: resource_quota.cc:27
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
resource_quota.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
security_context.h
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
grpc_pollset_shutdown
void grpc_pollset_shutdown(grpc_pollset *pollset, grpc_closure *closure)
Definition: pollset.cc:37
alloc.h
grpc_core::MetadataHandle< ClientMetadata >::TestOnlyWrap
static MetadataHandle TestOnlyWrap(ClientMetadata *p)
Definition: transport.h:123
exec_ctx.h
gpr_strdup
GPRAPI char * gpr_strdup(const char *src)
Definition: string.cc:39
grpc_core::Map
promise_detail::Map< Promise, Fn > Map(Promise promise, Fn fn)
Definition: src/core/lib/promise/map.h:67
grpc_core::ExecCtxWakeupScheduler
Definition: exec_ctx_wakeup_scheduler.h:29
grpc_core::Timestamp::InfFuture
static constexpr Timestamp InfFuture()
Definition: src/core/lib/gprpp/time.h:79
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_error
Definition: error_internal.h:42
grpc_metadata_batch
Definition: metadata_batch.h:1259
grpc_pollset
Definition: bm_cq_multiple_threads.cc:37
grpc_pollset_destroy
void grpc_pollset_destroy(grpc_pollset *pollset)
Definition: pollset.cc:41
sync.h
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
g_memory_allocator
static auto * g_memory_allocator
Definition: oauth2_utils.cc:38
grpc_test_fetch_oauth2_token_with_credentials
char * grpc_test_fetch_oauth2_token_with_credentials(grpc_call_credentials *creds)
Definition: oauth2_utils.cc:42


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:43