Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 "src/core/security/credentials.h" |
| 35 | |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <stdio.h> |
| 38 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 39 | #include "src/core/channel/channel_args.h" |
| 40 | #include "src/core/channel/http_client_filter.h" |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 41 | #include "src/core/json/json.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include "src/core/httpcli/httpcli.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 43 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 44 | #include "src/core/support/string.h" |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 45 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc/support/alloc.h> |
| 47 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 48 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc/support/sync.h> |
| 50 | #include <grpc/support/time.h> |
| 51 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | /* -- Common. -- */ |
| 53 | |
Julien Boeuf | b037bb6 | 2015-07-08 14:58:14 -0700 | [diff] [blame] | 54 | struct grpc_credentials_metadata_request { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | grpc_credentials *creds; |
| 56 | grpc_credentials_metadata_cb cb; |
David Garcia Quintas | 69ba871 | 2015-06-01 11:29:42 -0700 | [diff] [blame] | 57 | grpc_iomgr_closure *on_simulated_token_fetch_done_closure; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | void *user_data; |
Julien Boeuf | b037bb6 | 2015-07-08 14:58:14 -0700 | [diff] [blame] | 59 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | |
| 61 | static grpc_credentials_metadata_request * |
| 62 | grpc_credentials_metadata_request_create(grpc_credentials *creds, |
| 63 | grpc_credentials_metadata_cb cb, |
| 64 | void *user_data) { |
| 65 | grpc_credentials_metadata_request *r = |
| 66 | gpr_malloc(sizeof(grpc_credentials_metadata_request)); |
| 67 | r->creds = grpc_credentials_ref(creds); |
| 68 | r->cb = cb; |
David Garcia Quintas | 69ba871 | 2015-06-01 11:29:42 -0700 | [diff] [blame] | 69 | r->on_simulated_token_fetch_done_closure = |
| 70 | gpr_malloc(sizeof(grpc_iomgr_closure)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | r->user_data = user_data; |
| 72 | return r; |
| 73 | } |
| 74 | |
| 75 | static void grpc_credentials_metadata_request_destroy( |
| 76 | grpc_credentials_metadata_request *r) { |
| 77 | grpc_credentials_unref(r->creds); |
David Garcia Quintas | 69ba871 | 2015-06-01 11:29:42 -0700 | [diff] [blame] | 78 | gpr_free(r->on_simulated_token_fetch_done_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | gpr_free(r); |
| 80 | } |
| 81 | |
| 82 | grpc_credentials *grpc_credentials_ref(grpc_credentials *creds) { |
| 83 | if (creds == NULL) return NULL; |
| 84 | gpr_ref(&creds->refcount); |
| 85 | return creds; |
| 86 | } |
| 87 | |
| 88 | void grpc_credentials_unref(grpc_credentials *creds) { |
| 89 | if (creds == NULL) return; |
| 90 | if (gpr_unref(&creds->refcount)) creds->vtable->destroy(creds); |
| 91 | } |
| 92 | |
| 93 | void grpc_credentials_release(grpc_credentials *creds) { |
| 94 | grpc_credentials_unref(creds); |
| 95 | } |
| 96 | |
| 97 | int grpc_credentials_has_request_metadata(grpc_credentials *creds) { |
| 98 | if (creds == NULL) return 0; |
| 99 | return creds->vtable->has_request_metadata(creds); |
| 100 | } |
| 101 | |
| 102 | int grpc_credentials_has_request_metadata_only(grpc_credentials *creds) { |
| 103 | if (creds == NULL) return 0; |
| 104 | return creds->vtable->has_request_metadata_only(creds); |
| 105 | } |
| 106 | |
| 107 | void grpc_credentials_get_request_metadata(grpc_credentials *creds, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 108 | grpc_pollset *pollset, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 109 | const char *service_url, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | grpc_credentials_metadata_cb cb, |
| 111 | void *user_data) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 112 | if (creds == NULL || !grpc_credentials_has_request_metadata(creds) || |
| 113 | creds->vtable->get_request_metadata == NULL) { |
| 114 | if (cb != NULL) { |
| 115 | cb(user_data, NULL, 0, GRPC_CREDENTIALS_OK); |
| 116 | } |
| 117 | return; |
| 118 | } |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 119 | creds->vtable->get_request_metadata(creds, pollset, service_url, cb, |
| 120 | user_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 123 | grpc_security_status grpc_credentials_create_security_connector( |
| 124 | grpc_credentials *creds, const char *target, const grpc_channel_args *args, |
| 125 | grpc_credentials *request_metadata_creds, |
| 126 | grpc_channel_security_connector **sc, grpc_channel_args **new_args) { |
| 127 | *new_args = NULL; |
| 128 | if (creds == NULL || creds->vtable->create_security_connector == NULL || |
| 129 | grpc_credentials_has_request_metadata_only(creds)) { |
| 130 | gpr_log(GPR_ERROR, |
| 131 | "Invalid credentials for creating a security connector."); |
| 132 | return GRPC_SECURITY_ERROR; |
| 133 | } |
| 134 | return creds->vtable->create_security_connector( |
| 135 | creds, target, args, request_metadata_creds, sc, new_args); |
Craig Tiller | c4885ed | 2015-04-14 09:51:28 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | void grpc_server_credentials_release(grpc_server_credentials *creds) { |
| 139 | if (creds == NULL) return; |
| 140 | creds->vtable->destroy(creds); |
| 141 | } |
| 142 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 143 | grpc_security_status grpc_server_credentials_create_security_connector( |
| 144 | grpc_server_credentials *creds, grpc_security_connector **sc) { |
| 145 | if (creds == NULL || creds->vtable->create_security_connector == NULL) { |
| 146 | gpr_log(GPR_ERROR, "Server credentials cannot create security context."); |
| 147 | return GRPC_SECURITY_ERROR; |
| 148 | } |
| 149 | return creds->vtable->create_security_connector(creds, sc); |
| 150 | } |
| 151 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 152 | /* -- Ssl credentials. -- */ |
| 153 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | static void ssl_destroy(grpc_credentials *creds) { |
| 155 | grpc_ssl_credentials *c = (grpc_ssl_credentials *)creds; |
| 156 | if (c->config.pem_root_certs != NULL) gpr_free(c->config.pem_root_certs); |
| 157 | if (c->config.pem_private_key != NULL) gpr_free(c->config.pem_private_key); |
| 158 | if (c->config.pem_cert_chain != NULL) gpr_free(c->config.pem_cert_chain); |
| 159 | gpr_free(creds); |
| 160 | } |
| 161 | |
| 162 | static void ssl_server_destroy(grpc_server_credentials *creds) { |
| 163 | grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *)creds; |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 164 | size_t i; |
| 165 | for (i = 0; i < c->config.num_key_cert_pairs; i++) { |
| 166 | if (c->config.pem_private_keys[i] != NULL) { |
| 167 | gpr_free(c->config.pem_private_keys[i]); |
| 168 | } |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 169 | if (c->config.pem_cert_chains[i] != NULL) { |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 170 | gpr_free(c->config.pem_cert_chains[i]); |
| 171 | } |
| 172 | } |
| 173 | if (c->config.pem_private_keys != NULL) gpr_free(c->config.pem_private_keys); |
| 174 | if (c->config.pem_private_keys_sizes != NULL) { |
| 175 | gpr_free(c->config.pem_private_keys_sizes); |
| 176 | } |
| 177 | if (c->config.pem_cert_chains != NULL) gpr_free(c->config.pem_cert_chains); |
| 178 | if (c->config.pem_cert_chains_sizes != NULL) { |
| 179 | gpr_free(c->config.pem_cert_chains_sizes); |
| 180 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | if (c->config.pem_root_certs != NULL) gpr_free(c->config.pem_root_certs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | gpr_free(creds); |
| 183 | } |
| 184 | |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 185 | static int ssl_has_request_metadata(const grpc_credentials *creds) { return 0; } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | |
| 187 | static int ssl_has_request_metadata_only(const grpc_credentials *creds) { |
| 188 | return 0; |
| 189 | } |
| 190 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 191 | static grpc_security_status ssl_create_security_connector( |
| 192 | grpc_credentials *creds, const char *target, const grpc_channel_args *args, |
| 193 | grpc_credentials *request_metadata_creds, |
| 194 | grpc_channel_security_connector **sc, grpc_channel_args **new_args) { |
| 195 | grpc_ssl_credentials *c = (grpc_ssl_credentials *)creds; |
| 196 | grpc_security_status status = GRPC_SECURITY_OK; |
| 197 | size_t i = 0; |
| 198 | const char *overridden_target_name = NULL; |
| 199 | grpc_arg arg; |
| 200 | |
| 201 | for (i = 0; args && i < args->num_args; i++) { |
| 202 | grpc_arg *arg = &args->args[i]; |
| 203 | if (strcmp(arg->key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG) == 0 && |
| 204 | arg->type == GRPC_ARG_STRING) { |
| 205 | overridden_target_name = arg->value.string; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | status = grpc_ssl_channel_security_connector_create( |
| 210 | request_metadata_creds, &c->config, target, overridden_target_name, sc); |
| 211 | if (status != GRPC_SECURITY_OK) { |
| 212 | return status; |
| 213 | } |
| 214 | arg.type = GRPC_ARG_STRING; |
| 215 | arg.key = GRPC_ARG_HTTP2_SCHEME; |
| 216 | arg.value.string = "https"; |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 217 | *new_args = grpc_channel_args_copy_and_add(args, &arg, 1); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 218 | return status; |
| 219 | } |
| 220 | |
| 221 | static grpc_security_status ssl_server_create_security_connector( |
| 222 | grpc_server_credentials *creds, grpc_security_connector **sc) { |
| 223 | grpc_ssl_server_credentials *c = (grpc_ssl_server_credentials *)creds; |
| 224 | return grpc_ssl_server_security_connector_create(&c->config, sc); |
| 225 | } |
| 226 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 227 | static grpc_credentials_vtable ssl_vtable = { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 228 | ssl_destroy, ssl_has_request_metadata, ssl_has_request_metadata_only, NULL, |
| 229 | ssl_create_security_connector}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 231 | static grpc_server_credentials_vtable ssl_server_vtable = { |
| 232 | ssl_server_destroy, ssl_server_create_security_connector}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 233 | |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 234 | static void ssl_copy_key_material(const char *input, unsigned char **output, |
| 235 | size_t *output_size) { |
| 236 | *output_size = strlen(input); |
| 237 | *output = gpr_malloc(*output_size); |
| 238 | memcpy(*output, input, *output_size); |
| 239 | } |
| 240 | |
| 241 | static void ssl_build_config(const char *pem_root_certs, |
| 242 | grpc_ssl_pem_key_cert_pair *pem_key_cert_pair, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | grpc_ssl_config *config) { |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 244 | if (pem_root_certs != NULL) { |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 245 | ssl_copy_key_material(pem_root_certs, &config->pem_root_certs, |
| 246 | &config->pem_root_certs_size); |
| 247 | } |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 248 | if (pem_key_cert_pair != NULL) { |
| 249 | GPR_ASSERT(pem_key_cert_pair->private_key != NULL); |
| 250 | GPR_ASSERT(pem_key_cert_pair->cert_chain != NULL); |
| 251 | ssl_copy_key_material(pem_key_cert_pair->private_key, |
| 252 | &config->pem_private_key, |
| 253 | &config->pem_private_key_size); |
| 254 | ssl_copy_key_material(pem_key_cert_pair->cert_chain, |
| 255 | &config->pem_cert_chain, |
| 256 | &config->pem_cert_chain_size); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | static void ssl_build_server_config( |
| 261 | const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs, |
| 262 | size_t num_key_cert_pairs, grpc_ssl_server_config *config) { |
| 263 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 264 | if (pem_root_certs != NULL) { |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 265 | ssl_copy_key_material(pem_root_certs, &config->pem_root_certs, |
| 266 | &config->pem_root_certs_size); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 267 | } |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 268 | if (num_key_cert_pairs > 0) { |
| 269 | GPR_ASSERT(pem_key_cert_pairs != NULL); |
| 270 | config->pem_private_keys = |
| 271 | gpr_malloc(num_key_cert_pairs * sizeof(unsigned char *)); |
| 272 | config->pem_cert_chains = |
| 273 | gpr_malloc(num_key_cert_pairs * sizeof(unsigned char *)); |
| 274 | config->pem_private_keys_sizes = |
| 275 | gpr_malloc(num_key_cert_pairs * sizeof(size_t)); |
| 276 | config->pem_cert_chains_sizes = |
| 277 | gpr_malloc(num_key_cert_pairs * sizeof(size_t)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 278 | } |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 279 | config->num_key_cert_pairs = num_key_cert_pairs; |
| 280 | for (i = 0; i < num_key_cert_pairs; i++) { |
| 281 | GPR_ASSERT(pem_key_cert_pairs[i].private_key != NULL); |
| 282 | GPR_ASSERT(pem_key_cert_pairs[i].cert_chain != NULL); |
| 283 | ssl_copy_key_material(pem_key_cert_pairs[i].private_key, |
| 284 | &config->pem_private_keys[i], |
| 285 | &config->pem_private_keys_sizes[i]); |
| 286 | ssl_copy_key_material(pem_key_cert_pairs[i].cert_chain, |
| 287 | &config->pem_cert_chains[i], |
| 288 | &config->pem_cert_chains_sizes[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | grpc_credentials *grpc_ssl_credentials_create( |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 293 | const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 294 | grpc_ssl_credentials *c = gpr_malloc(sizeof(grpc_ssl_credentials)); |
| 295 | memset(c, 0, sizeof(grpc_ssl_credentials)); |
| 296 | c->base.type = GRPC_CREDENTIALS_TYPE_SSL; |
| 297 | c->base.vtable = &ssl_vtable; |
| 298 | gpr_ref_init(&c->base.refcount, 1); |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 299 | ssl_build_config(pem_root_certs, pem_key_cert_pair, &c->config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 300 | return &c->base; |
| 301 | } |
| 302 | |
| 303 | grpc_server_credentials *grpc_ssl_server_credentials_create( |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 304 | const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pairs, |
| 305 | size_t num_key_cert_pairs) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 306 | grpc_ssl_server_credentials *c = |
| 307 | gpr_malloc(sizeof(grpc_ssl_server_credentials)); |
| 308 | memset(c, 0, sizeof(grpc_ssl_server_credentials)); |
| 309 | c->base.type = GRPC_CREDENTIALS_TYPE_SSL; |
| 310 | c->base.vtable = &ssl_server_vtable; |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 311 | ssl_build_server_config(pem_root_certs, pem_key_cert_pairs, |
| 312 | num_key_cert_pairs, &c->config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 313 | return &c->base; |
| 314 | } |
| 315 | |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 316 | /* -- Jwt credentials -- */ |
| 317 | |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 318 | static void jwt_reset_cache(grpc_service_account_jwt_access_credentials *c) { |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 319 | if (c->cached.jwt_md != NULL) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 320 | grpc_credentials_md_store_unref(c->cached.jwt_md); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 321 | c->cached.jwt_md = NULL; |
| 322 | } |
| 323 | if (c->cached.service_url != NULL) { |
| 324 | gpr_free(c->cached.service_url); |
| 325 | c->cached.service_url = NULL; |
| 326 | } |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 327 | c->cached.jwt_expiration = gpr_inf_past(GPR_CLOCK_REALTIME); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void jwt_destroy(grpc_credentials *creds) { |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 331 | grpc_service_account_jwt_access_credentials *c = |
| 332 | (grpc_service_account_jwt_access_credentials *)creds; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 333 | grpc_auth_json_key_destruct(&c->key); |
| 334 | jwt_reset_cache(c); |
| 335 | gpr_mu_destroy(&c->cache_mu); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 336 | gpr_free(c); |
| 337 | } |
| 338 | |
| 339 | static int jwt_has_request_metadata(const grpc_credentials *creds) { return 1; } |
| 340 | |
| 341 | static int jwt_has_request_metadata_only(const grpc_credentials *creds) { |
| 342 | return 1; |
| 343 | } |
| 344 | |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 345 | static void jwt_get_request_metadata(grpc_credentials *creds, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 346 | grpc_pollset *pollset, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 347 | const char *service_url, |
| 348 | grpc_credentials_metadata_cb cb, |
| 349 | void *user_data) { |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 350 | grpc_service_account_jwt_access_credentials *c = |
| 351 | (grpc_service_account_jwt_access_credentials *)creds; |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 352 | gpr_timespec refresh_threshold = gpr_time_from_seconds( |
| 353 | GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS, GPR_TIMESPAN); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 354 | |
| 355 | /* See if we can return a cached jwt. */ |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 356 | grpc_credentials_md_store *jwt_md = NULL; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 357 | { |
| 358 | gpr_mu_lock(&c->cache_mu); |
| 359 | if (c->cached.service_url != NULL && |
Ronnie Sahlberg | 2ad8d21 | 2015-03-07 08:39:22 -0800 | [diff] [blame] | 360 | strcmp(c->cached.service_url, service_url) == 0 && |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 361 | c->cached.jwt_md != NULL && |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 362 | (gpr_time_cmp(gpr_time_sub(c->cached.jwt_expiration, |
| 363 | gpr_now(GPR_CLOCK_REALTIME)), |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 364 | refresh_threshold) > 0)) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 365 | jwt_md = grpc_credentials_md_store_ref(c->cached.jwt_md); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 366 | } |
| 367 | gpr_mu_unlock(&c->cache_mu); |
| 368 | } |
| 369 | |
| 370 | if (jwt_md == NULL) { |
| 371 | char *jwt = NULL; |
| 372 | /* Generate a new jwt. */ |
| 373 | gpr_mu_lock(&c->cache_mu); |
| 374 | jwt_reset_cache(c); |
| 375 | jwt = grpc_jwt_encode_and_sign(&c->key, service_url, c->jwt_lifetime, NULL); |
| 376 | if (jwt != NULL) { |
| 377 | char *md_value; |
| 378 | gpr_asprintf(&md_value, "Bearer %s", jwt); |
| 379 | gpr_free(jwt); |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 380 | c->cached.jwt_expiration = |
| 381 | gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), c->jwt_lifetime); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 382 | c->cached.service_url = gpr_strdup(service_url); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 383 | c->cached.jwt_md = grpc_credentials_md_store_create(1); |
| 384 | grpc_credentials_md_store_add_cstrings( |
| 385 | c->cached.jwt_md, GRPC_AUTHORIZATION_METADATA_KEY, md_value); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 386 | gpr_free(md_value); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 387 | jwt_md = grpc_credentials_md_store_ref(c->cached.jwt_md); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 388 | } |
| 389 | gpr_mu_unlock(&c->cache_mu); |
| 390 | } |
| 391 | |
| 392 | if (jwt_md != NULL) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 393 | cb(user_data, jwt_md->entries, jwt_md->num_entries, GRPC_CREDENTIALS_OK); |
| 394 | grpc_credentials_md_store_unref(jwt_md); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 395 | } else { |
| 396 | cb(user_data, NULL, 0, GRPC_CREDENTIALS_ERROR); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static grpc_credentials_vtable jwt_vtable = { |
| 401 | jwt_destroy, jwt_has_request_metadata, jwt_has_request_metadata_only, |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 402 | jwt_get_request_metadata, NULL}; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 403 | |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 404 | grpc_credentials * |
| 405 | grpc_service_account_jwt_access_credentials_create_from_auth_json_key( |
Julien Boeuf | 5400606 | 2015-07-07 19:13:04 -0700 | [diff] [blame] | 406 | grpc_auth_json_key key, gpr_timespec token_lifetime) { |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 407 | grpc_service_account_jwt_access_credentials *c; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 408 | if (!grpc_auth_json_key_is_valid(&key)) { |
| 409 | gpr_log(GPR_ERROR, "Invalid input for jwt credentials creation"); |
| 410 | return NULL; |
| 411 | } |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 412 | c = gpr_malloc(sizeof(grpc_service_account_jwt_access_credentials)); |
| 413 | memset(c, 0, sizeof(grpc_service_account_jwt_access_credentials)); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 414 | c->base.type = GRPC_CREDENTIALS_TYPE_JWT; |
| 415 | gpr_ref_init(&c->base.refcount, 1); |
| 416 | c->base.vtable = &jwt_vtable; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 417 | c->key = key; |
| 418 | c->jwt_lifetime = token_lifetime; |
| 419 | gpr_mu_init(&c->cache_mu); |
| 420 | jwt_reset_cache(c); |
| 421 | return &c->base; |
| 422 | } |
| 423 | |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 424 | grpc_credentials *grpc_service_account_jwt_access_credentials_create( |
| 425 | const char *json_key, gpr_timespec token_lifetime) { |
| 426 | return grpc_service_account_jwt_access_credentials_create_from_auth_json_key( |
Julien Boeuf | 5400606 | 2015-07-07 19:13:04 -0700 | [diff] [blame] | 427 | grpc_auth_json_key_create_from_string(json_key), token_lifetime); |
| 428 | } |
| 429 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 430 | /* -- Oauth2TokenFetcher credentials -- */ |
| 431 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 432 | static void oauth2_token_fetcher_destroy(grpc_credentials *creds) { |
| 433 | grpc_oauth2_token_fetcher_credentials *c = |
| 434 | (grpc_oauth2_token_fetcher_credentials *)creds; |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 435 | grpc_credentials_md_store_unref(c->access_token_md); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 436 | gpr_mu_destroy(&c->mu); |
Craig Tiller | a0abe37 | 2015-06-01 11:31:08 -0700 | [diff] [blame] | 437 | grpc_httpcli_context_destroy(&c->httpcli_context); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 438 | gpr_free(c); |
| 439 | } |
| 440 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 441 | static int oauth2_token_fetcher_has_request_metadata( |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 442 | const grpc_credentials *creds) { |
| 443 | return 1; |
| 444 | } |
| 445 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 446 | static int oauth2_token_fetcher_has_request_metadata_only( |
| 447 | const grpc_credentials *creds) { |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | grpc_credentials_status |
| 452 | grpc_oauth2_token_fetcher_credentials_parse_server_response( |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 453 | const grpc_httpcli_response *response, grpc_credentials_md_store **token_md, |
| 454 | gpr_timespec *token_lifetime) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 455 | char *null_terminated_body = NULL; |
| 456 | char *new_access_token = NULL; |
| 457 | grpc_credentials_status status = GRPC_CREDENTIALS_OK; |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 458 | grpc_json *json = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 459 | |
Julien Boeuf | 90bd7c4 | 2015-02-18 18:51:16 -0800 | [diff] [blame] | 460 | if (response == NULL) { |
| 461 | gpr_log(GPR_ERROR, "Received NULL response."); |
| 462 | status = GRPC_CREDENTIALS_ERROR; |
| 463 | goto end; |
| 464 | } |
| 465 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 466 | if (response->body_length > 0) { |
| 467 | null_terminated_body = gpr_malloc(response->body_length + 1); |
| 468 | null_terminated_body[response->body_length] = '\0'; |
| 469 | memcpy(null_terminated_body, response->body, response->body_length); |
| 470 | } |
| 471 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 472 | if (response->status != 200) { |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 473 | gpr_log(GPR_ERROR, "Call to http server ended with error %d [%s].", |
| 474 | response->status, |
| 475 | null_terminated_body != NULL ? null_terminated_body : ""); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 476 | status = GRPC_CREDENTIALS_ERROR; |
| 477 | goto end; |
| 478 | } else { |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 479 | grpc_json *access_token = NULL; |
| 480 | grpc_json *token_type = NULL; |
| 481 | grpc_json *expires_in = NULL; |
| 482 | grpc_json *ptr; |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 483 | json = grpc_json_parse_string(null_terminated_body); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 484 | if (json == NULL) { |
| 485 | gpr_log(GPR_ERROR, "Could not parse JSON from %s", null_terminated_body); |
| 486 | status = GRPC_CREDENTIALS_ERROR; |
| 487 | goto end; |
| 488 | } |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 489 | if (json->type != GRPC_JSON_OBJECT) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 490 | gpr_log(GPR_ERROR, "Response should be a JSON object"); |
| 491 | status = GRPC_CREDENTIALS_ERROR; |
| 492 | goto end; |
| 493 | } |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 494 | for (ptr = json->child; ptr; ptr = ptr->next) { |
| 495 | if (strcmp(ptr->key, "access_token") == 0) { |
| 496 | access_token = ptr; |
| 497 | } else if (strcmp(ptr->key, "token_type") == 0) { |
| 498 | token_type = ptr; |
| 499 | } else if (strcmp(ptr->key, "expires_in") == 0) { |
| 500 | expires_in = ptr; |
| 501 | } |
| 502 | } |
| 503 | if (access_token == NULL || access_token->type != GRPC_JSON_STRING) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 504 | gpr_log(GPR_ERROR, "Missing or invalid access_token in JSON."); |
| 505 | status = GRPC_CREDENTIALS_ERROR; |
| 506 | goto end; |
| 507 | } |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 508 | if (token_type == NULL || token_type->type != GRPC_JSON_STRING) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 509 | gpr_log(GPR_ERROR, "Missing or invalid token_type in JSON."); |
| 510 | status = GRPC_CREDENTIALS_ERROR; |
| 511 | goto end; |
| 512 | } |
Nicolas Noble | 614c2bf | 2015-01-21 15:48:36 -0800 | [diff] [blame] | 513 | if (expires_in == NULL || expires_in->type != GRPC_JSON_NUMBER) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 514 | gpr_log(GPR_ERROR, "Missing or invalid expires_in in JSON."); |
| 515 | status = GRPC_CREDENTIALS_ERROR; |
| 516 | goto end; |
| 517 | } |
Nicolas Noble | fee065c | 2015-01-26 11:41:12 -0800 | [diff] [blame] | 518 | gpr_asprintf(&new_access_token, "%s %s", token_type->value, |
| 519 | access_token->value); |
Nicolas Noble | e04455a | 2015-01-26 17:01:29 -0800 | [diff] [blame] | 520 | token_lifetime->tv_sec = strtol(expires_in->value, NULL, 10); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 521 | token_lifetime->tv_nsec = 0; |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 522 | token_lifetime->clock_type = GPR_TIMESPAN; |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 523 | if (*token_md != NULL) grpc_credentials_md_store_unref(*token_md); |
| 524 | *token_md = grpc_credentials_md_store_create(1); |
| 525 | grpc_credentials_md_store_add_cstrings( |
| 526 | *token_md, GRPC_AUTHORIZATION_METADATA_KEY, new_access_token); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 527 | status = GRPC_CREDENTIALS_OK; |
| 528 | } |
| 529 | |
| 530 | end: |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 531 | if (status != GRPC_CREDENTIALS_OK && (*token_md != NULL)) { |
| 532 | grpc_credentials_md_store_unref(*token_md); |
| 533 | *token_md = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 534 | } |
| 535 | if (null_terminated_body != NULL) gpr_free(null_terminated_body); |
| 536 | if (new_access_token != NULL) gpr_free(new_access_token); |
Nicolas Noble | 8c2be9b | 2015-01-27 14:21:18 -0800 | [diff] [blame] | 537 | if (json != NULL) grpc_json_destroy(json); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 538 | return status; |
| 539 | } |
| 540 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 541 | static void on_oauth2_token_fetcher_http_response( |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 542 | void *user_data, const grpc_httpcli_response *response) { |
| 543 | grpc_credentials_metadata_request *r = |
| 544 | (grpc_credentials_metadata_request *)user_data; |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 545 | grpc_oauth2_token_fetcher_credentials *c = |
| 546 | (grpc_oauth2_token_fetcher_credentials *)r->creds; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 547 | gpr_timespec token_lifetime; |
| 548 | grpc_credentials_status status; |
| 549 | |
| 550 | gpr_mu_lock(&c->mu); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 551 | status = grpc_oauth2_token_fetcher_credentials_parse_server_response( |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 552 | response, &c->access_token_md, &token_lifetime); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 553 | if (status == GRPC_CREDENTIALS_OK) { |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 554 | c->token_expiration = |
| 555 | gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), token_lifetime); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 556 | r->cb(r->user_data, c->access_token_md->entries, |
| 557 | c->access_token_md->num_entries, status); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 558 | } else { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 559 | c->token_expiration = gpr_inf_past(GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 560 | r->cb(r->user_data, NULL, 0, status); |
| 561 | } |
| 562 | gpr_mu_unlock(&c->mu); |
| 563 | grpc_credentials_metadata_request_destroy(r); |
| 564 | } |
| 565 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 566 | static void oauth2_token_fetcher_get_request_metadata( |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 567 | grpc_credentials *creds, grpc_pollset *pollset, const char *service_url, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 568 | grpc_credentials_metadata_cb cb, void *user_data) { |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 569 | grpc_oauth2_token_fetcher_credentials *c = |
| 570 | (grpc_oauth2_token_fetcher_credentials *)creds; |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 571 | gpr_timespec refresh_threshold = gpr_time_from_seconds( |
| 572 | GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS, GPR_TIMESPAN); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 573 | grpc_credentials_md_store *cached_access_token_md = NULL; |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 574 | { |
| 575 | gpr_mu_lock(&c->mu); |
| 576 | if (c->access_token_md != NULL && |
Craig Tiller | f1bff01 | 2015-07-06 11:20:50 -0700 | [diff] [blame] | 577 | (gpr_time_cmp( |
| 578 | gpr_time_sub(c->token_expiration, gpr_now(GPR_CLOCK_REALTIME)), |
| 579 | refresh_threshold) > 0)) { |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 580 | cached_access_token_md = |
| 581 | grpc_credentials_md_store_ref(c->access_token_md); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 582 | } |
| 583 | gpr_mu_unlock(&c->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 584 | } |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 585 | if (cached_access_token_md != NULL) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 586 | cb(user_data, cached_access_token_md->entries, |
| 587 | cached_access_token_md->num_entries, GRPC_CREDENTIALS_OK); |
| 588 | grpc_credentials_md_store_unref(cached_access_token_md); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 589 | } else { |
| 590 | c->fetch_func( |
| 591 | grpc_credentials_metadata_request_create(creds, cb, user_data), |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 592 | &c->httpcli_context, pollset, on_oauth2_token_fetcher_http_response, |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 593 | gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), refresh_threshold)); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 594 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 595 | } |
| 596 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 597 | static void init_oauth2_token_fetcher(grpc_oauth2_token_fetcher_credentials *c, |
| 598 | grpc_fetch_oauth2_func fetch_func) { |
| 599 | memset(c, 0, sizeof(grpc_oauth2_token_fetcher_credentials)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 600 | c->base.type = GRPC_CREDENTIALS_TYPE_OAUTH2; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 601 | gpr_ref_init(&c->base.refcount, 1); |
| 602 | gpr_mu_init(&c->mu); |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 603 | c->token_expiration = gpr_inf_past(GPR_CLOCK_REALTIME); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 604 | c->fetch_func = fetch_func; |
Julien Boeuf | feca1bf | 2015-06-22 16:46:20 +0200 | [diff] [blame] | 605 | grpc_httpcli_context_init(&c->httpcli_context); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* -- ComputeEngine credentials. -- */ |
| 609 | |
| 610 | static grpc_credentials_vtable compute_engine_vtable = { |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 611 | oauth2_token_fetcher_destroy, oauth2_token_fetcher_has_request_metadata, |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 612 | oauth2_token_fetcher_has_request_metadata_only, |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 613 | oauth2_token_fetcher_get_request_metadata, NULL}; |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 614 | |
| 615 | static void compute_engine_fetch_oauth2( |
| 616 | grpc_credentials_metadata_request *metadata_req, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 617 | grpc_httpcli_context *httpcli_context, grpc_pollset *pollset, |
| 618 | grpc_httpcli_response_cb response_cb, gpr_timespec deadline) { |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 619 | grpc_httpcli_header header = {"Metadata-Flavor", "Google"}; |
| 620 | grpc_httpcli_request request; |
| 621 | memset(&request, 0, sizeof(grpc_httpcli_request)); |
| 622 | request.host = GRPC_COMPUTE_ENGINE_METADATA_HOST; |
| 623 | request.path = GRPC_COMPUTE_ENGINE_METADATA_TOKEN_PATH; |
| 624 | request.hdr_count = 1; |
| 625 | request.hdrs = &header; |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 626 | grpc_httpcli_get(httpcli_context, pollset, &request, deadline, response_cb, |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 627 | metadata_req); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | grpc_credentials *grpc_compute_engine_credentials_create(void) { |
| 631 | grpc_oauth2_token_fetcher_credentials *c = |
| 632 | gpr_malloc(sizeof(grpc_oauth2_token_fetcher_credentials)); |
| 633 | init_oauth2_token_fetcher(c, compute_engine_fetch_oauth2); |
| 634 | c->base.vtable = &compute_engine_vtable; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 635 | return &c->base; |
| 636 | } |
| 637 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 638 | /* -- ServiceAccount credentials. -- */ |
| 639 | |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 640 | static void service_account_destroy(grpc_credentials *creds) { |
| 641 | grpc_service_account_credentials *c = |
| 642 | (grpc_service_account_credentials *)creds; |
| 643 | if (c->scope != NULL) gpr_free(c->scope); |
| 644 | grpc_auth_json_key_destruct(&c->key); |
| 645 | oauth2_token_fetcher_destroy(&c->base.base); |
| 646 | } |
| 647 | |
| 648 | static grpc_credentials_vtable service_account_vtable = { |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 649 | service_account_destroy, oauth2_token_fetcher_has_request_metadata, |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 650 | oauth2_token_fetcher_has_request_metadata_only, |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 651 | oauth2_token_fetcher_get_request_metadata, NULL}; |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 652 | |
| 653 | static void service_account_fetch_oauth2( |
| 654 | grpc_credentials_metadata_request *metadata_req, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 655 | grpc_httpcli_context *httpcli_context, grpc_pollset *pollset, |
| 656 | grpc_httpcli_response_cb response_cb, gpr_timespec deadline) { |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 657 | grpc_service_account_credentials *c = |
| 658 | (grpc_service_account_credentials *)metadata_req->creds; |
| 659 | grpc_httpcli_header header = {"Content-Type", |
| 660 | "application/x-www-form-urlencoded"}; |
| 661 | grpc_httpcli_request request; |
| 662 | char *body = NULL; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 663 | char *jwt = grpc_jwt_encode_and_sign(&c->key, GRPC_JWT_OAUTH2_AUDIENCE, |
| 664 | c->token_lifetime, c->scope); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 665 | if (jwt == NULL) { |
| 666 | grpc_httpcli_response response; |
| 667 | memset(&response, 0, sizeof(grpc_httpcli_response)); |
| 668 | response.status = 400; /* Invalid request. */ |
| 669 | gpr_log(GPR_ERROR, "Could not create signed jwt."); |
| 670 | /* Do not even send the request, just call the response callback. */ |
| 671 | response_cb(metadata_req, &response); |
| 672 | return; |
| 673 | } |
Craig Tiller | be6a355 | 2015-01-23 13:04:45 -0800 | [diff] [blame] | 674 | gpr_asprintf(&body, "%s%s", GRPC_SERVICE_ACCOUNT_POST_BODY_PREFIX, jwt); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 675 | memset(&request, 0, sizeof(grpc_httpcli_request)); |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 676 | request.host = GRPC_GOOGLE_OAUTH2_SERVICE_HOST; |
| 677 | request.path = GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH; |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 678 | request.hdr_count = 1; |
| 679 | request.hdrs = &header; |
| 680 | request.use_ssl = 1; |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 681 | grpc_httpcli_post(httpcli_context, pollset, &request, body, strlen(body), |
| 682 | deadline, response_cb, metadata_req); |
jboeuf | 1a809c0 | 2014-12-19 15:44:30 -0800 | [diff] [blame] | 683 | gpr_free(body); |
| 684 | gpr_free(jwt); |
| 685 | } |
| 686 | |
| 687 | grpc_credentials *grpc_service_account_credentials_create( |
| 688 | const char *json_key, const char *scope, gpr_timespec token_lifetime) { |
| 689 | grpc_service_account_credentials *c; |
| 690 | grpc_auth_json_key key = grpc_auth_json_key_create_from_string(json_key); |
| 691 | |
| 692 | if (scope == NULL || (strlen(scope) == 0) || |
| 693 | !grpc_auth_json_key_is_valid(&key)) { |
| 694 | gpr_log(GPR_ERROR, |
| 695 | "Invalid input for service account credentials creation"); |
| 696 | return NULL; |
| 697 | } |
| 698 | c = gpr_malloc(sizeof(grpc_service_account_credentials)); |
| 699 | memset(c, 0, sizeof(grpc_service_account_credentials)); |
| 700 | init_oauth2_token_fetcher(&c->base, service_account_fetch_oauth2); |
| 701 | c->base.base.vtable = &service_account_vtable; |
| 702 | c->scope = gpr_strdup(scope); |
| 703 | c->key = key; |
| 704 | c->token_lifetime = token_lifetime; |
| 705 | return &c->base.base; |
| 706 | } |
| 707 | |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 708 | /* -- RefreshToken credentials. -- */ |
| 709 | |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 710 | static void refresh_token_destroy(grpc_credentials *creds) { |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 711 | grpc_refresh_token_credentials *c = (grpc_refresh_token_credentials *)creds; |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 712 | grpc_auth_refresh_token_destruct(&c->refresh_token); |
| 713 | oauth2_token_fetcher_destroy(&c->base.base); |
| 714 | } |
| 715 | |
| 716 | static grpc_credentials_vtable refresh_token_vtable = { |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 717 | refresh_token_destroy, oauth2_token_fetcher_has_request_metadata, |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 718 | oauth2_token_fetcher_has_request_metadata_only, |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 719 | oauth2_token_fetcher_get_request_metadata, NULL}; |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 720 | |
| 721 | static void refresh_token_fetch_oauth2( |
| 722 | grpc_credentials_metadata_request *metadata_req, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 723 | grpc_httpcli_context *httpcli_context, grpc_pollset *pollset, |
| 724 | grpc_httpcli_response_cb response_cb, gpr_timespec deadline) { |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 725 | grpc_refresh_token_credentials *c = |
| 726 | (grpc_refresh_token_credentials *)metadata_req->creds; |
| 727 | grpc_httpcli_header header = {"Content-Type", |
| 728 | "application/x-www-form-urlencoded"}; |
| 729 | grpc_httpcli_request request; |
| 730 | char *body = NULL; |
| 731 | gpr_asprintf(&body, GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING, |
| 732 | c->refresh_token.client_id, c->refresh_token.client_secret, |
| 733 | c->refresh_token.refresh_token); |
| 734 | memset(&request, 0, sizeof(grpc_httpcli_request)); |
| 735 | request.host = GRPC_GOOGLE_OAUTH2_SERVICE_HOST; |
| 736 | request.path = GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH; |
| 737 | request.hdr_count = 1; |
| 738 | request.hdrs = &header; |
| 739 | request.use_ssl = 1; |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 740 | grpc_httpcli_post(httpcli_context, pollset, &request, body, strlen(body), |
| 741 | deadline, response_cb, metadata_req); |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 742 | gpr_free(body); |
| 743 | } |
| 744 | |
Julien Boeuf | 5400606 | 2015-07-07 19:13:04 -0700 | [diff] [blame] | 745 | grpc_credentials *grpc_refresh_token_credentials_create_from_auth_refresh_token( |
| 746 | grpc_auth_refresh_token refresh_token) { |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 747 | grpc_refresh_token_credentials *c; |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 748 | if (!grpc_auth_refresh_token_is_valid(&refresh_token)) { |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 749 | gpr_log(GPR_ERROR, "Invalid input for refresh token credentials creation"); |
Julien Boeuf | 9835cf0 | 2015-03-09 16:56:44 -0700 | [diff] [blame] | 750 | return NULL; |
| 751 | } |
| 752 | c = gpr_malloc(sizeof(grpc_refresh_token_credentials)); |
| 753 | memset(c, 0, sizeof(grpc_refresh_token_credentials)); |
| 754 | init_oauth2_token_fetcher(&c->base, refresh_token_fetch_oauth2); |
| 755 | c->base.base.vtable = &refresh_token_vtable; |
| 756 | c->refresh_token = refresh_token; |
| 757 | return &c->base.base; |
| 758 | } |
| 759 | |
Julien Boeuf | 5400606 | 2015-07-07 19:13:04 -0700 | [diff] [blame] | 760 | grpc_credentials *grpc_refresh_token_credentials_create( |
| 761 | const char *json_refresh_token) { |
| 762 | return grpc_refresh_token_credentials_create_from_auth_refresh_token( |
| 763 | grpc_auth_refresh_token_create_from_string(json_refresh_token)); |
| 764 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 765 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 766 | /* -- Fake Oauth2 credentials. -- */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 767 | |
| 768 | static void fake_oauth2_destroy(grpc_credentials *creds) { |
| 769 | grpc_fake_oauth2_credentials *c = (grpc_fake_oauth2_credentials *)creds; |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 770 | grpc_credentials_md_store_unref(c->access_token_md); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 771 | gpr_free(c); |
| 772 | } |
| 773 | |
| 774 | static int fake_oauth2_has_request_metadata(const grpc_credentials *creds) { |
| 775 | return 1; |
| 776 | } |
| 777 | |
| 778 | static int fake_oauth2_has_request_metadata_only( |
| 779 | const grpc_credentials *creds) { |
| 780 | return 1; |
| 781 | } |
| 782 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 783 | void on_simulated_token_fetch_done(void *user_data, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 784 | grpc_credentials_metadata_request *r = |
| 785 | (grpc_credentials_metadata_request *)user_data; |
| 786 | grpc_fake_oauth2_credentials *c = (grpc_fake_oauth2_credentials *)r->creds; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 787 | GPR_ASSERT(success); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 788 | r->cb(r->user_data, c->access_token_md->entries, |
| 789 | c->access_token_md->num_entries, GRPC_CREDENTIALS_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 790 | grpc_credentials_metadata_request_destroy(r); |
| 791 | } |
| 792 | |
| 793 | static void fake_oauth2_get_request_metadata(grpc_credentials *creds, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 794 | grpc_pollset *pollset, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 795 | const char *service_url, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 796 | grpc_credentials_metadata_cb cb, |
| 797 | void *user_data) { |
| 798 | grpc_fake_oauth2_credentials *c = (grpc_fake_oauth2_credentials *)creds; |
| 799 | |
| 800 | if (c->is_async) { |
David Garcia Quintas | 69ba871 | 2015-06-01 11:29:42 -0700 | [diff] [blame] | 801 | grpc_credentials_metadata_request *cb_arg = |
David Garcia Quintas | 8b8cce0 | 2015-05-31 12:39:40 -0700 | [diff] [blame] | 802 | grpc_credentials_metadata_request_create(creds, cb, user_data); |
David Garcia Quintas | 69ba871 | 2015-06-01 11:29:42 -0700 | [diff] [blame] | 803 | grpc_iomgr_closure_init(cb_arg->on_simulated_token_fetch_done_closure, |
| 804 | on_simulated_token_fetch_done, cb_arg); |
| 805 | grpc_iomgr_add_callback(cb_arg->on_simulated_token_fetch_done_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 806 | } else { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 807 | cb(user_data, c->access_token_md->entries, 1, GRPC_CREDENTIALS_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
| 811 | static grpc_credentials_vtable fake_oauth2_vtable = { |
| 812 | fake_oauth2_destroy, fake_oauth2_has_request_metadata, |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 813 | fake_oauth2_has_request_metadata_only, fake_oauth2_get_request_metadata, |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 814 | NULL}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 815 | |
| 816 | grpc_credentials *grpc_fake_oauth2_credentials_create( |
| 817 | const char *token_md_value, int is_async) { |
| 818 | grpc_fake_oauth2_credentials *c = |
| 819 | gpr_malloc(sizeof(grpc_fake_oauth2_credentials)); |
| 820 | memset(c, 0, sizeof(grpc_fake_oauth2_credentials)); |
| 821 | c->base.type = GRPC_CREDENTIALS_TYPE_OAUTH2; |
| 822 | c->base.vtable = &fake_oauth2_vtable; |
| 823 | gpr_ref_init(&c->base.refcount, 1); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 824 | c->access_token_md = grpc_credentials_md_store_create(1); |
| 825 | grpc_credentials_md_store_add_cstrings( |
| 826 | c->access_token_md, GRPC_AUTHORIZATION_METADATA_KEY, token_md_value); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 827 | c->is_async = is_async; |
| 828 | return &c->base; |
| 829 | } |
| 830 | |
Julien Boeuf | 2805be1 | 2015-07-01 02:47:18 -0700 | [diff] [blame] | 831 | /* -- Oauth2 Access Token credentials. -- */ |
| 832 | |
Julien Boeuf | 2805be1 | 2015-07-01 02:47:18 -0700 | [diff] [blame] | 833 | static void access_token_destroy(grpc_credentials *creds) { |
| 834 | grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds; |
| 835 | grpc_credentials_md_store_unref(c->access_token_md); |
| 836 | gpr_free(c); |
| 837 | } |
| 838 | |
| 839 | static int access_token_has_request_metadata(const grpc_credentials *creds) { |
| 840 | return 1; |
| 841 | } |
| 842 | |
| 843 | static int access_token_has_request_metadata_only( |
| 844 | const grpc_credentials *creds) { |
| 845 | return 1; |
| 846 | } |
| 847 | |
| 848 | static void access_token_get_request_metadata(grpc_credentials *creds, |
| 849 | grpc_pollset *pollset, |
| 850 | const char *service_url, |
| 851 | grpc_credentials_metadata_cb cb, |
| 852 | void *user_data) { |
| 853 | grpc_access_token_credentials *c = (grpc_access_token_credentials *)creds; |
| 854 | cb(user_data, c->access_token_md->entries, 1, GRPC_CREDENTIALS_OK); |
| 855 | } |
| 856 | |
| 857 | static grpc_credentials_vtable access_token_vtable = { |
| 858 | access_token_destroy, access_token_has_request_metadata, |
| 859 | access_token_has_request_metadata_only, access_token_get_request_metadata, |
| 860 | NULL}; |
| 861 | |
| 862 | grpc_credentials *grpc_access_token_credentials_create( |
| 863 | const char *access_token) { |
| 864 | grpc_access_token_credentials *c = |
| 865 | gpr_malloc(sizeof(grpc_access_token_credentials)); |
| 866 | char *token_md_value; |
| 867 | memset(c, 0, sizeof(grpc_access_token_credentials)); |
| 868 | c->base.type = GRPC_CREDENTIALS_TYPE_OAUTH2; |
| 869 | c->base.vtable = &access_token_vtable; |
| 870 | gpr_ref_init(&c->base.refcount, 1); |
| 871 | c->access_token_md = grpc_credentials_md_store_create(1); |
| 872 | gpr_asprintf(&token_md_value, "Bearer %s", access_token); |
| 873 | grpc_credentials_md_store_add_cstrings( |
| 874 | c->access_token_md, GRPC_AUTHORIZATION_METADATA_KEY, token_md_value); |
| 875 | gpr_free(token_md_value); |
| 876 | return &c->base; |
| 877 | } |
| 878 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 879 | /* -- Fake transport security credentials. -- */ |
| 880 | |
| 881 | static void fake_transport_security_credentials_destroy( |
| 882 | grpc_credentials *creds) { |
| 883 | gpr_free(creds); |
| 884 | } |
| 885 | |
| 886 | static void fake_transport_security_server_credentials_destroy( |
| 887 | grpc_server_credentials *creds) { |
| 888 | gpr_free(creds); |
| 889 | } |
| 890 | |
| 891 | static int fake_transport_security_has_request_metadata( |
| 892 | const grpc_credentials *creds) { |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static int fake_transport_security_has_request_metadata_only( |
| 897 | const grpc_credentials *creds) { |
| 898 | return 0; |
| 899 | } |
| 900 | |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 901 | static grpc_security_status fake_transport_security_create_security_connector( |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 902 | grpc_credentials *c, const char *target, const grpc_channel_args *args, |
| 903 | grpc_credentials *request_metadata_creds, |
| 904 | grpc_channel_security_connector **sc, grpc_channel_args **new_args) { |
| 905 | *sc = grpc_fake_channel_security_connector_create(request_metadata_creds, 1); |
| 906 | return GRPC_SECURITY_OK; |
| 907 | } |
| 908 | |
| 909 | static grpc_security_status |
| 910 | fake_transport_security_server_create_security_connector( |
| 911 | grpc_server_credentials *c, grpc_security_connector **sc) { |
| 912 | *sc = grpc_fake_server_security_connector_create(); |
| 913 | return GRPC_SECURITY_OK; |
| 914 | } |
| 915 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 916 | static grpc_credentials_vtable fake_transport_security_credentials_vtable = { |
| 917 | fake_transport_security_credentials_destroy, |
| 918 | fake_transport_security_has_request_metadata, |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 919 | fake_transport_security_has_request_metadata_only, NULL, |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 920 | fake_transport_security_create_security_connector}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 921 | |
| 922 | static grpc_server_credentials_vtable |
| 923 | fake_transport_security_server_credentials_vtable = { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 924 | fake_transport_security_server_credentials_destroy, |
| 925 | fake_transport_security_server_create_security_connector}; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 926 | |
| 927 | grpc_credentials *grpc_fake_transport_security_credentials_create(void) { |
| 928 | grpc_credentials *c = gpr_malloc(sizeof(grpc_credentials)); |
| 929 | memset(c, 0, sizeof(grpc_credentials)); |
| 930 | c->type = GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY; |
| 931 | c->vtable = &fake_transport_security_credentials_vtable; |
| 932 | gpr_ref_init(&c->refcount, 1); |
| 933 | return c; |
| 934 | } |
| 935 | |
Craig Tiller | 3eef2c4 | 2015-01-15 11:37:54 -0800 | [diff] [blame] | 936 | grpc_server_credentials *grpc_fake_transport_security_server_credentials_create( |
| 937 | void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 938 | grpc_server_credentials *c = gpr_malloc(sizeof(grpc_server_credentials)); |
| 939 | memset(c, 0, sizeof(grpc_server_credentials)); |
| 940 | c->type = GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY; |
| 941 | c->vtable = &fake_transport_security_server_credentials_vtable; |
| 942 | return c; |
| 943 | } |
| 944 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 945 | /* -- Composite credentials. -- */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 946 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 947 | typedef struct { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 948 | grpc_composite_credentials *composite_creds; |
| 949 | size_t creds_index; |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 950 | grpc_credentials_md_store *md_elems; |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 951 | char *service_url; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 952 | void *user_data; |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 953 | grpc_pollset *pollset; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 954 | grpc_credentials_metadata_cb cb; |
| 955 | } grpc_composite_credentials_metadata_context; |
| 956 | |
| 957 | static void composite_destroy(grpc_credentials *creds) { |
| 958 | grpc_composite_credentials *c = (grpc_composite_credentials *)creds; |
| 959 | size_t i; |
| 960 | for (i = 0; i < c->inner.num_creds; i++) { |
| 961 | grpc_credentials_unref(c->inner.creds_array[i]); |
| 962 | } |
| 963 | gpr_free(c->inner.creds_array); |
| 964 | gpr_free(creds); |
| 965 | } |
| 966 | |
| 967 | static int composite_has_request_metadata(const grpc_credentials *creds) { |
| 968 | const grpc_composite_credentials *c = |
| 969 | (const grpc_composite_credentials *)creds; |
| 970 | size_t i; |
| 971 | for (i = 0; i < c->inner.num_creds; i++) { |
| 972 | if (grpc_credentials_has_request_metadata(c->inner.creds_array[i])) { |
| 973 | return 1; |
| 974 | } |
| 975 | } |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | static int composite_has_request_metadata_only(const grpc_credentials *creds) { |
| 980 | const grpc_composite_credentials *c = |
| 981 | (const grpc_composite_credentials *)creds; |
| 982 | size_t i; |
| 983 | for (i = 0; i < c->inner.num_creds; i++) { |
| 984 | if (!grpc_credentials_has_request_metadata_only(c->inner.creds_array[i])) { |
| 985 | return 0; |
| 986 | } |
| 987 | } |
| 988 | return 1; |
| 989 | } |
| 990 | |
| 991 | static void composite_md_context_destroy( |
| 992 | grpc_composite_credentials_metadata_context *ctx) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 993 | grpc_credentials_md_store_unref(ctx->md_elems); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 994 | if (ctx->service_url != NULL) gpr_free(ctx->service_url); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 995 | gpr_free(ctx); |
| 996 | } |
| 997 | |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 998 | static void composite_metadata_cb(void *user_data, |
| 999 | grpc_credentials_md *md_elems, size_t num_md, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1000 | grpc_credentials_status status) { |
| 1001 | grpc_composite_credentials_metadata_context *ctx = |
| 1002 | (grpc_composite_credentials_metadata_context *)user_data; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1003 | if (status != GRPC_CREDENTIALS_OK) { |
| 1004 | ctx->cb(ctx->user_data, NULL, 0, status); |
| 1005 | return; |
| 1006 | } |
| 1007 | |
| 1008 | /* Copy the metadata in the context. */ |
| 1009 | if (num_md > 0) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1010 | size_t i; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1011 | for (i = 0; i < num_md; i++) { |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1012 | grpc_credentials_md_store_add(ctx->md_elems, md_elems[i].key, |
| 1013 | md_elems[i].value); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1014 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /* See if we need to get some more metadata. */ |
| 1018 | while (ctx->creds_index < ctx->composite_creds->inner.num_creds) { |
| 1019 | grpc_credentials *inner_creds = |
| 1020 | ctx->composite_creds->inner.creds_array[ctx->creds_index++]; |
| 1021 | if (grpc_credentials_has_request_metadata(inner_creds)) { |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 1022 | grpc_credentials_get_request_metadata(inner_creds, ctx->pollset, |
| 1023 | ctx->service_url, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 1024 | composite_metadata_cb, ctx); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1025 | return; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /* We're done!. */ |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1030 | ctx->cb(ctx->user_data, ctx->md_elems->entries, ctx->md_elems->num_entries, |
| 1031 | GRPC_CREDENTIALS_OK); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1032 | composite_md_context_destroy(ctx); |
| 1033 | } |
| 1034 | |
| 1035 | static void composite_get_request_metadata(grpc_credentials *creds, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 1036 | grpc_pollset *pollset, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 1037 | const char *service_url, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1038 | grpc_credentials_metadata_cb cb, |
| 1039 | void *user_data) { |
| 1040 | grpc_composite_credentials *c = (grpc_composite_credentials *)creds; |
| 1041 | grpc_composite_credentials_metadata_context *ctx; |
| 1042 | if (!grpc_credentials_has_request_metadata(creds)) { |
| 1043 | cb(user_data, NULL, 0, GRPC_CREDENTIALS_OK); |
| 1044 | return; |
| 1045 | } |
| 1046 | ctx = gpr_malloc(sizeof(grpc_composite_credentials_metadata_context)); |
| 1047 | memset(ctx, 0, sizeof(grpc_composite_credentials_metadata_context)); |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 1048 | ctx->service_url = gpr_strdup(service_url); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1049 | ctx->user_data = user_data; |
| 1050 | ctx->cb = cb; |
| 1051 | ctx->composite_creds = c; |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 1052 | ctx->pollset = pollset; |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1053 | ctx->md_elems = grpc_credentials_md_store_create(c->inner.num_creds); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1054 | while (ctx->creds_index < c->inner.num_creds) { |
| 1055 | grpc_credentials *inner_creds = c->inner.creds_array[ctx->creds_index++]; |
| 1056 | if (grpc_credentials_has_request_metadata(inner_creds)) { |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 1057 | grpc_credentials_get_request_metadata(inner_creds, pollset, service_url, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 1058 | composite_metadata_cb, ctx); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1059 | return; |
| 1060 | } |
| 1061 | } |
| 1062 | GPR_ASSERT(0); /* Should have exited before. */ |
| 1063 | } |
| 1064 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1065 | static grpc_security_status composite_create_security_connector( |
| 1066 | grpc_credentials *creds, const char *target, const grpc_channel_args *args, |
| 1067 | grpc_credentials *request_metadata_creds, |
| 1068 | grpc_channel_security_connector **sc, grpc_channel_args **new_args) { |
| 1069 | grpc_composite_credentials *c = (grpc_composite_credentials *)creds; |
| 1070 | if (c->connector_creds == NULL) { |
| 1071 | gpr_log(GPR_ERROR, |
| 1072 | "Cannot create security connector, missing connector credentials."); |
| 1073 | return GRPC_SECURITY_ERROR; |
| 1074 | } |
| 1075 | return grpc_credentials_create_security_connector(c->connector_creds, target, |
| 1076 | args, creds, sc, new_args); |
| 1077 | } |
| 1078 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1079 | static grpc_credentials_vtable composite_credentials_vtable = { |
| 1080 | composite_destroy, composite_has_request_metadata, |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1081 | composite_has_request_metadata_only, composite_get_request_metadata, |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 1082 | composite_create_security_connector}; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1083 | |
| 1084 | static grpc_credentials_array get_creds_array(grpc_credentials **creds_addr) { |
| 1085 | grpc_credentials_array result; |
| 1086 | grpc_credentials *creds = *creds_addr; |
| 1087 | result.creds_array = creds_addr; |
| 1088 | result.num_creds = 1; |
Ronnie Sahlberg | 2ad8d21 | 2015-03-07 08:39:22 -0800 | [diff] [blame] | 1089 | if (strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1090 | result = *grpc_composite_credentials_get_credentials(creds); |
| 1091 | } |
| 1092 | return result; |
| 1093 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1094 | |
| 1095 | grpc_credentials *grpc_composite_credentials_create(grpc_credentials *creds1, |
| 1096 | grpc_credentials *creds2) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1097 | size_t i; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1098 | size_t creds_array_byte_size; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1099 | grpc_credentials_array creds1_array; |
| 1100 | grpc_credentials_array creds2_array; |
| 1101 | grpc_composite_credentials *c; |
| 1102 | GPR_ASSERT(creds1 != NULL); |
| 1103 | GPR_ASSERT(creds2 != NULL); |
| 1104 | c = gpr_malloc(sizeof(grpc_composite_credentials)); |
| 1105 | memset(c, 0, sizeof(grpc_composite_credentials)); |
| 1106 | c->base.type = GRPC_CREDENTIALS_TYPE_COMPOSITE; |
| 1107 | c->base.vtable = &composite_credentials_vtable; |
| 1108 | gpr_ref_init(&c->base.refcount, 1); |
| 1109 | creds1_array = get_creds_array(&creds1); |
| 1110 | creds2_array = get_creds_array(&creds2); |
| 1111 | c->inner.num_creds = creds1_array.num_creds + creds2_array.num_creds; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1112 | creds_array_byte_size = c->inner.num_creds * sizeof(grpc_credentials *); |
| 1113 | c->inner.creds_array = gpr_malloc(creds_array_byte_size); |
| 1114 | memset(c->inner.creds_array, 0, creds_array_byte_size); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1115 | for (i = 0; i < creds1_array.num_creds; i++) { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1116 | grpc_credentials *cur_creds = creds1_array.creds_array[i]; |
| 1117 | if (!grpc_credentials_has_request_metadata_only(cur_creds)) { |
| 1118 | if (c->connector_creds == NULL) { |
| 1119 | c->connector_creds = cur_creds; |
| 1120 | } else { |
| 1121 | gpr_log(GPR_ERROR, "Cannot compose multiple connector credentials."); |
| 1122 | goto fail; |
| 1123 | } |
| 1124 | } |
| 1125 | c->inner.creds_array[i] = grpc_credentials_ref(cur_creds); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1126 | } |
| 1127 | for (i = 0; i < creds2_array.num_creds; i++) { |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1128 | grpc_credentials *cur_creds = creds2_array.creds_array[i]; |
| 1129 | if (!grpc_credentials_has_request_metadata_only(cur_creds)) { |
| 1130 | if (c->connector_creds == NULL) { |
| 1131 | c->connector_creds = cur_creds; |
| 1132 | } else { |
| 1133 | gpr_log(GPR_ERROR, "Cannot compose multiple connector credentials."); |
| 1134 | goto fail; |
| 1135 | } |
| 1136 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1137 | c->inner.creds_array[i + creds1_array.num_creds] = |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1138 | grpc_credentials_ref(cur_creds); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1139 | } |
| 1140 | return &c->base; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 1141 | |
| 1142 | fail: |
| 1143 | grpc_credentials_unref(&c->base); |
| 1144 | return NULL; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | const grpc_credentials_array *grpc_composite_credentials_get_credentials( |
| 1148 | grpc_credentials *creds) { |
| 1149 | const grpc_composite_credentials *c = |
| 1150 | (const grpc_composite_credentials *)creds; |
Ronnie Sahlberg | 2ad8d21 | 2015-03-07 08:39:22 -0800 | [diff] [blame] | 1151 | GPR_ASSERT(strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1152 | return &c->inner; |
| 1153 | } |
| 1154 | |
jboeuf | 6ad120e | 2015-01-12 17:08:15 -0800 | [diff] [blame] | 1155 | grpc_credentials *grpc_credentials_contains_type( |
| 1156 | grpc_credentials *creds, const char *type, |
| 1157 | grpc_credentials **composite_creds) { |
| 1158 | size_t i; |
Ronnie Sahlberg | 2ad8d21 | 2015-03-07 08:39:22 -0800 | [diff] [blame] | 1159 | if (strcmp(creds->type, type) == 0) { |
jboeuf | 6ad120e | 2015-01-12 17:08:15 -0800 | [diff] [blame] | 1160 | if (composite_creds != NULL) *composite_creds = NULL; |
| 1161 | return creds; |
Ronnie Sahlberg | 2ad8d21 | 2015-03-07 08:39:22 -0800 | [diff] [blame] | 1162 | } else if (strcmp(creds->type, GRPC_CREDENTIALS_TYPE_COMPOSITE) == 0) { |
jboeuf | 6ad120e | 2015-01-12 17:08:15 -0800 | [diff] [blame] | 1163 | const grpc_credentials_array *inner_creds_array = |
| 1164 | grpc_composite_credentials_get_credentials(creds); |
| 1165 | for (i = 0; i < inner_creds_array->num_creds; i++) { |
Ronnie Sahlberg | 2ad8d21 | 2015-03-07 08:39:22 -0800 | [diff] [blame] | 1166 | if (strcmp(type, inner_creds_array->creds_array[i]->type) == 0) { |
jboeuf | 6ad120e | 2015-01-12 17:08:15 -0800 | [diff] [blame] | 1167 | if (composite_creds != NULL) *composite_creds = creds; |
| 1168 | return inner_creds_array->creds_array[i]; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | return NULL; |
| 1173 | } |
| 1174 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1175 | /* -- IAM credentials. -- */ |
| 1176 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1177 | static void iam_destroy(grpc_credentials *creds) { |
| 1178 | grpc_iam_credentials *c = (grpc_iam_credentials *)creds; |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1179 | grpc_credentials_md_store_unref(c->iam_md); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1180 | gpr_free(c); |
| 1181 | } |
| 1182 | |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 1183 | static int iam_has_request_metadata(const grpc_credentials *creds) { return 1; } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1184 | |
| 1185 | static int iam_has_request_metadata_only(const grpc_credentials *creds) { |
| 1186 | return 1; |
| 1187 | } |
| 1188 | |
| 1189 | static void iam_get_request_metadata(grpc_credentials *creds, |
Craig Tiller | 06bac34 | 2015-06-01 12:55:57 -0700 | [diff] [blame] | 1190 | grpc_pollset *pollset, |
Julien Boeuf | f47a5cb | 2015-02-18 12:24:08 -0800 | [diff] [blame] | 1191 | const char *service_url, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1192 | grpc_credentials_metadata_cb cb, |
| 1193 | void *user_data) { |
| 1194 | grpc_iam_credentials *c = (grpc_iam_credentials *)creds; |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1195 | cb(user_data, c->iam_md->entries, c->iam_md->num_entries, |
| 1196 | GRPC_CREDENTIALS_OK); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | static grpc_credentials_vtable iam_vtable = { |
| 1200 | iam_destroy, iam_has_request_metadata, iam_has_request_metadata_only, |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1201 | iam_get_request_metadata, NULL}; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1202 | |
| 1203 | grpc_credentials *grpc_iam_credentials_create(const char *token, |
| 1204 | const char *authority_selector) { |
| 1205 | grpc_iam_credentials *c; |
| 1206 | GPR_ASSERT(token != NULL); |
| 1207 | GPR_ASSERT(authority_selector != NULL); |
| 1208 | c = gpr_malloc(sizeof(grpc_iam_credentials)); |
| 1209 | memset(c, 0, sizeof(grpc_iam_credentials)); |
| 1210 | c->base.type = GRPC_CREDENTIALS_TYPE_IAM; |
| 1211 | c->base.vtable = &iam_vtable; |
| 1212 | gpr_ref_init(&c->base.refcount, 1); |
Julien Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 1213 | c->iam_md = grpc_credentials_md_store_create(2); |
| 1214 | grpc_credentials_md_store_add_cstrings( |
| 1215 | c->iam_md, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, token); |
| 1216 | grpc_credentials_md_store_add_cstrings( |
| 1217 | c->iam_md, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, authority_selector); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1218 | return &c->base; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1219 | } |