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