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/security/credentials/jwt/jwt_verifier.h"
30 #include "test/core/util/cmdline.h"
31
32 typedef struct {
33 grpc_pollset* pollset;
34 gpr_mu* mu;
35 int is_done;
36 int success;
37 } synchronizer;
38
print_usage_and_exit(gpr_cmdline * cl,const char * argv0)39 static void print_usage_and_exit(gpr_cmdline* cl, const char* argv0) {
40 std::string usage = gpr_cmdline_usage_string(cl, argv0);
41 fprintf(stderr, "%s", usage.c_str());
42 fflush(stderr);
43 gpr_cmdline_destroy(cl);
44 exit(1);
45 }
46
on_jwt_verification_done(void * user_data,grpc_jwt_verifier_status status,grpc_jwt_claims * claims)47 static void on_jwt_verification_done(void* user_data,
48 grpc_jwt_verifier_status status,
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());
57 grpc_jwt_claims_destroy(claims);
58 } else {
59 GPR_ASSERT(claims == nullptr);
60 fprintf(stderr, "Verification failed with error %s\n",
61 grpc_jwt_verifier_status_to_string(status));
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
main(int argc,char ** argv)71 int main(int argc, char** argv) {
72 synchronizer sync;
73 grpc_jwt_verifier* verifier;
74 gpr_cmdline* cl;
75 const char* jwt = nullptr;
76 const char* aud = nullptr;
77 grpc_core::ExecCtx exec_ctx;
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
96 grpc_jwt_verifier_verify(verifier, sync.pollset, jwt, aud,
97 on_jwt_verification_done, &sync);
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",
104 grpc_pollset_work(sync.pollset, &worker, GRPC_MILLIS_INF_FUTURE)))
105 sync.is_done = true;
106 gpr_mu_unlock(sync.mu);
107 grpc_core::ExecCtx::Get()->Flush();
108 gpr_mu_lock(sync.mu);
109 }
110 gpr_mu_unlock(sync.mu);
111
112 gpr_free(sync.pollset);
113
114 grpc_jwt_verifier_destroy(verifier);
115
116 gpr_cmdline_destroy(cl);
117 grpc_shutdown();
118 return !sync.success;
119 }
120