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