| 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/slice.h> |
| 23 |
#include <grpc/support/alloc.h> |
| 24 |
#include <grpc/support/log.h> |
| 25 |
|
| 26 |
#include "src/core/lib/iomgr/load_file.h" |
| 27 |
#include "src/core/lib/security/credentials/jwt/jwt_credentials.h" |
| 28 |
#include "test/core/util/cmdline.h" |
| 29 |
|
| 30 |
void create_jwt(const char* json_key_file_path, const char* service_url, |
| 31 |
const char* scope) { |
| 32 |
grpc_auth_json_key key; |
| 33 |
char* jwt; |
| 34 |
grpc_slice json_key_data; |
| 35 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 36 |
"load_file", grpc_load_file(json_key_file_path, 1, &json_key_data))); |
| 37 |
key = grpc_auth_json_key_create_from_string( |
| 38 |
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(json_key_data)); |
| 39 |
grpc_slice_unref(json_key_data); |
| 40 |
if (!grpc_auth_json_key_is_valid(&key)) { |
| 41 |
fprintf(stderr, "Could not parse json key.\n"); |
| 42 |
fflush(stderr); |
| 43 |
exit(1); |
| 44 |
} |
| 45 |
jwt = grpc_jwt_encode_and_sign( |
| 46 |
&key, service_url == nullptr ? GRPC_JWT_OAUTH2_AUDIENCE : service_url, |
| 47 |
grpc_max_auth_token_lifetime(), scope); |
| 48 |
grpc_auth_json_key_destruct(&key); |
| 49 |
if (jwt == nullptr) { |
| 50 |
fprintf(stderr, "Could not create JWT.\n"); |
| 51 |
fflush(stderr); |
| 52 |
exit(1); |
| 53 |
} |
| 54 |
fprintf(stdout, "%s\n", jwt); |
| 55 |
gpr_free(jwt); |
| 56 |
} |
| 57 |
|
| 58 |
int main(int argc, char** argv) { |
| 59 |
const char* scope = nullptr; |
| 60 |
const char* json_key_file_path = nullptr; |
| 61 |
const char* service_url = nullptr; |
| 62 |
grpc_init(); |
| 63 |
gpr_cmdline* cl = gpr_cmdline_create("create_jwt"); |
| 64 |
gpr_cmdline_add_string(cl, "json_key", "File path of the json key.", |
| 65 |
&json_key_file_path); |
| 66 |
gpr_cmdline_add_string(cl, "scope", |
| 67 |
"OPTIONAL Space delimited permissions. Mutually " |
| 68 |
"exclusive with service_url", |
| 69 |
&scope); |
| 70 |
gpr_cmdline_add_string(cl, "service_url", |
| 71 |
"OPTIONAL service URL. Mutually exclusive with scope.", |
| 72 |
&service_url); |
| 73 |
gpr_cmdline_parse(cl, argc, argv); |
| 74 |
|
| 75 |
if (json_key_file_path == nullptr) { |
| 76 |
fprintf(stderr, "Missing --json_key option.\n"); |
| 77 |
fflush(stderr); |
| 78 |
exit(1); |
| 79 |
} |
| 80 |
if (scope != nullptr) { |
| 81 |
if (service_url != nullptr) { |
| 82 |
fprintf(stderr, |
| 83 |
"Options --scope and --service_url are mutually exclusive.\n"); |
| 84 |
fflush(stderr); |
| 85 |
exit(1); |
| 86 |
} |
| 87 |
} else if (service_url == nullptr) { |
| 88 |
fprintf(stderr, "Need one of --service_url or --scope options.\n"); |
| 89 |
fflush(stderr); |
| 90 |
exit(1); |
| 91 |
} |
| 92 |
|
| 93 |
create_jwt(json_key_file_path, service_url, scope); |
| 94 |
|
| 95 |
gpr_cmdline_destroy(cl); |
| 96 |
grpc_shutdown(); |
| 97 |
return 0; |
| 98 |
} |