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 "src/core/security/credentials.h" |
| 38 #include "src/core/security/json_token.h" |
| 39 #include "src/core/support/load_file.h" |
| 40 |
| 41 #include <grpc/support/alloc.h> |
| 42 #include <grpc/support/cmdline.h> |
| 43 #include <grpc/support/log.h> |
| 44 #include <grpc/support/slice.h> |
| 45 |
| 46 void create_jwt(const char *json_key_file_path, const char *service_url, |
| 47 const char *scope) { |
| 48 grpc_auth_json_key key; |
| 49 int ok = 0; |
| 50 char *jwt; |
| 51 gpr_slice json_key_data = gpr_load_file(json_key_file_path, 1, &ok); |
| 52 if (!ok) { |
| 53 fprintf(stderr, "Could not read %s.\n", json_key_file_path); |
| 54 exit(1); |
| 55 } |
| 56 key = grpc_auth_json_key_create_from_string( |
| 57 (const char *)GPR_SLICE_START_PTR(json_key_data)); |
| 58 gpr_slice_unref(json_key_data); |
| 59 if (!grpc_auth_json_key_is_valid(&key)) { |
| 60 fprintf(stderr, "Could not parse json key.\n"); |
| 61 exit(1); |
| 62 } |
| 63 jwt = grpc_jwt_encode_and_sign( |
| 64 &key, service_url == NULL ? GRPC_JWT_OAUTH2_AUDIENCE : service_url, |
| 65 grpc_max_auth_token_lifetime(), scope); |
| 66 grpc_auth_json_key_destruct(&key); |
| 67 if (jwt == NULL) { |
| 68 fprintf(stderr, "Could not create JWT.\n"); |
| 69 exit(1); |
| 70 } |
| 71 fprintf(stdout, "%s\n", jwt); |
| 72 gpr_free(jwt); |
| 73 } |
| 74 |
| 75 int main(int argc, char **argv) { |
| 76 char *scope = NULL; |
| 77 char *json_key_file_path = NULL; |
| 78 char *service_url = NULL; |
| 79 gpr_cmdline *cl = gpr_cmdline_create("create_jwt"); |
| 80 gpr_cmdline_add_string(cl, "json_key", "File path of the json key.", |
| 81 &json_key_file_path); |
| 82 gpr_cmdline_add_string(cl, "scope", |
| 83 "OPTIONAL Space delimited permissions. Mutually " |
| 84 "exclusive with service_url", |
| 85 &scope); |
| 86 gpr_cmdline_add_string(cl, "service_url", |
| 87 "OPTIONAL service URL. Mutually exclusive with scope.", |
| 88 &service_url); |
| 89 gpr_cmdline_parse(cl, argc, argv); |
| 90 |
| 91 if (json_key_file_path == NULL) { |
| 92 fprintf(stderr, "Missing --json_key option.\n"); |
| 93 exit(1); |
| 94 } |
| 95 if (scope != NULL) { |
| 96 if (service_url != NULL) { |
| 97 fprintf(stderr, |
| 98 "Options --scope and --service_url are mutually exclusive.\n"); |
| 99 exit(1); |
| 100 } |
| 101 } else if (service_url == NULL) { |
| 102 fprintf(stderr, "Need one of --service_url or --scope options.\n"); |
| 103 exit(1); |
| 104 } |
| 105 |
| 106 create_jwt(json_key_file_path, service_url, scope); |
| 107 |
| 108 gpr_cmdline_destroy(cl); |
| 109 return 0; |
| 110 } |
OLD | NEW |