aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/security/print_google_default_creds_token.cc
diff options
context:
space:
mode:
authorGravatar Yash Tibrewal <yashkt@google.com>2017-11-09 17:46:29 -0800
committerGravatar Yash Tibrewal <yashkt@google.com>2017-11-09 17:46:29 -0800
commit4e9265c828f0b559b5fdba04913fed46bf771399 (patch)
tree4a379fc2bdc037753cf8d81f8b86327e4bc50a42 /test/core/security/print_google_default_creds_token.cc
parent0ee7574732a06e8cace4e099a678f4bd5dbff679 (diff)
parentd9da7387b8057f3bd99a417a5ee905377bce9296 (diff)
Merge with master
Diffstat (limited to 'test/core/security/print_google_default_creds_token.cc')
-rw-r--r--test/core/security/print_google_default_creds_token.cc128
1 files changed, 128 insertions, 0 deletions
diff --git a/test/core/security/print_google_default_creds_token.cc b/test/core/security/print_google_default_creds_token.cc
new file mode 100644
index 0000000000..6ea51658e1
--- /dev/null
+++ b/test/core/security/print_google_default_creds_token.cc
@@ -0,0 +1,128 @@
+/*
+ *
+ * Copyright 2015 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <grpc/grpc.h>
+#include <grpc/grpc_security.h>
+#include <grpc/slice.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/cmdline.h>
+#include <grpc/support/log.h>
+#include <grpc/support/sync.h>
+
+#include "src/core/lib/security/credentials/composite/composite_credentials.h"
+#include "src/core/lib/security/credentials/credentials.h"
+#include "src/core/lib/slice/slice_string_helpers.h"
+#include "src/core/lib/support/string.h"
+
+typedef struct {
+ gpr_mu* mu;
+ grpc_polling_entity pops;
+ bool is_done;
+
+ grpc_credentials_mdelem_array md_array;
+ grpc_closure on_request_metadata;
+} synchronizer;
+
+static void on_metadata_response(void* arg, grpc_error* error) {
+ synchronizer* sync = static_cast<synchronizer*>(arg);
+ if (error != GRPC_ERROR_NONE) {
+ fprintf(stderr, "Fetching token failed: %s\n", grpc_error_string(error));
+ } else {
+ char* token;
+ GPR_ASSERT(sync->md_array.size == 1);
+ token = grpc_slice_to_c_string(GRPC_MDVALUE(sync->md_array.md[0]));
+ printf("\nGot token: %s\n\n", token);
+ gpr_free(token);
+ }
+ gpr_mu_lock(sync->mu);
+ sync->is_done = true;
+ GRPC_LOG_IF_ERROR(
+ "pollset_kick",
+ grpc_pollset_kick(grpc_polling_entity_pollset(&sync->pops), NULL));
+ gpr_mu_unlock(sync->mu);
+}
+
+int main(int argc, char** argv) {
+ int result = 0;
+ ExecCtx _local_exec_ctx;
+ synchronizer sync;
+ grpc_channel_credentials* creds = NULL;
+ const char* service_url = "https://test.foo.google.com/Foo";
+ grpc_auth_metadata_context context;
+ gpr_cmdline* cl = gpr_cmdline_create("print_google_default_creds_token");
+ grpc_pollset* pollset = nullptr;
+ grpc_error* error = nullptr;
+ gpr_cmdline_add_string(cl, "service_url",
+ "Service URL for the token request.", &service_url);
+ gpr_cmdline_parse(cl, argc, argv);
+ memset(&context, 0, sizeof(context));
+ context.service_url = service_url;
+
+ grpc_init();
+
+ creds = grpc_google_default_credentials_create();
+ if (creds == NULL) {
+ fprintf(stderr, "\nCould not find default credentials.\n\n");
+ result = 1;
+ goto end;
+ }
+
+ memset(&sync, 0, sizeof(sync));
+ pollset = (grpc_pollset*)gpr_zalloc(grpc_pollset_size());
+ grpc_pollset_init(pollset, &sync.mu);
+ sync.pops = grpc_polling_entity_create_from_pollset(pollset);
+ sync.is_done = false;
+ GRPC_CLOSURE_INIT(&sync.on_request_metadata, on_metadata_response, &sync,
+ grpc_schedule_on_exec_ctx);
+
+ error = GRPC_ERROR_NONE;
+ if (grpc_call_credentials_get_request_metadata(
+ ((grpc_composite_channel_credentials*)creds)->call_creds, &sync.pops,
+ context, &sync.md_array, &sync.on_request_metadata, &error)) {
+ // Synchronous response. Invoke callback directly.
+ on_metadata_response(&sync, error);
+ GRPC_ERROR_UNREF(error);
+ }
+
+ gpr_mu_lock(sync.mu);
+ while (!sync.is_done) {
+ grpc_pollset_worker* worker = NULL;
+ if (!GRPC_LOG_IF_ERROR(
+ "pollset_work",
+ grpc_pollset_work(grpc_polling_entity_pollset(&sync.pops), &worker,
+ GRPC_MILLIS_INF_FUTURE)))
+ sync.is_done = true;
+ gpr_mu_unlock(sync.mu);
+ grpc_exec_ctx_flush();
+ gpr_mu_lock(sync.mu);
+ }
+ gpr_mu_unlock(sync.mu);
+
+ grpc_exec_ctx_finish();
+
+ grpc_channel_credentials_release(creds);
+ gpr_free(grpc_polling_entity_pollset(&sync.pops));
+
+end:
+ gpr_cmdline_destroy(cl);
+ grpc_shutdown();
+ return result;
+}