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 char* usage = gpr_cmdline_usage_string(cl, argv0);
41 fprintf(stderr, "%s", usage);
42 fflush(stderr);
43 gpr_free(usage);
44 gpr_cmdline_destroy(cl);
45 exit(1);
46 }
47
on_jwt_verification_done(void * user_data,grpc_jwt_verifier_status status,grpc_jwt_claims * claims)48 static void on_jwt_verification_done(void* user_data,
49 grpc_jwt_verifier_status status,
50 grpc_jwt_claims* claims) {
51 synchronizer* sync = static_cast<synchronizer*>(user_data);
52
53 sync->success = (status == GRPC_JWT_VERIFIER_OK);
54 if (sync->success) {
55 char* claims_str;
56 GPR_ASSERT(claims != nullptr);
57 claims_str = grpc_json_dump_to_string(
58 const_cast<grpc_json*>(grpc_jwt_claims_json(claims)), 2);
59 printf("Claims: \n\n%s\n", claims_str);
60 gpr_free(claims_str);
61 grpc_jwt_claims_destroy(claims);
62 } else {
63 GPR_ASSERT(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, GRPC_MILLIS_INF_FUTURE)))
109 sync.is_done = true;
110 gpr_mu_unlock(sync.mu);
111 grpc_core::ExecCtx::Get()->Flush();
112 gpr_mu_lock(sync.mu);
113 }
114 gpr_mu_unlock(sync.mu);
115
116 gpr_free(sync.pollset);
117
118 grpc_jwt_verifier_destroy(verifier);
119
120 gpr_cmdline_destroy(cl);
121 grpc_shutdown();
122 return !sync.success;
123 }
124