fetch_oauth2.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <stdio.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 <grpcpp/security/credentials.h>
  27. #include "src/core/lib/iomgr/error.h"
  28. #include "src/core/lib/iomgr/load_file.h"
  29. #include "src/core/lib/security/credentials/credentials.h"
  30. #include "src/core/lib/security/util/json_util.h"
  31. #include "src/cpp/client/secure_credentials.h"
  32. #include "test/core/security/oauth2_utils.h"
  33. #include "test/core/util/cmdline.h"
  34. static grpc_call_credentials* create_sts_creds(const char* json_file_path) {
  35. grpc::experimental::StsCredentialsOptions options;
  36. if (strlen(json_file_path) == 0) {
  37. auto status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
  38. if (!status.ok()) {
  39. gpr_log(GPR_ERROR, "%s", status.error_message().c_str());
  40. return nullptr;
  41. }
  42. } else {
  43. grpc_slice sts_options_slice;
  44. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  45. "load_file", grpc_load_file(json_file_path, 1, &sts_options_slice)));
  46. auto status = grpc::experimental::StsCredentialsOptionsFromJson(
  47. reinterpret_cast<const char*>(GRPC_SLICE_START_PTR(sts_options_slice)),
  48. &options);
  49. gpr_slice_unref(sts_options_slice);
  50. if (!status.ok()) {
  51. gpr_log(GPR_ERROR, "%s", status.error_message().c_str());
  52. return nullptr;
  53. }
  54. }
  55. grpc_sts_credentials_options opts =
  56. grpc::experimental::StsCredentialsCppToCoreOptions(options);
  57. grpc_call_credentials* result = grpc_sts_credentials_create(&opts, nullptr);
  58. return result;
  59. }
  60. static grpc_call_credentials* create_refresh_token_creds(
  61. const char* json_refresh_token_file_path) {
  62. grpc_slice refresh_token;
  63. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  64. "load_file",
  65. grpc_load_file(json_refresh_token_file_path, 1, &refresh_token)));
  66. grpc_call_credentials* result = grpc_google_refresh_token_credentials_create(
  67. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(refresh_token),
  68. nullptr);
  69. gpr_slice_unref(refresh_token);
  70. return result;
  71. }
  72. int main(int argc, char** argv) {
  73. grpc_call_credentials* creds = nullptr;
  74. const char* json_sts_options_file_path = nullptr;
  75. const char* json_refresh_token_file_path = nullptr;
  76. char* token = nullptr;
  77. int use_gce = 0;
  78. gpr_cmdline* cl = gpr_cmdline_create("fetch_oauth2");
  79. gpr_cmdline_add_string(cl, "json_refresh_token",
  80. "File path of the json refresh token.",
  81. &json_refresh_token_file_path);
  82. gpr_cmdline_add_string(
  83. cl, "json_sts_options",
  84. "File path of the json sts options. If the path is empty, the program "
  85. "will attempt to use the $STS_CREDENTIALS environment variable to access "
  86. "a file containing the options.",
  87. &json_sts_options_file_path);
  88. gpr_cmdline_add_flag(
  89. cl, "gce",
  90. "Get a token from the GCE metadata server (only works in GCE).",
  91. &use_gce);
  92. gpr_cmdline_parse(cl, argc, argv);
  93. grpc_init();
  94. if (json_sts_options_file_path != nullptr &&
  95. json_refresh_token_file_path != nullptr) {
  96. gpr_log(
  97. GPR_ERROR,
  98. "--json_sts_options and --json_refresh_token are mutually exclusive.");
  99. exit(1);
  100. }
  101. if (use_gce) {
  102. if (json_sts_options_file_path != nullptr ||
  103. json_refresh_token_file_path != nullptr) {
  104. gpr_log(GPR_INFO,
  105. "Ignoring json refresh token or sts options to get a token from "
  106. "the GCE metadata server.");
  107. }
  108. creds = grpc_google_compute_engine_credentials_create(nullptr);
  109. if (creds == nullptr) {
  110. gpr_log(GPR_ERROR, "Could not create gce credentials.");
  111. exit(1);
  112. }
  113. } else if (json_refresh_token_file_path != nullptr) {
  114. creds = create_refresh_token_creds(json_refresh_token_file_path);
  115. if (creds == nullptr) {
  116. gpr_log(GPR_ERROR,
  117. "Could not create refresh token creds. %s does probably not "
  118. "contain a valid json refresh token.",
  119. json_refresh_token_file_path);
  120. exit(1);
  121. }
  122. } else if (json_sts_options_file_path != nullptr) {
  123. creds = create_sts_creds(json_sts_options_file_path);
  124. if (creds == nullptr) {
  125. gpr_log(GPR_ERROR,
  126. "Could not create sts creds. %s does probably not contain a "
  127. "valid json for sts options.",
  128. json_sts_options_file_path);
  129. exit(1);
  130. }
  131. } else {
  132. gpr_log(
  133. GPR_ERROR,
  134. "Missing --gce, --json_sts_options, or --json_refresh_token option.");
  135. exit(1);
  136. }
  137. GPR_ASSERT(creds != nullptr);
  138. token = grpc_test_fetch_oauth2_token_with_credentials(creds);
  139. if (token != nullptr) {
  140. printf("Got token: %s.\n", token);
  141. gpr_free(token);
  142. }
  143. grpc_call_credentials_release(creds);
  144. gpr_cmdline_destroy(cl);
  145. grpc_shutdown();
  146. return 0;
  147. }