verify_jwt.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "src/core/lib/security/credentials/jwt/jwt_verifier.h"
  27. #include "test/core/util/cmdline.h"
  28. typedef struct {
  29. grpc_pollset* pollset;
  30. gpr_mu* mu;
  31. int is_done;
  32. int success;
  33. } synchronizer;
  34. static void print_usage_and_exit(gpr_cmdline* cl, const char* argv0) {
  35. std::string usage = gpr_cmdline_usage_string(cl, argv0);
  36. fprintf(stderr, "%s", usage.c_str());
  37. fflush(stderr);
  38. gpr_cmdline_destroy(cl);
  39. exit(1);
  40. }
  41. static void on_jwt_verification_done(void* user_data,
  42. grpc_jwt_verifier_status status,
  43. grpc_jwt_claims* claims) {
  44. synchronizer* sync = static_cast<synchronizer*>(user_data);
  45. sync->success = (status == GRPC_JWT_VERIFIER_OK);
  46. if (sync->success) {
  47. GPR_ASSERT(claims != nullptr);
  48. std::string claims_str = grpc_jwt_claims_json(claims)->Dump(/*indent=*/2);
  49. printf("Claims: \n\n%s\n", claims_str.c_str());
  50. grpc_jwt_claims_destroy(claims);
  51. } else {
  52. GPR_ASSERT(claims == nullptr);
  53. fprintf(stderr, "Verification failed with error %s\n",
  54. grpc_jwt_verifier_status_to_string(status));
  55. fflush(stderr);
  56. }
  57. gpr_mu_lock(sync->mu);
  58. sync->is_done = 1;
  59. GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(sync->pollset, nullptr));
  60. gpr_mu_unlock(sync->mu);
  61. }
  62. int main(int argc, char** argv) {
  63. synchronizer sync;
  64. grpc_jwt_verifier* verifier;
  65. gpr_cmdline* cl;
  66. const char* jwt = nullptr;
  67. const char* aud = nullptr;
  68. grpc_core::ExecCtx exec_ctx;
  69. grpc_init();
  70. cl = gpr_cmdline_create("JWT verifier tool");
  71. gpr_cmdline_add_string(cl, "jwt", "JSON web token to verify", &jwt);
  72. gpr_cmdline_add_string(cl, "aud", "Audience for the JWT", &aud);
  73. gpr_cmdline_parse(cl, argc, argv);
  74. if (jwt == nullptr || aud == nullptr) {
  75. print_usage_and_exit(cl, argv[0]);
  76. }
  77. verifier = grpc_jwt_verifier_create(nullptr, 0);
  78. grpc_init();
  79. sync.pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
  80. grpc_pollset_init(sync.pollset, &sync.mu);
  81. sync.is_done = 0;
  82. grpc_jwt_verifier_verify(verifier, sync.pollset, jwt, aud,
  83. on_jwt_verification_done, &sync);
  84. gpr_mu_lock(sync.mu);
  85. while (!sync.is_done) {
  86. grpc_pollset_worker* worker = nullptr;
  87. if (!GRPC_LOG_IF_ERROR(
  88. "pollset_work",
  89. grpc_pollset_work(sync.pollset, &worker,
  90. grpc_core::Timestamp::InfFuture()))) {
  91. sync.is_done = true;
  92. }
  93. gpr_mu_unlock(sync.mu);
  94. grpc_core::ExecCtx::Get()->Flush();
  95. gpr_mu_lock(sync.mu);
  96. }
  97. gpr_mu_unlock(sync.mu);
  98. gpr_free(sync.pollset);
  99. grpc_jwt_verifier_destroy(verifier);
  100. gpr_cmdline_destroy(cl);
  101. grpc_shutdown();
  102. return !sync.success;
  103. }