xref: /aosp_15_r20/external/grpc-grpc/test/core/security/oauth2_utils.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 #include "test/core/security/oauth2_utils.h"
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>
28 #include <grpc/support/string_util.h>
29 #include <grpc/support/sync.h>
30 
31 #include "src/core/lib/gprpp/crash.h"
32 #include "src/core/lib/gprpp/notification.h"
33 #include "src/core/lib/iomgr/exec_ctx.h"
34 #include "src/core/lib/promise/exec_ctx_wakeup_scheduler.h"
35 #include "src/core/lib/promise/map.h"
36 #include "src/core/lib/resource_quota/resource_quota.h"
37 #include "src/core/lib/security/context/security_context.h"
38 #include "src/core/lib/security/credentials/credentials.h"
39 
grpc_test_fetch_oauth2_token_with_credentials(grpc_call_credentials * creds)40 char* grpc_test_fetch_oauth2_token_with_credentials(
41     grpc_call_credentials* creds) {
42   grpc_core::ExecCtx exec_ctx;
43   grpc_call_credentials::GetRequestMetadataArgs get_request_metadata_args;
44   // TODO(hork): rm once GetRequestMetadata does not depend on pollsets.
45   grpc_pollset* pollset =
46       static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
47   gpr_mu* mu = nullptr;
48   grpc_pollset_init(pollset, &mu);
49   auto pops = grpc_polling_entity_create_from_pollset(pollset);
50   bool is_done = false;
51   grpc_core::Notification done;
52   grpc_core::MemoryAllocator memory_allocator =
53       grpc_core::MemoryAllocator(grpc_core::ResourceQuota::Default()
54                                      ->memory_quota()
55                                      ->CreateMemoryAllocator("test"));
56   auto arena = grpc_core::MakeScopedArena(1024, &memory_allocator);
57   grpc_metadata_batch initial_metadata;
58   char* token = nullptr;
59 
60   auto activity = grpc_core::MakeActivity(
61       [creds, &initial_metadata, &get_request_metadata_args]() {
62         return grpc_core::Map(
63             creds->GetRequestMetadata(
64                 grpc_core::ClientMetadataHandle(
65                     &initial_metadata,
66                     grpc_core::Arena::PooledDeleter(nullptr)),
67                 &get_request_metadata_args),
68             [](const absl::StatusOr<grpc_core::ClientMetadataHandle>& s) {
69               return s.status();
70             });
71       },
72       grpc_core::ExecCtxWakeupScheduler(),
73       [&is_done, &done, &token, &initial_metadata](absl::Status result) {
74         is_done = true;
75         if (!result.ok()) {
76           gpr_log(GPR_ERROR, "Fetching token failed: %s",
77                   result.ToString().c_str());
78         } else {
79           std::string buffer;
80           token = gpr_strdup(
81               std::string(
82                   initial_metadata
83                       .GetStringValue(GRPC_AUTHORIZATION_METADATA_KEY, &buffer)
84                       .value_or(""))
85                   .c_str());
86         }
87         done.Notify();
88       },
89       arena.get(), &pops);
90   grpc_core::ExecCtx::Get()->Flush();
91 
92   if (grpc_core::IsEventEngineClientEnabled()) {
93     done.WaitForNotification();
94   } else {
95     gpr_mu_lock(mu);
96     while (!is_done) {
97       grpc_pollset_worker* worker = nullptr;
98       if (!GRPC_LOG_IF_ERROR(
99               "pollset_work",
100               grpc_pollset_work(grpc_polling_entity_pollset(&pops), &worker,
101                                 grpc_core::Timestamp::InfFuture()))) {
102         is_done = true;
103       }
104     }
105     gpr_mu_unlock(mu);
106   }
107   grpc_pollset_shutdown(
108       grpc_polling_entity_pollset(&pops),
109       GRPC_CLOSURE_CREATE([](void*, grpc_error_handle) {}, nullptr, nullptr));
110   grpc_core::ExecCtx::Get()->Flush();
111   grpc_pollset_destroy(grpc_polling_entity_pollset(&pops));
112   gpr_free(pollset);
113   return token;
114 }
115