• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
20 #include <grpc/grpc_security.h>
21 #include <grpc/slice.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/sync.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "absl/log/check.h"
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/iomgr/pollset.h"
30 #include "src/core/lib/security/credentials/jwt/jwt_verifier.h"
31 #include "src/core/util/crash.h"
32 #include "src/core/util/json/json_writer.h"
33 #include "test/core/test_util/cmdline.h"
34 
35 typedef struct {
36   grpc_pollset* pollset;
37   gpr_mu* mu;
38   int is_done;
39   int success;
40 } synchronizer;
41 
print_usage_and_exit(gpr_cmdline * cl,const char * argv0)42 static void print_usage_and_exit(gpr_cmdline* cl, const char* argv0) {
43   std::string usage = gpr_cmdline_usage_string(cl, argv0);
44   fprintf(stderr, "%s", usage.c_str());
45   fflush(stderr);
46   gpr_cmdline_destroy(cl);
47   exit(1);
48 }
49 
on_jwt_verification_done(void * user_data,grpc_jwt_verifier_status status,grpc_jwt_claims * claims)50 static void on_jwt_verification_done(void* user_data,
51                                      grpc_jwt_verifier_status status,
52                                      grpc_jwt_claims* claims) {
53   synchronizer* sync = static_cast<synchronizer*>(user_data);
54 
55   sync->success = (status == GRPC_JWT_VERIFIER_OK);
56   if (sync->success) {
57     CHECK_NE(claims, nullptr);
58     std::string claims_str =
59         grpc_core::JsonDump(*grpc_jwt_claims_json(claims), /*indent=*/2);
60     printf("Claims: \n\n%s\n", claims_str.c_str());
61     grpc_jwt_claims_destroy(claims);
62   } else {
63     CHECK_EQ(claims, nullptr);
64     fprintf(stderr, "Verification failed with error %s\n",
65             grpc_jwt_verifier_status_to_string(status));
66     fflush(stderr);
67   }
68 
69   gpr_mu_lock(sync->mu);
70   sync->is_done = 1;
71   GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(sync->pollset, nullptr));
72   gpr_mu_unlock(sync->mu);
73 }
74 
main(int argc,char ** argv)75 int main(int argc, char** argv) {
76   synchronizer sync;
77   grpc_jwt_verifier* verifier;
78   gpr_cmdline* cl;
79   const char* jwt = nullptr;
80   const char* aud = nullptr;
81   grpc_core::ExecCtx exec_ctx;
82 
83   grpc_init();
84   cl = gpr_cmdline_create("JWT verifier tool");
85   gpr_cmdline_add_string(cl, "jwt", "JSON web token to verify", &jwt);
86   gpr_cmdline_add_string(cl, "aud", "Audience for the JWT", &aud);
87   gpr_cmdline_parse(cl, argc, argv);
88   if (jwt == nullptr || aud == nullptr) {
89     print_usage_and_exit(cl, argv[0]);
90   }
91 
92   verifier = grpc_jwt_verifier_create(nullptr, 0);
93 
94   grpc_init();
95 
96   sync.pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
97   grpc_pollset_init(sync.pollset, &sync.mu);
98   sync.is_done = 0;
99 
100   grpc_jwt_verifier_verify(verifier, sync.pollset, jwt, aud,
101                            on_jwt_verification_done, &sync);
102 
103   gpr_mu_lock(sync.mu);
104   while (!sync.is_done) {
105     grpc_pollset_worker* worker = nullptr;
106     if (!GRPC_LOG_IF_ERROR(
107             "pollset_work",
108             grpc_pollset_work(sync.pollset, &worker,
109                               grpc_core::Timestamp::InfFuture()))) {
110       sync.is_done = true;
111     }
112     gpr_mu_unlock(sync.mu);
113     grpc_core::ExecCtx::Get()->Flush();
114     gpr_mu_lock(sync.mu);
115   }
116   gpr_mu_unlock(sync.mu);
117 
118   gpr_free(sync.pollset);
119 
120   grpc_jwt_verifier_destroy(verifier);
121 
122   gpr_cmdline_destroy(cl);
123   grpc_shutdown();
124   return !sync.success;
125 }
126