verify_jwt.cc
Go to the documentation of this file.
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 <stdio.h>
20 #include <string.h>
21 
22 #include <grpc/grpc.h>
23 #include <grpc/grpc_security.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/sync.h>
28 
30 #include "test/core/util/cmdline.h"
31 
32 typedef struct {
34  gpr_mu* mu;
35  int is_done;
36  int success;
37 } synchronizer;
38 
39 static void print_usage_and_exit(gpr_cmdline* cl, const char* argv0) {
41  fprintf(stderr, "%s", usage.c_str());
42  fflush(stderr);
44  exit(1);
45 }
46 
47 static void on_jwt_verification_done(void* user_data,
49  grpc_jwt_claims* claims) {
50  synchronizer* sync = static_cast<synchronizer*>(user_data);
51 
52  sync->success = (status == GRPC_JWT_VERIFIER_OK);
53  if (sync->success) {
54  GPR_ASSERT(claims != nullptr);
55  std::string claims_str = grpc_jwt_claims_json(claims)->Dump(/*indent=*/2);
56  printf("Claims: \n\n%s\n", claims_str.c_str());
58  } else {
59  GPR_ASSERT(claims == nullptr);
60  fprintf(stderr, "Verification failed with error %s\n",
62  fflush(stderr);
63  }
64 
65  gpr_mu_lock(sync->mu);
66  sync->is_done = 1;
67  GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(sync->pollset, nullptr));
68  gpr_mu_unlock(sync->mu);
69 }
70 
71 int main(int argc, char** argv) {
72  synchronizer sync;
74  gpr_cmdline* cl;
75  const char* jwt = nullptr;
76  const char* aud = nullptr;
78 
79  grpc_init();
80  cl = gpr_cmdline_create("JWT verifier tool");
81  gpr_cmdline_add_string(cl, "jwt", "JSON web token to verify", &jwt);
82  gpr_cmdline_add_string(cl, "aud", "Audience for the JWT", &aud);
83  gpr_cmdline_parse(cl, argc, argv);
84  if (jwt == nullptr || aud == nullptr) {
85  print_usage_and_exit(cl, argv[0]);
86  }
87 
88  verifier = grpc_jwt_verifier_create(nullptr, 0);
89 
90  grpc_init();
91 
92  sync.pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
93  grpc_pollset_init(sync.pollset, &sync.mu);
94  sync.is_done = 0;
95 
98 
99  gpr_mu_lock(sync.mu);
100  while (!sync.is_done) {
101  grpc_pollset_worker* worker = nullptr;
102  if (!GRPC_LOG_IF_ERROR(
103  "pollset_work",
106  sync.is_done = true;
107  }
108  gpr_mu_unlock(sync.mu);
110  gpr_mu_lock(sync.mu);
111  }
112  gpr_mu_unlock(sync.mu);
113 
114  gpr_free(sync.pollset);
115 
117 
119  grpc_shutdown();
120  return !sync.success;
121 }
grpc_pollset_worker
struct grpc_pollset_worker grpc_pollset_worker
Definition: pollset.h:39
grpc_pollset_size
size_t grpc_pollset_size(void)
Definition: pollset.cc:56
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
grpc_jwt_verifier_verify
void grpc_jwt_verifier_verify(grpc_jwt_verifier *verifier, grpc_pollset *pollset, const char *jwt, const char *audience, grpc_jwt_verification_done_cb cb, void *user_data)
Definition: jwt_verifier.cc:880
log.h
GRPC_JWT_VERIFIER_OK
@ GRPC_JWT_VERIFIER_OK
Definition: jwt_verifier.h:42
gpr_cmdline_usage_string
std::string gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0)
Definition: cmdline.cc:151
slice.h
string.h
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_cmdline_destroy
void gpr_cmdline_destroy(gpr_cmdline *cl)
Definition: cmdline.cc:79
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
synchronizer::pollset
grpc_pollset * pollset
Definition: verify_jwt.cc:33
status
absl::Status status
Definition: rls.cc:251
grpc_pollset_work
grpc_error_handle grpc_pollset_work(grpc_pollset *pollset, grpc_pollset_worker **worker, grpc_core::Timestamp deadline)
Definition: pollset.cc:45
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc_security.h
verifier
static void verifier(grpc_server *server, grpc_completion_queue *cq, void *)
Definition: badreq.cc:31
grpc_jwt_verifier_status_to_string
const char * grpc_jwt_verifier_status_to_string(grpc_jwt_verifier_status status)
Definition: jwt_verifier.cc:73
grpc_jwt_claims_destroy
void grpc_jwt_claims_destroy(grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:219
grpc_pollset_init
void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu)
Definition: pollset.cc:33
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
grpc_jwt_verifier_status
grpc_jwt_verifier_status
Definition: jwt_verifier.h:41
jwt_verifier.h
main
int main(int argc, char **argv)
Definition: verify_jwt.cc:71
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
worker
Definition: worker.py:1
gpr_cmdline_add_string
void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help, const char **value)
Definition: cmdline.cc:115
grpc.h
grpc_jwt_claims_json
const Json * grpc_jwt_claims_json(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:224
synchronizer::is_done
bool is_done
Definition: print_google_default_creds_token.cc:38
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
grpc_pollset_kick
grpc_error_handle grpc_pollset_kick(grpc_pollset *pollset, grpc_pollset_worker *specific_worker)
Definition: pollset.cc:51
print_usage_and_exit
static void print_usage_and_exit(gpr_cmdline *cl, const char *argv0)
Definition: verify_jwt.cc:39
grpc_core::ExecCtx
Definition: exec_ctx.h:97
synchronizer::mu
gpr_mu * mu
Definition: print_google_default_creds_token.cc:36
synchronizer::success
int success
Definition: verify_jwt.cc:36
bloaty::usage
const char usage[]
Definition: bloaty.cc:1843
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_jwt_verifier_create
grpc_jwt_verifier * grpc_jwt_verifier_create(const grpc_jwt_verifier_email_domain_key_url_mapping *mappings, size_t num_mappings)
Definition: jwt_verifier.cc:925
synchronizer
Definition: print_google_default_creds_token.cc:35
alloc.h
cmdline.h
on_jwt_verification_done
static void on_jwt_verification_done(void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims *claims)
Definition: verify_jwt.cc:47
grpc_jwt_verifier
Definition: jwt_verifier.cc:422
gpr_cmdline_create
gpr_cmdline * gpr_cmdline_create(const char *description)
Definition: cmdline.cc:66
synchronizer::is_done
int is_done
Definition: verify_jwt.cc:35
gpr_cmdline
Definition: cmdline.cc:48
grpc_jwt_claims
Definition: jwt_verifier.cc:206
grpc_core::Timestamp::InfFuture
static constexpr Timestamp InfFuture()
Definition: src/core/lib/gprpp/time.h:79
grpc_jwt_verifier_destroy
void grpc_jwt_verifier_destroy(grpc_jwt_verifier *v)
Definition: jwt_verifier.cc:947
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_pollset
Definition: bm_cq_multiple_threads.cc:37
sync.h
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
gpr_cmdline_parse
int gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv)
Definition: cmdline.cc:309


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:52