create_jwt.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/slice.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include "src/core/lib/iomgr/load_file.h"
  24. #include "src/core/lib/security/credentials/jwt/jwt_credentials.h"
  25. #include "test/core/util/cmdline.h"
  26. void create_jwt(const char* json_key_file_path, const char* service_url,
  27. const char* scope) {
  28. grpc_auth_json_key key;
  29. char* jwt;
  30. grpc_slice json_key_data;
  31. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  32. "load_file", grpc_load_file(json_key_file_path, 1, &json_key_data)));
  33. key = grpc_auth_json_key_create_from_string(
  34. reinterpret_cast<const char*> GRPC_SLICE_START_PTR(json_key_data));
  35. grpc_slice_unref(json_key_data);
  36. if (!grpc_auth_json_key_is_valid(&key)) {
  37. fprintf(stderr, "Could not parse json key.\n");
  38. fflush(stderr);
  39. exit(1);
  40. }
  41. jwt = grpc_jwt_encode_and_sign(
  42. &key, service_url == nullptr ? GRPC_JWT_OAUTH2_AUDIENCE : service_url,
  43. grpc_max_auth_token_lifetime(), scope);
  44. grpc_auth_json_key_destruct(&key);
  45. if (jwt == nullptr) {
  46. fprintf(stderr, "Could not create JWT.\n");
  47. fflush(stderr);
  48. exit(1);
  49. }
  50. fprintf(stdout, "%s\n", jwt);
  51. gpr_free(jwt);
  52. }
  53. int main(int argc, char** argv) {
  54. const char* scope = nullptr;
  55. const char* json_key_file_path = nullptr;
  56. const char* service_url = nullptr;
  57. grpc_init();
  58. gpr_cmdline* cl = gpr_cmdline_create("create_jwt");
  59. gpr_cmdline_add_string(cl, "json_key", "File path of the json key.",
  60. &json_key_file_path);
  61. gpr_cmdline_add_string(cl, "scope",
  62. "OPTIONAL Space delimited permissions. Mutually "
  63. "exclusive with service_url",
  64. &scope);
  65. gpr_cmdline_add_string(cl, "service_url",
  66. "OPTIONAL service URL. Mutually exclusive with scope.",
  67. &service_url);
  68. gpr_cmdline_parse(cl, argc, argv);
  69. if (json_key_file_path == nullptr) {
  70. fprintf(stderr, "Missing --json_key option.\n");
  71. fflush(stderr);
  72. exit(1);
  73. }
  74. if (scope != nullptr) {
  75. if (service_url != nullptr) {
  76. fprintf(stderr,
  77. "Options --scope and --service_url are mutually exclusive.\n");
  78. fflush(stderr);
  79. exit(1);
  80. }
  81. } else if (service_url == nullptr) {
  82. fprintf(stderr, "Need one of --service_url or --scope options.\n");
  83. fflush(stderr);
  84. exit(1);
  85. }
  86. create_jwt(json_key_file_path, service_url, scope);
  87. gpr_cmdline_destroy(cl);
  88. grpc_shutdown();
  89. return 0;
  90. }