1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/security/credentials/iam/iam_credentials.h"
22
23 #include <string.h>
24
25 #include "src/core/lib/surface/api_trace.h"
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/string_util.h>
30 #include <grpc/support/sync.h>
31
iam_destruct(grpc_call_credentials * creds)32 static void iam_destruct(grpc_call_credentials* creds) {
33 grpc_google_iam_credentials* c =
34 reinterpret_cast<grpc_google_iam_credentials*>(creds);
35 grpc_credentials_mdelem_array_destroy(&c->md_array);
36 }
37
iam_get_request_metadata(grpc_call_credentials * creds,grpc_polling_entity * pollent,grpc_auth_metadata_context context,grpc_credentials_mdelem_array * md_array,grpc_closure * on_request_metadata,grpc_error ** error)38 static bool iam_get_request_metadata(grpc_call_credentials* creds,
39 grpc_polling_entity* pollent,
40 grpc_auth_metadata_context context,
41 grpc_credentials_mdelem_array* md_array,
42 grpc_closure* on_request_metadata,
43 grpc_error** error) {
44 grpc_google_iam_credentials* c =
45 reinterpret_cast<grpc_google_iam_credentials*>(creds);
46 grpc_credentials_mdelem_array_append(md_array, &c->md_array);
47 return true;
48 }
49
iam_cancel_get_request_metadata(grpc_call_credentials * c,grpc_credentials_mdelem_array * md_array,grpc_error * error)50 static void iam_cancel_get_request_metadata(
51 grpc_call_credentials* c, grpc_credentials_mdelem_array* md_array,
52 grpc_error* error) {
53 GRPC_ERROR_UNREF(error);
54 }
55
56 static grpc_call_credentials_vtable iam_vtable = {
57 iam_destruct, iam_get_request_metadata, iam_cancel_get_request_metadata};
58
grpc_google_iam_credentials_create(const char * token,const char * authority_selector,void * reserved)59 grpc_call_credentials* grpc_google_iam_credentials_create(
60 const char* token, const char* authority_selector, void* reserved) {
61 grpc_core::ExecCtx exec_ctx;
62 GRPC_API_TRACE(
63 "grpc_iam_credentials_create(token=%s, authority_selector=%s, "
64 "reserved=%p)",
65 3, (token, authority_selector, reserved));
66 GPR_ASSERT(reserved == nullptr);
67 GPR_ASSERT(token != nullptr);
68 GPR_ASSERT(authority_selector != nullptr);
69 grpc_google_iam_credentials* c =
70 static_cast<grpc_google_iam_credentials*>(gpr_zalloc(sizeof(*c)));
71 c->base.type = GRPC_CALL_CREDENTIALS_TYPE_IAM;
72 c->base.vtable = &iam_vtable;
73 gpr_ref_init(&c->base.refcount, 1);
74 grpc_mdelem md = grpc_mdelem_from_slices(
75 grpc_slice_from_static_string(GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY),
76 grpc_slice_from_copied_string(token));
77 grpc_credentials_mdelem_array_add(&c->md_array, md);
78 GRPC_MDELEM_UNREF(md);
79 md = grpc_mdelem_from_slices(
80 grpc_slice_from_static_string(GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY),
81 grpc_slice_from_copied_string(authority_selector));
82 grpc_credentials_mdelem_array_add(&c->md_array, md);
83 GRPC_MDELEM_UNREF(md);
84
85 return &c->base;
86 }
87