OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 37 #include <grpc/grpc.h> |
| 38 #include <grpc/grpc_security.h> |
| 39 #include <grpc/support/alloc.h> |
| 40 #include <grpc/support/cmdline.h> |
| 41 #include <grpc/support/log.h> |
| 42 #include <grpc/support/slice.h> |
| 43 #include <grpc/support/sync.h> |
| 44 |
| 45 #include "src/core/security/credentials.h" |
| 46 #include "src/core/support/load_file.h" |
| 47 #include "test/core/security/oauth2_utils.h" |
| 48 |
| 49 static grpc_call_credentials *create_refresh_token_creds( |
| 50 const char *json_refresh_token_file_path) { |
| 51 int success; |
| 52 gpr_slice refresh_token = |
| 53 gpr_load_file(json_refresh_token_file_path, 1, &success); |
| 54 if (!success) { |
| 55 gpr_log(GPR_ERROR, "Could not read file %s.", json_refresh_token_file_path); |
| 56 exit(1); |
| 57 } |
| 58 return grpc_google_refresh_token_credentials_create( |
| 59 (const char *)GPR_SLICE_START_PTR(refresh_token), NULL); |
| 60 } |
| 61 |
| 62 int main(int argc, char **argv) { |
| 63 grpc_call_credentials *creds = NULL; |
| 64 char *json_key_file_path = NULL; |
| 65 char *json_refresh_token_file_path = NULL; |
| 66 char *token = NULL; |
| 67 int use_gce = 0; |
| 68 char *scope = NULL; |
| 69 gpr_cmdline *cl = gpr_cmdline_create("fetch_oauth2"); |
| 70 gpr_cmdline_add_string(cl, "json_refresh_token", |
| 71 "File path of the json refresh token.", |
| 72 &json_refresh_token_file_path); |
| 73 gpr_cmdline_add_flag( |
| 74 cl, "gce", |
| 75 "Get a token from the GCE metadata server (only works in GCE).", |
| 76 &use_gce); |
| 77 gpr_cmdline_parse(cl, argc, argv); |
| 78 |
| 79 grpc_init(); |
| 80 |
| 81 if (json_key_file_path != NULL && json_refresh_token_file_path != NULL) { |
| 82 gpr_log(GPR_ERROR, |
| 83 "--json_key and --json_refresh_token are mutually exclusive."); |
| 84 exit(1); |
| 85 } |
| 86 |
| 87 if (use_gce) { |
| 88 if (json_key_file_path != NULL || scope != NULL) { |
| 89 gpr_log(GPR_INFO, |
| 90 "Ignoring json key and scope to get a token from the GCE " |
| 91 "metadata server."); |
| 92 } |
| 93 creds = grpc_google_compute_engine_credentials_create(NULL); |
| 94 if (creds == NULL) { |
| 95 gpr_log(GPR_ERROR, "Could not create gce credentials."); |
| 96 exit(1); |
| 97 } |
| 98 } else if (json_refresh_token_file_path != NULL) { |
| 99 creds = create_refresh_token_creds(json_refresh_token_file_path); |
| 100 if (creds == NULL) { |
| 101 gpr_log(GPR_ERROR, |
| 102 "Could not create refresh token creds. %s does probably not " |
| 103 "contain a valid json refresh token.", |
| 104 json_refresh_token_file_path); |
| 105 exit(1); |
| 106 } |
| 107 } else { |
| 108 gpr_log(GPR_ERROR, "Missing --gce or --json_refresh_token option."); |
| 109 exit(1); |
| 110 } |
| 111 GPR_ASSERT(creds != NULL); |
| 112 |
| 113 token = grpc_test_fetch_oauth2_token_with_credentials(creds); |
| 114 if (token != NULL) { |
| 115 printf("Got token: %s.\n", token); |
| 116 gpr_free(token); |
| 117 } |
| 118 grpc_call_credentials_release(creds); |
| 119 gpr_cmdline_destroy(cl); |
| 120 grpc_shutdown(); |
| 121 return 0; |
| 122 } |
OLD | NEW |