Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/lib/security/credentials/iam/iam_credentials.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "src/core/lib/surface/api_trace.h" |
| 39 | |
| 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/string_util.h> |
| 43 | #include <grpc/support/sync.h> |
| 44 | |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 45 | static void iam_destruct(grpc_exec_ctx *exec_ctx, |
| 46 | grpc_call_credentials *creds) { |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 47 | grpc_google_iam_credentials *c = (grpc_google_iam_credentials *)creds; |
Craig Tiller | bd1795c | 2016-10-31 15:30:00 -0700 | [diff] [blame] | 48 | grpc_credentials_md_store_unref(exec_ctx, c->iam_md); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static void iam_get_request_metadata(grpc_exec_ctx *exec_ctx, |
| 52 | grpc_call_credentials *creds, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 53 | grpc_polling_entity *pollent, |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 54 | grpc_auth_metadata_context context, |
| 55 | grpc_credentials_metadata_cb cb, |
| 56 | void *user_data) { |
| 57 | grpc_google_iam_credentials *c = (grpc_google_iam_credentials *)creds; |
| 58 | cb(exec_ctx, user_data, c->iam_md->entries, c->iam_md->num_entries, |
Julien Boeuf | bfc7ed6 | 2016-06-04 18:03:42 -0700 | [diff] [blame] | 59 | GRPC_CREDENTIALS_OK, NULL); |
Julien Boeuf | 8ca294e | 2016-05-02 14:56:30 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static grpc_call_credentials_vtable iam_vtable = {iam_destruct, |
| 63 | iam_get_request_metadata}; |
| 64 | |
| 65 | grpc_call_credentials *grpc_google_iam_credentials_create( |
| 66 | const char *token, const char *authority_selector, void *reserved) { |
| 67 | grpc_google_iam_credentials *c; |
| 68 | GRPC_API_TRACE( |
| 69 | "grpc_iam_credentials_create(token=%s, authority_selector=%s, " |
| 70 | "reserved=%p)", |
| 71 | 3, (token, authority_selector, reserved)); |
| 72 | GPR_ASSERT(reserved == NULL); |
| 73 | GPR_ASSERT(token != NULL); |
| 74 | GPR_ASSERT(authority_selector != NULL); |
| 75 | c = gpr_malloc(sizeof(grpc_google_iam_credentials)); |
| 76 | memset(c, 0, sizeof(grpc_google_iam_credentials)); |
| 77 | c->base.type = GRPC_CALL_CREDENTIALS_TYPE_IAM; |
| 78 | c->base.vtable = &iam_vtable; |
| 79 | gpr_ref_init(&c->base.refcount, 1); |
| 80 | c->iam_md = grpc_credentials_md_store_create(2); |
| 81 | grpc_credentials_md_store_add_cstrings( |
| 82 | c->iam_md, GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, token); |
| 83 | grpc_credentials_md_store_add_cstrings( |
| 84 | c->iam_md, GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, authority_selector); |
| 85 | return &c->base; |
| 86 | } |