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 <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/string_util.h>
26 #include <grpc/support/sync.h>
27
28 #include "absl/strings/str_format.h"
29 #include "src/core/lib/gprpp/ref_counted_ptr.h"
30 #include "src/core/lib/surface/api_trace.h"
31
~grpc_google_iam_credentials()32 grpc_google_iam_credentials::~grpc_google_iam_credentials() {
33 grpc_credentials_mdelem_array_destroy(&md_array_);
34 }
35
get_request_metadata(grpc_polling_entity *,grpc_auth_metadata_context,grpc_credentials_mdelem_array * md_array,grpc_closure *,grpc_error **)36 bool grpc_google_iam_credentials::get_request_metadata(
37 grpc_polling_entity* /*pollent*/, grpc_auth_metadata_context /*context*/,
38 grpc_credentials_mdelem_array* md_array,
39 grpc_closure* /*on_request_metadata*/, grpc_error** /*error*/) {
40 grpc_credentials_mdelem_array_append(md_array, &md_array_);
41 return true;
42 }
43
cancel_get_request_metadata(grpc_credentials_mdelem_array *,grpc_error * error)44 void grpc_google_iam_credentials::cancel_get_request_metadata(
45 grpc_credentials_mdelem_array* /*md_array*/, grpc_error* error) {
46 GRPC_ERROR_UNREF(error);
47 }
48
grpc_google_iam_credentials(const char * token,const char * authority_selector)49 grpc_google_iam_credentials::grpc_google_iam_credentials(
50 const char* token, const char* authority_selector)
51 : grpc_call_credentials(GRPC_CALL_CREDENTIALS_TYPE_IAM),
52 debug_string_(absl::StrFormat(
53 "GoogleIAMCredentials{Token:%s,AuthoritySelector:%s}",
54 token != nullptr ? "present" : "absent", authority_selector)) {
55 grpc_mdelem md = grpc_mdelem_from_slices(
56 grpc_slice_from_static_string(GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY),
57 grpc_slice_from_copied_string(token));
58 grpc_credentials_mdelem_array_add(&md_array_, md);
59 GRPC_MDELEM_UNREF(md);
60 md = grpc_mdelem_from_slices(
61 grpc_slice_from_static_string(GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY),
62 grpc_slice_from_copied_string(authority_selector));
63 grpc_credentials_mdelem_array_add(&md_array_, md);
64 GRPC_MDELEM_UNREF(md);
65 }
66
grpc_google_iam_credentials_create(const char * token,const char * authority_selector,void * reserved)67 grpc_call_credentials* grpc_google_iam_credentials_create(
68 const char* token, const char* authority_selector, void* reserved) {
69 grpc_core::ExecCtx exec_ctx;
70 GRPC_API_TRACE(
71 "grpc_iam_credentials_create(token=%s, authority_selector=%s, "
72 "reserved=%p)",
73 3, (token, authority_selector, reserved));
74 GPR_ASSERT(reserved == nullptr);
75 GPR_ASSERT(token != nullptr);
76 GPR_ASSERT(authority_selector != nullptr);
77 return grpc_core::MakeRefCounted<grpc_google_iam_credentials>(
78 token, authority_selector)
79 .release();
80 }
81