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