• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/grpc.h>
23 #include <grpc/grpc_security.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/sync.h>
28 
29 #include "src/core/lib/iomgr/load_file.h"
30 #include "src/core/lib/security/credentials/credentials.h"
31 #include "test/core/security/oauth2_utils.h"
32 #include "test/core/util/cmdline.h"
33 
create_refresh_token_creds(const char * json_refresh_token_file_path)34 static grpc_call_credentials* create_refresh_token_creds(
35     const char* json_refresh_token_file_path) {
36   grpc_slice refresh_token;
37   GPR_ASSERT(GRPC_LOG_IF_ERROR(
38       "load_file",
39       grpc_load_file(json_refresh_token_file_path, 1, &refresh_token)));
40   return grpc_google_refresh_token_credentials_create(
41       reinterpret_cast<const char*> GRPC_SLICE_START_PTR(refresh_token),
42       nullptr);
43 }
44 
main(int argc,char ** argv)45 int main(int argc, char** argv) {
46   grpc_call_credentials* creds = nullptr;
47   char* json_key_file_path = nullptr;
48   const char* json_refresh_token_file_path = nullptr;
49   char* token = nullptr;
50   int use_gce = 0;
51   char* scope = nullptr;
52   gpr_cmdline* cl = gpr_cmdline_create("fetch_oauth2");
53   gpr_cmdline_add_string(cl, "json_refresh_token",
54                          "File path of the json refresh token.",
55                          &json_refresh_token_file_path);
56   gpr_cmdline_add_flag(
57       cl, "gce",
58       "Get a token from the GCE metadata server (only works in GCE).",
59       &use_gce);
60   gpr_cmdline_parse(cl, argc, argv);
61 
62   grpc_init();
63 
64   if (json_key_file_path != nullptr &&
65       json_refresh_token_file_path != nullptr) {
66     gpr_log(GPR_ERROR,
67             "--json_key and --json_refresh_token are mutually exclusive.");
68     exit(1);
69   }
70 
71   if (use_gce) {
72     if (json_key_file_path != nullptr || scope != nullptr) {
73       gpr_log(GPR_INFO,
74               "Ignoring json key and scope to get a token from the GCE "
75               "metadata server.");
76     }
77     creds = grpc_google_compute_engine_credentials_create(nullptr);
78     if (creds == nullptr) {
79       gpr_log(GPR_ERROR, "Could not create gce credentials.");
80       exit(1);
81     }
82   } else if (json_refresh_token_file_path != nullptr) {
83     creds = create_refresh_token_creds(json_refresh_token_file_path);
84     if (creds == nullptr) {
85       gpr_log(GPR_ERROR,
86               "Could not create refresh token creds. %s does probably not "
87               "contain a valid json refresh token.",
88               json_refresh_token_file_path);
89       exit(1);
90     }
91   } else {
92     gpr_log(GPR_ERROR, "Missing --gce or --json_refresh_token option.");
93     exit(1);
94   }
95   GPR_ASSERT(creds != nullptr);
96 
97   token = grpc_test_fetch_oauth2_token_with_credentials(creds);
98   if (token != nullptr) {
99     printf("Got token: %s.\n", token);
100     gpr_free(token);
101   }
102   grpc_call_credentials_release(creds);
103   gpr_cmdline_destroy(cl);
104   grpc_shutdown();
105   return 0;
106 }
107