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