Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 37 | #include <grpc/grpc.h> |
| 38 | #include <grpc/grpc_security.h> |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 39 | #include <grpc/slice.h> |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/cmdline.h> |
| 42 | #include <grpc/support/log.h> |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 43 | #include <grpc/support/sync.h> |
| 44 | |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 45 | #include "src/core/lib/security/credentials/composite/composite_credentials.h" |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 46 | #include "src/core/lib/security/credentials/credentials.h" |
Craig Tiller | a9b09dd | 2016-10-28 09:05:27 -0700 | [diff] [blame] | 47 | #include "src/core/lib/slice/slice_string_helpers.h" |
Craig Tiller | c650fb3 | 2016-10-28 09:05:50 -0700 | [diff] [blame] | 48 | #include "src/core/lib/support/string.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | typedef struct { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 51 | gpr_mu *mu; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 52 | grpc_polling_entity pops; |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 53 | int is_done; |
| 54 | } synchronizer; |
| 55 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | static void on_metadata_response(grpc_exec_ctx *exec_ctx, void *user_data, |
| 57 | grpc_credentials_md *md_elems, size_t num_md, |
Julien Boeuf | 2e3c9ad | 2016-01-19 17:14:38 -0800 | [diff] [blame] | 58 | grpc_credentials_status status, |
| 59 | const char *error_details) { |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 60 | synchronizer *sync = user_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 61 | if (status == GRPC_CREDENTIALS_ERROR) { |
| 62 | fprintf(stderr, "Fetching token failed.\n"); |
| 63 | } else { |
| 64 | char *token; |
| 65 | GPR_ASSERT(num_md == 1); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 66 | token = grpc_slice_to_c_string(md_elems[0].value); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | printf("\nGot token: %s\n\n", token); |
| 68 | gpr_free(token); |
| 69 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 70 | gpr_mu_lock(sync->mu); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 71 | sync->is_done = 1; |
Craig Tiller | c97065d | 2016-06-07 13:00:14 -0700 | [diff] [blame] | 72 | GRPC_LOG_IF_ERROR( |
| 73 | "pollset_kick", |
| 74 | grpc_pollset_kick(grpc_polling_entity_pollset(&sync->pops), NULL)); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 75 | gpr_mu_unlock(sync->mu); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | int main(int argc, char **argv) { |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 79 | int result = 0; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 80 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 81 | synchronizer sync; |
Julien Boeuf | 4e02e84 | 2015-10-09 22:49:42 -0700 | [diff] [blame] | 82 | grpc_channel_credentials *creds = NULL; |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 83 | char *service_url = "https://test.foo.google.com/Foo"; |
Julien Boeuf | 3c957e6 | 2015-11-18 21:33:58 -0800 | [diff] [blame] | 84 | grpc_auth_metadata_context context; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | gpr_cmdline *cl = gpr_cmdline_create("print_google_default_creds_token"); |
| 86 | gpr_cmdline_add_string(cl, "service_url", |
| 87 | "Service URL for the token request.", &service_url); |
| 88 | gpr_cmdline_parse(cl, argc, argv); |
Julien Boeuf | 3c957e6 | 2015-11-18 21:33:58 -0800 | [diff] [blame] | 89 | memset(&context, 0, sizeof(context)); |
| 90 | context.service_url = service_url; |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 91 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | grpc_init(); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 93 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | creds = grpc_google_default_credentials_create(); |
| 95 | if (creds == NULL) { |
| 96 | fprintf(stderr, "\nCould not find default credentials.\n\n"); |
| 97 | result = 1; |
| 98 | goto end; |
| 99 | } |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 100 | |
Yuchen Zeng | 47de64c | 2017-02-22 19:04:38 -0800 | [diff] [blame] | 101 | grpc_pollset *pollset = gpr_zalloc(grpc_pollset_size()); |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 102 | grpc_pollset_init(pollset, &sync.mu); |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 103 | sync.pops = grpc_polling_entity_create_from_pollset(pollset); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 104 | sync.is_done = 0; |
| 105 | |
Julien Boeuf | b73cbc2 | 2015-10-20 21:52:54 -0700 | [diff] [blame] | 106 | grpc_call_credentials_get_request_metadata( |
| 107 | &exec_ctx, ((grpc_composite_channel_credentials *)creds)->call_creds, |
David Garcia Quintas | 6044909 | 2016-05-04 20:20:04 -0700 | [diff] [blame] | 108 | &sync.pops, context, on_metadata_response, &sync); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 109 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 110 | gpr_mu_lock(sync.mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | while (!sync.is_done) { |
Craig Tiller | 3633ce4 | 2016-02-18 08:08:53 -0800 | [diff] [blame] | 112 | grpc_pollset_worker *worker = NULL; |
Craig Tiller | 1aee536 | 2016-05-07 11:26:50 -0700 | [diff] [blame] | 113 | if (!GRPC_LOG_IF_ERROR( |
| 114 | "pollset_work", |
Craig Tiller | c97065d | 2016-06-07 13:00:14 -0700 | [diff] [blame] | 115 | grpc_pollset_work(&exec_ctx, |
| 116 | grpc_polling_entity_pollset(&sync.pops), &worker, |
Craig Tiller | 1aee536 | 2016-05-07 11:26:50 -0700 | [diff] [blame] | 117 | gpr_now(GPR_CLOCK_MONOTONIC), |
| 118 | gpr_inf_future(GPR_CLOCK_MONOTONIC)))) |
| 119 | sync.is_done = 1; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 120 | gpr_mu_unlock(sync.mu); |
| 121 | grpc_exec_ctx_flush(&exec_ctx); |
| 122 | gpr_mu_lock(sync.mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 124 | gpr_mu_unlock(sync.mu); |
| 125 | |
| 126 | grpc_exec_ctx_finish(&exec_ctx); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 127 | |
Julien Boeuf | 4e02e84 | 2015-10-09 22:49:42 -0700 | [diff] [blame] | 128 | grpc_channel_credentials_release(creds); |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 129 | gpr_free(grpc_polling_entity_pollset(&sync.pops)); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 130 | |
| 131 | end: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | gpr_cmdline_destroy(cl); |
| 133 | grpc_shutdown(); |
Julien Boeuf | cd9b1c8 | 2015-02-20 17:40:41 -0800 | [diff] [blame] | 134 | return result; |
| 135 | } |