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