create_jwt.cc
Go to the documentation of this file.
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 
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) {
33  char* jwt;
34  grpc_slice json_key_data;
36  "load_file", grpc_load_file(json_key_file_path, 1, &json_key_data)));
38  reinterpret_cast<const char*> GRPC_SLICE_START_PTR(json_key_data));
39  grpc_slice_unref(json_key_data);
41  fprintf(stderr, "Could not parse json key.\n");
42  fflush(stderr);
43  exit(1);
44  }
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 
96  grpc_shutdown();
97  return 0;
98 }
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
main
int main(int argc, char **argv)
Definition: create_jwt.cc:58
log.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
load_file.h
demumble_test.stdout
stdout
Definition: demumble_test.py:38
slice.h
string.h
grpc_jwt_encode_and_sign
char * grpc_jwt_encode_and_sign(const grpc_auth_json_key *json_key, const char *audience, gpr_timespec token_lifetime, const char *scope)
Definition: json_token.cc:266
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_cmdline_destroy
void gpr_cmdline_destroy(gpr_cmdline *cl)
Definition: cmdline.cc:79
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc_auth_json_key_destruct
void grpc_auth_json_key_destruct(grpc_auth_json_key *json_key)
Definition: json_token.cc:134
service_url
std::string service_url
Definition: call_creds_util.cc:39
grpc_auth_json_key_create_from_string
grpc_auth_json_key grpc_auth_json_key_create_from_string(const char *json_string)
Definition: json_token.cc:126
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_auth_json_key_is_valid
int grpc_auth_json_key_is_valid(const grpc_auth_json_key *json_key)
Definition: json_token.cc:66
grpc_max_auth_token_lifetime
GRPCAPI gpr_timespec grpc_max_auth_token_lifetime(void)
Definition: json_token.cc:48
create_jwt
void create_jwt(const char *json_key_file_path, const char *service_url, const char *scope)
Definition: create_jwt.cc:30
gpr_cmdline_add_string
void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help, const char **value)
Definition: cmdline.cc:115
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
key
const char * key
Definition: hpack_parser_table.cc:164
alloc.h
cmdline.h
gpr_cmdline_create
gpr_cmdline * gpr_cmdline_create(const char *description)
Definition: cmdline.cc:66
gpr_cmdline
Definition: cmdline.cc:48
GRPC_JWT_OAUTH2_AUDIENCE
#define GRPC_JWT_OAUTH2_AUDIENCE
Definition: json_token.h:32
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
gpr_cmdline_parse
int gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv)
Definition: cmdline.cc:309
jwt_credentials.h
grpc_auth_json_key
Definition: json_token.h:36


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:59