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 <stdlib.h>
24 
25 #include <initializer_list>
26 #include <memory>
27 #include <utility>
28 
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/string_view.h"
31 
32 #include <grpc/support/log.h>
33 
34 #include "src/core/lib/debug/trace.h"
35 #include "src/core/lib/gprpp/ref_counted_ptr.h"
36 #include "src/core/lib/iomgr/exec_ctx.h"
37 #include "src/core/lib/promise/promise.h"
38 #include "src/core/lib/surface/api_trace.h"
39 #include "src/core/lib/transport/metadata_batch.h"
40 
41 grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>>
GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata,const grpc_call_credentials::GetRequestMetadataArgs *)42 grpc_google_iam_credentials::GetRequestMetadata(
43     grpc_core::ClientMetadataHandle initial_metadata,
44     const grpc_call_credentials::GetRequestMetadataArgs*) {
45   if (token_.has_value()) {
46     initial_metadata->Append(
47         GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY, token_->Ref(),
48         [](absl::string_view, const grpc_core::Slice&) { abort(); });
49   }
50   initial_metadata->Append(
51       GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY, authority_selector_.Ref(),
52       [](absl::string_view, const grpc_core::Slice&) { abort(); });
53   return grpc_core::Immediate(std::move(initial_metadata));
54 }
55 
grpc_google_iam_credentials(const char * token,const char * authority_selector)56 grpc_google_iam_credentials::grpc_google_iam_credentials(
57     const char* token, const char* authority_selector)
58     : token_(token == nullptr ? absl::optional<grpc_core::Slice>()
59                               : grpc_core::Slice::FromCopiedString(token)),
60       authority_selector_(
61           grpc_core::Slice::FromCopiedString(authority_selector)),
62       debug_string_(absl::StrFormat(
63           "GoogleIAMCredentials{Token:%s,AuthoritySelector:%s}",
64           token != nullptr ? "present" : "absent", authority_selector)) {}
65 
Type()66 grpc_core::UniqueTypeName grpc_google_iam_credentials::Type() {
67   static grpc_core::UniqueTypeName::Factory kFactory("Iam");
68   return kFactory.Create();
69 }
70 
grpc_google_iam_credentials_create(const char * token,const char * authority_selector,void * reserved)71 grpc_call_credentials* grpc_google_iam_credentials_create(
72     const char* token, const char* authority_selector, void* reserved) {
73   grpc_core::ExecCtx exec_ctx;
74   GRPC_API_TRACE(
75       "grpc_iam_credentials_create(token=%s, authority_selector=%s, "
76       "reserved=%p)",
77       3, (token, authority_selector, reserved));
78   GPR_ASSERT(reserved == nullptr);
79   GPR_ASSERT(token != nullptr);
80   GPR_ASSERT(authority_selector != nullptr);
81   return grpc_core::MakeRefCounted<grpc_google_iam_credentials>(
82              token, authority_selector)
83       .release();
84 }
85