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 <grpc/credentials.h>
22 #include <grpc/grpc.h>
23 #include <grpc/grpc_security.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
27 #include <grpc/support/sync.h>
28 #include <string.h>
29
30 #include "absl/log/log.h"
31 #include "src/core/lib/iomgr/exec_ctx.h"
32 #include "src/core/lib/promise/exec_ctx_wakeup_scheduler.h"
33 #include "src/core/lib/promise/map.h"
34 #include "src/core/lib/resource_quota/resource_quota.h"
35 #include "src/core/lib/security/context/security_context.h"
36 #include "src/core/lib/security/credentials/credentials.h"
37 #include "src/core/util/crash.h"
38 #include "src/core/util/notification.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 auto arena = grpc_core::SimpleArenaAllocator()->MakeArena();
53 grpc_metadata_batch initial_metadata;
54 char* token = nullptr;
55
56 auto activity = grpc_core::MakeActivity(
57 [creds, &initial_metadata, &get_request_metadata_args]() {
58 return grpc_core::Map(
59 creds->GetRequestMetadata(
60 grpc_core::ClientMetadataHandle(
61 &initial_metadata,
62 grpc_core::Arena::PooledDeleter(nullptr)),
63 &get_request_metadata_args),
64 [](const absl::StatusOr<grpc_core::ClientMetadataHandle>& s) {
65 return s.status();
66 });
67 },
68 grpc_core::ExecCtxWakeupScheduler(),
69 [&is_done, &done, &token, &initial_metadata](absl::Status result) {
70 is_done = true;
71 if (!result.ok()) {
72 LOG(ERROR) << "Fetching token failed: " << result;
73 } else {
74 std::string buffer;
75 token = gpr_strdup(
76 std::string(
77 initial_metadata
78 .GetStringValue(GRPC_AUTHORIZATION_METADATA_KEY, &buffer)
79 .value_or(""))
80 .c_str());
81 }
82 done.Notify();
83 },
84 arena.get(), &pops);
85 grpc_core::ExecCtx::Get()->Flush();
86
87 if (grpc_core::IsEventEngineClientEnabled()) {
88 done.WaitForNotification();
89 } else {
90 gpr_mu_lock(mu);
91 while (!is_done) {
92 grpc_pollset_worker* worker = nullptr;
93 if (!GRPC_LOG_IF_ERROR(
94 "pollset_work",
95 grpc_pollset_work(grpc_polling_entity_pollset(&pops), &worker,
96 grpc_core::Timestamp::InfFuture()))) {
97 is_done = true;
98 }
99 }
100 gpr_mu_unlock(mu);
101 }
102 grpc_pollset_shutdown(
103 grpc_polling_entity_pollset(&pops),
104 GRPC_CLOSURE_CREATE([](void*, grpc_error_handle) {}, nullptr, nullptr));
105 grpc_core::ExecCtx::Get()->Flush();
106 grpc_pollset_destroy(grpc_polling_entity_pollset(&pops));
107 gpr_free(pollset);
108 return token;
109 }
110