3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/security/credentials/iam/iam_credentials.h"
25 #include "src/core/lib/gprpp/ref_counted_ptr.h"
26 #include "src/core/lib/surface/api_trace.h"
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/string_util.h>
31 #include <grpc/support/sync.h>
33 grpc_google_iam_credentials::~grpc_google_iam_credentials() {
34 grpc_credentials_mdelem_array_destroy(&md_array_);
37 bool grpc_google_iam_credentials::get_request_metadata(
38 grpc_polling_entity* pollent, grpc_auth_metadata_context context,
39 grpc_credentials_mdelem_array* md_array, grpc_closure* on_request_metadata,
41 grpc_credentials_mdelem_array_append(md_array, &md_array_);
45 void grpc_google_iam_credentials::cancel_get_request_metadata(
46 grpc_credentials_mdelem_array* md_array, grpc_error* error) {
47 GRPC_ERROR_UNREF(error);
50 grpc_google_iam_credentials::grpc_google_iam_credentials(
51 const char* token, const char* authority_selector)
52 : grpc_call_credentials(GRPC_CALL_CREDENTIALS_TYPE_IAM) {
53 grpc_mdelem md = grpc_mdelem_from_slices(
54 grpc_slice_from_static_string(GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY),
55 grpc_slice_from_copied_string(token));
56 grpc_credentials_mdelem_array_add(&md_array_, md);
57 GRPC_MDELEM_UNREF(md);
58 md = grpc_mdelem_from_slices(
59 grpc_slice_from_static_string(GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY),
60 grpc_slice_from_copied_string(authority_selector));
61 grpc_credentials_mdelem_array_add(&md_array_, md);
62 GRPC_MDELEM_UNREF(md);
65 grpc_call_credentials* grpc_google_iam_credentials_create(
66 const char* token, const char* authority_selector, void* reserved) {
67 grpc_core::ExecCtx exec_ctx;
69 "grpc_iam_credentials_create(token=%s, authority_selector=%s, "
71 3, (token, authority_selector, reserved));
72 GPR_ASSERT(reserved == nullptr);
73 GPR_ASSERT(token != nullptr);
74 GPR_ASSERT(authority_selector != nullptr);
75 return grpc_core::MakeRefCounted<grpc_google_iam_credentials>(
76 token, authority_selector)