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