| 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/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 |
|
| 38 |
static auto* g_memory_allocator = new grpc_core::MemoryAllocator( |
| 39 |
grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator( |
| 40 |
"test")); |
| 41 |
|
| 42 |
char* grpc_test_fetch_oauth2_token_with_credentials( |
| 43 |
grpc_call_credentials* creds) { |
| 44 |
grpc_core::ExecCtx exec_ctx; |
| 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 |
|
| 54 |
auto arena = grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 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( |
| 62 |
grpc_core::ClientMetadataHandle( |
| 63 |
&initial_metadata, |
| 64 |
grpc_core::Arena::PooledDeleter(nullptr)), |
| 65 |
&get_request_metadata_args), |
| 66 |
[](const absl::StatusOr<grpc_core::ClientMetadataHandle>& s) { |
| 67 |
return s.status(); |
| 68 |
}); |
| 69 |
}, |
| 70 |
grpc_core::ExecCtxWakeupScheduler(), |
| 71 |
[&is_done, &token, &initial_metadata](absl::Status result) { |
| 72 |
is_done = true; |
| 73 |
if (!result.ok()) { |
| 74 |
gpr_log(GPR_ERROR, "Fetching token failed: %s", |
| 75 |
result.ToString().c_str()); |
| 76 |
} else { |
| 77 |
std::string buffer; |
| 78 |
token = gpr_strdup( |
| 79 |
std::string( |
| 80 |
initial_metadata |
| 81 |
.GetStringValue(GRPC_AUTHORIZATION_METADATA_KEY, &buffer) |
| 82 |
.value_or("")) |
| 83 |
.c_str()); |
| 84 |
} |
| 85 |
}, |
| 86 |
arena.get(), &pops); |
| 87 |
|
| 88 |
grpc_core::ExecCtx::Get()->Flush(); |
| 89 |
|
| 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 |
|
| 109 |
return token; |
| 110 |
} |