Debian Codegraph @

src/core/lib/security/credentials/iam/iam_credentials.cc

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