OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <stdio.h> |
| 35 #include <string.h> |
| 36 |
| 37 #include <grpc/grpc.h> |
| 38 #include <grpc/grpc_security.h> |
| 39 #include <grpc/support/alloc.h> |
| 40 #include <grpc/support/cmdline.h> |
| 41 #include <grpc/support/log.h> |
| 42 #include <grpc/support/slice.h> |
| 43 #include <grpc/support/sync.h> |
| 44 |
| 45 #include "src/core/security/jwt_verifier.h" |
| 46 |
| 47 typedef struct { |
| 48 grpc_pollset *pollset; |
| 49 gpr_mu *mu; |
| 50 int is_done; |
| 51 int success; |
| 52 } synchronizer; |
| 53 |
| 54 static void print_usage_and_exit(gpr_cmdline *cl, const char *argv0) { |
| 55 char *usage = gpr_cmdline_usage_string(cl, argv0); |
| 56 fprintf(stderr, "%s", usage); |
| 57 gpr_free(usage); |
| 58 gpr_cmdline_destroy(cl); |
| 59 exit(1); |
| 60 } |
| 61 |
| 62 static void on_jwt_verification_done(void *user_data, |
| 63 grpc_jwt_verifier_status status, |
| 64 grpc_jwt_claims *claims) { |
| 65 synchronizer *sync = user_data; |
| 66 |
| 67 sync->success = (status == GRPC_JWT_VERIFIER_OK); |
| 68 if (sync->success) { |
| 69 char *claims_str; |
| 70 GPR_ASSERT(claims != NULL); |
| 71 claims_str = |
| 72 grpc_json_dump_to_string((grpc_json *)grpc_jwt_claims_json(claims), 2); |
| 73 printf("Claims: \n\n%s\n", claims_str); |
| 74 gpr_free(claims_str); |
| 75 grpc_jwt_claims_destroy(claims); |
| 76 } else { |
| 77 GPR_ASSERT(claims == NULL); |
| 78 fprintf(stderr, "Verification failed with error %s\n", |
| 79 grpc_jwt_verifier_status_to_string(status)); |
| 80 } |
| 81 |
| 82 gpr_mu_lock(sync->mu); |
| 83 sync->is_done = 1; |
| 84 grpc_pollset_kick(sync->pollset, NULL); |
| 85 gpr_mu_unlock(sync->mu); |
| 86 } |
| 87 |
| 88 int main(int argc, char **argv) { |
| 89 synchronizer sync; |
| 90 grpc_jwt_verifier *verifier; |
| 91 gpr_cmdline *cl; |
| 92 char *jwt = NULL; |
| 93 char *aud = NULL; |
| 94 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 95 |
| 96 cl = gpr_cmdline_create("JWT verifier tool"); |
| 97 gpr_cmdline_add_string(cl, "jwt", "JSON web token to verify", &jwt); |
| 98 gpr_cmdline_add_string(cl, "aud", "Audience for the JWT", &aud); |
| 99 gpr_cmdline_parse(cl, argc, argv); |
| 100 if (jwt == NULL || aud == NULL) { |
| 101 print_usage_and_exit(cl, argv[0]); |
| 102 } |
| 103 |
| 104 verifier = grpc_jwt_verifier_create(NULL, 0); |
| 105 |
| 106 grpc_init(); |
| 107 |
| 108 sync.pollset = gpr_malloc(grpc_pollset_size()); |
| 109 grpc_pollset_init(sync.pollset, &sync.mu); |
| 110 sync.is_done = 0; |
| 111 |
| 112 grpc_jwt_verifier_verify(&exec_ctx, verifier, sync.pollset, jwt, aud, |
| 113 on_jwt_verification_done, &sync); |
| 114 |
| 115 gpr_mu_lock(sync.mu); |
| 116 while (!sync.is_done) { |
| 117 grpc_pollset_worker *worker = NULL; |
| 118 grpc_pollset_work(&exec_ctx, sync.pollset, &worker, |
| 119 gpr_now(GPR_CLOCK_MONOTONIC), |
| 120 gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 121 gpr_mu_unlock(sync.mu); |
| 122 grpc_exec_ctx_finish(&exec_ctx); |
| 123 gpr_mu_lock(sync.mu); |
| 124 } |
| 125 gpr_mu_unlock(sync.mu); |
| 126 |
| 127 gpr_free(sync.pollset); |
| 128 |
| 129 grpc_jwt_verifier_destroy(verifier); |
| 130 gpr_cmdline_destroy(cl); |
| 131 return !sync.success; |
| 132 } |
OLD | NEW |