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/gpr/string.h"
30 #include "src/core/lib/security/credentials/composite/composite_credentials.h"
31 #include "src/core/lib/security/credentials/credentials.h"
32 #include "src/core/lib/slice/slice_string_helpers.h"
33 #include "test/core/util/cmdline.h"
34 
35 typedef struct {
36   gpr_mu* mu;
37   grpc_polling_entity pops;
38   bool is_done;
39 
40   grpc_credentials_mdelem_array md_array;
41   grpc_closure on_request_metadata;
42 } synchronizer;
43 
on_metadata_response(void * arg,grpc_error * error)44 static void on_metadata_response(void* arg, grpc_error* error) {
45   synchronizer* sync = static_cast<synchronizer*>(arg);
46   if (error != GRPC_ERROR_NONE) {
47     fprintf(stderr, "Fetching token failed: %s\n", grpc_error_string(error));
48     fflush(stderr);
49   } else {
50     char* token;
51     GPR_ASSERT(sync->md_array.size == 1);
52     token = grpc_slice_to_c_string(GRPC_MDVALUE(sync->md_array.md[0]));
53     printf("\nGot token: %s\n\n", token);
54     gpr_free(token);
55   }
56   gpr_mu_lock(sync->mu);
57   sync->is_done = true;
58   GRPC_LOG_IF_ERROR(
59       "pollset_kick",
60       grpc_pollset_kick(grpc_polling_entity_pollset(&sync->pops), nullptr));
61   gpr_mu_unlock(sync->mu);
62 }
63 
main(int argc,char ** argv)64 int main(int argc, char** argv) {
65   int result = 0;
66   grpc_core::ExecCtx exec_ctx;
67   synchronizer sync;
68   grpc_channel_credentials* creds = nullptr;
69   const char* service_url = "https://test.foo.google.com/Foo";
70   grpc_auth_metadata_context context;
71   gpr_cmdline* cl = gpr_cmdline_create("print_google_default_creds_token");
72   grpc_pollset* pollset = nullptr;
73   grpc_error* error = nullptr;
74   gpr_cmdline_add_string(cl, "service_url",
75                          "Service URL for the token request.", &service_url);
76   gpr_cmdline_parse(cl, argc, argv);
77   memset(&context, 0, sizeof(context));
78   context.service_url = service_url;
79 
80   grpc_init();
81 
82   creds = grpc_google_default_credentials_create();
83   if (creds == nullptr) {
84     fprintf(stderr, "\nCould not find default credentials.\n\n");
85     fflush(stderr);
86     result = 1;
87     goto end;
88   }
89 
90   memset(&sync, 0, sizeof(sync));
91   pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
92   grpc_pollset_init(pollset, &sync.mu);
93   sync.pops = grpc_polling_entity_create_from_pollset(pollset);
94   sync.is_done = false;
95   GRPC_CLOSURE_INIT(&sync.on_request_metadata, on_metadata_response, &sync,
96                     grpc_schedule_on_exec_ctx);
97 
98   error = GRPC_ERROR_NONE;
99   if (reinterpret_cast<grpc_composite_channel_credentials*>(creds)
100           ->mutable_call_creds()
101           ->get_request_metadata(&sync.pops, context, &sync.md_array,
102                                  &sync.on_request_metadata, &error)) {
103     // Synchronous response.  Invoke callback directly.
104     on_metadata_response(&sync, error);
105     GRPC_ERROR_UNREF(error);
106   }
107 
108   gpr_mu_lock(sync.mu);
109   while (!sync.is_done) {
110     grpc_pollset_worker* worker = nullptr;
111     if (!GRPC_LOG_IF_ERROR(
112             "pollset_work",
113             grpc_pollset_work(grpc_polling_entity_pollset(&sync.pops), &worker,
114                               GRPC_MILLIS_INF_FUTURE)))
115       sync.is_done = true;
116     gpr_mu_unlock(sync.mu);
117     grpc_core::ExecCtx::Get()->Flush();
118     gpr_mu_lock(sync.mu);
119   }
120   gpr_mu_unlock(sync.mu);
121 
122   grpc_channel_credentials_release(creds);
123   gpr_free(grpc_polling_entity_pollset(&sync.pops));
124 
125 end:
126   gpr_cmdline_destroy(cl);
127   grpc_shutdown();
128   return result;
129 }
130