oauth2_utils.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "test/core/security/oauth2_utils.h"
  19. #include <string.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/grpc_security.h>
  22. #include <grpc/slice.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/sync.h>
  26. #include "src/core/lib/iomgr/exec_ctx.h"
  27. #include "src/core/lib/promise/exec_ctx_wakeup_scheduler.h"
  28. #include "src/core/lib/promise/map.h"
  29. #include "src/core/lib/resource_quota/resource_quota.h"
  30. #include "src/core/lib/security/credentials/credentials.h"
  31. static auto* g_memory_allocator = new grpc_core::MemoryAllocator(
  32. grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
  33. "test"));
  34. char* grpc_test_fetch_oauth2_token_with_credentials(
  35. grpc_call_credentials* creds) {
  36. grpc_core::ExecCtx exec_ctx;
  37. grpc_call_credentials::GetRequestMetadataArgs get_request_metadata_args;
  38. grpc_pollset* pollset =
  39. static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  40. gpr_mu* mu = nullptr;
  41. grpc_pollset_init(pollset, &mu);
  42. auto pops = grpc_polling_entity_create_from_pollset(pollset);
  43. bool is_done = false;
  44. auto arena = grpc_core::MakeScopedArena(1024, g_memory_allocator);
  45. grpc_metadata_batch initial_metadata{arena.get()};
  46. char* token = nullptr;
  47. auto activity = grpc_core::MakeActivity(
  48. [creds, &initial_metadata, &get_request_metadata_args]() {
  49. return grpc_core::Map(
  50. creds->GetRequestMetadata(
  51. grpc_core::ClientInitialMetadata::TestOnlyWrap(
  52. &initial_metadata),
  53. &get_request_metadata_args),
  54. [](const absl::StatusOr<grpc_core::ClientInitialMetadata>& s) {
  55. return s.status();
  56. });
  57. },
  58. grpc_core::ExecCtxWakeupScheduler(),
  59. [&is_done, &token, &initial_metadata](absl::Status result) {
  60. is_done = true;
  61. if (!result.ok()) {
  62. gpr_log(GPR_ERROR, "Fetching token failed: %s",
  63. result.ToString().c_str());
  64. } else {
  65. std::string buffer;
  66. token = gpr_strdup(
  67. std::string(
  68. initial_metadata
  69. .GetStringValue(GRPC_AUTHORIZATION_METADATA_KEY, &buffer)
  70. .value_or(""))
  71. .c_str());
  72. }
  73. },
  74. arena.get(), &pops);
  75. grpc_core::ExecCtx::Get()->Flush();
  76. gpr_mu_lock(mu);
  77. while (!is_done) {
  78. grpc_pollset_worker* worker = nullptr;
  79. if (!GRPC_LOG_IF_ERROR(
  80. "pollset_work",
  81. grpc_pollset_work(grpc_polling_entity_pollset(&pops), &worker,
  82. grpc_core::Timestamp::InfFuture()))) {
  83. is_done = true;
  84. }
  85. }
  86. gpr_mu_unlock(mu);
  87. grpc_pollset_shutdown(
  88. grpc_polling_entity_pollset(&pops),
  89. GRPC_CLOSURE_CREATE([](void*, grpc_error_handle) {}, nullptr, nullptr));
  90. grpc_core::ExecCtx::Get()->Flush();
  91. grpc_pollset_destroy(grpc_polling_entity_pollset(&pops));
  92. gpr_free(pollset);
  93. return token;
  94. }