blob: aaf0e7f6b18b293d74d1d08e279ac26ec49d6d9b [file] [log] [blame]
Julien Boeuffeca1bf2015-06-22 16:46:20 +02001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Julien Boeuffeca1bf2015-06-22 16:46:20 +02004 * 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
Julien Boeuffeca1bf2015-06-22 16:46:20 +020037#include <grpc/grpc.h>
38#include <grpc/grpc_security.h>
Craig Tiller28b72422016-10-26 21:15:29 -070039#include <grpc/slice.h>
Julien Boeuffeca1bf2015-06-22 16:46:20 +020040#include <grpc/support/alloc.h>
41#include <grpc/support/cmdline.h>
42#include <grpc/support/log.h>
Julien Boeuffeca1bf2015-06-22 16:46:20 +020043#include <grpc/support/sync.h>
44
Julien Boeuf8ca294e2016-05-02 14:56:30 -070045#include "src/core/lib/security/credentials/jwt/jwt_verifier.h"
Craig Tiller69b093b2016-02-25 19:04:07 -080046
Craig Tillera82950e2015-09-22 12:33:20 -070047typedef struct {
Craig Tiller69b093b2016-02-25 19:04:07 -080048 grpc_pollset *pollset;
49 gpr_mu *mu;
Julien Boeuffeca1bf2015-06-22 16:46:20 +020050 int is_done;
51 int success;
52} synchronizer;
53
Craig Tillera82950e2015-09-22 12:33:20 -070054static 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);
Julien Boeuffeca1bf2015-06-22 16:46:20 +020060}
61
Craig Tiller3cf79222016-11-14 08:02:45 -080062static void on_jwt_verification_done(grpc_exec_ctx *exec_ctx, void *user_data,
Craig Tillera82950e2015-09-22 12:33:20 -070063 grpc_jwt_verifier_status status,
64 grpc_jwt_claims *claims) {
Julien Boeuffeca1bf2015-06-22 16:46:20 +020065 synchronizer *sync = user_data;
66
67 sync->success = (status == GRPC_JWT_VERIFIER_OK);
Craig Tillera82950e2015-09-22 12:33:20 -070068 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);
Craig Tiller3cf79222016-11-14 08:02:45 -080075 grpc_jwt_claims_destroy(exec_ctx, claims);
Craig Tillera82950e2015-09-22 12:33:20 -070076 } else {
77 GPR_ASSERT(claims == NULL);
78 fprintf(stderr, "Verification failed with error %s\n",
79 grpc_jwt_verifier_status_to_string(status));
80 }
Julien Boeuffeca1bf2015-06-22 16:46:20 +020081
Craig Tiller69b093b2016-02-25 19:04:07 -080082 gpr_mu_lock(sync->mu);
Julien Boeuffeca1bf2015-06-22 16:46:20 +020083 sync->is_done = 1;
Craig Tiller1aee5362016-05-07 11:26:50 -070084 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(sync->pollset, NULL));
Craig Tiller69b093b2016-02-25 19:04:07 -080085 gpr_mu_unlock(sync->mu);
Julien Boeuffeca1bf2015-06-22 16:46:20 +020086}
87
Craig Tillera82950e2015-09-22 12:33:20 -070088int main(int argc, char **argv) {
Julien Boeuffeca1bf2015-06-22 16:46:20 +020089 synchronizer sync;
90 grpc_jwt_verifier *verifier;
91 gpr_cmdline *cl;
92 char *jwt = NULL;
93 char *aud = NULL;
Craig Tillerf5768a62015-09-22 10:54:34 -070094 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Julien Boeuffeca1bf2015-06-22 16:46:20 +020095
Julien Boeuf964d7bb2016-11-17 16:59:48 -080096 grpc_init();
Craig Tillera82950e2015-09-22 12:33:20 -070097 cl = gpr_cmdline_create("JWT verifier tool");
98 gpr_cmdline_add_string(cl, "jwt", "JSON web token to verify", &jwt);
99 gpr_cmdline_add_string(cl, "aud", "Audience for the JWT", &aud);
100 gpr_cmdline_parse(cl, argc, argv);
101 if (jwt == NULL || aud == NULL) {
102 print_usage_and_exit(cl, argv[0]);
103 }
Julien Boeuffeca1bf2015-06-22 16:46:20 +0200104
Craig Tillera82950e2015-09-22 12:33:20 -0700105 verifier = grpc_jwt_verifier_create(NULL, 0);
Julien Boeuffeca1bf2015-06-22 16:46:20 +0200106
Craig Tillera82950e2015-09-22 12:33:20 -0700107 grpc_init();
Julien Boeuffeca1bf2015-06-22 16:46:20 +0200108
Craig Tiller69b093b2016-02-25 19:04:07 -0800109 sync.pollset = gpr_malloc(grpc_pollset_size());
110 grpc_pollset_init(sync.pollset, &sync.mu);
Julien Boeuffeca1bf2015-06-22 16:46:20 +0200111 sync.is_done = 0;
112
Craig Tiller69b093b2016-02-25 19:04:07 -0800113 grpc_jwt_verifier_verify(&exec_ctx, verifier, sync.pollset, jwt, aud,
Craig Tillera82950e2015-09-22 12:33:20 -0700114 on_jwt_verification_done, &sync);
Julien Boeuffeca1bf2015-06-22 16:46:20 +0200115
Craig Tiller69b093b2016-02-25 19:04:07 -0800116 gpr_mu_lock(sync.mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700117 while (!sync.is_done) {
Craig Tiller3633ce42016-02-18 08:08:53 -0800118 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700119 if (!GRPC_LOG_IF_ERROR(
120 "pollset_work",
121 grpc_pollset_work(&exec_ctx, sync.pollset, &worker,
122 gpr_now(GPR_CLOCK_MONOTONIC),
123 gpr_inf_future(GPR_CLOCK_MONOTONIC))))
124 sync.is_done = true;
Craig Tiller69b093b2016-02-25 19:04:07 -0800125 gpr_mu_unlock(sync.mu);
Craig Tiller9e5ac1b2017-02-14 22:25:50 -0800126 grpc_exec_ctx_flush(&exec_ctx);
Craig Tiller69b093b2016-02-25 19:04:07 -0800127 gpr_mu_lock(sync.mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700128 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800129 gpr_mu_unlock(sync.mu);
130
131 gpr_free(sync.pollset);
Julien Boeuffeca1bf2015-06-22 16:46:20 +0200132
Craig Tiller9e5ac1b2017-02-14 22:25:50 -0800133 grpc_jwt_verifier_destroy(&exec_ctx, verifier);
134 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700135 gpr_cmdline_destroy(cl);
Julien Boeuf964d7bb2016-11-17 16:59:48 -0800136 grpc_shutdown();
Julien Boeuffeca1bf2015-06-22 16:46:20 +0200137 return !sync.success;
138}