Debian Codegraph @

src/core/lib/security/credentials/composite/composite_credentials.h

raw
1 /*
2 *
3 * Copyright 2015 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 #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_COMPOSITE_COMPOSITE_CREDENTIALS_H
20 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_COMPOSITE_COMPOSITE_CREDENTIALS_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <algorithm>
25 #include <string>
26 #include <utility>
27 #include <vector>
28
29 #include "absl/status/statusor.h"
30
31 #include <grpc/grpc.h>
32 #include <grpc/grpc_security.h>
33 #include <grpc/grpc_security_constants.h>
34
35 #include "src/core/lib/channel/channel_args.h"
36 #include "src/core/lib/gpr/useful.h"
37 #include "src/core/lib/gprpp/ref_counted_ptr.h"
38 #include "src/core/lib/gprpp/unique_type_name.h"
39 #include "src/core/lib/promise/arena_promise.h"
40 #include "src/core/lib/security/credentials/credentials.h"
41 #include "src/core/lib/security/security_connector/security_connector.h"
42 #include "src/core/lib/transport/transport.h"
43
44 /* -- Composite channel credentials. -- */
45
46 class grpc_composite_channel_credentials : public grpc_channel_credentials {
47 public:
48 grpc_composite_channel_credentials(
49 grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,
50 grpc_core::RefCountedPtr<grpc_call_credentials> call_creds)
51 : inner_creds_(std::move(channel_creds)),
52 call_creds_(std::move(call_creds)) {}
53
54 ~grpc_composite_channel_credentials() override = default;
55
56 grpc_core::RefCountedPtr<grpc_channel_credentials>
57 duplicate_without_call_credentials() override {
58 return inner_creds_;
59 }
60
61 grpc_core::RefCountedPtr<grpc_channel_security_connector>
62 create_security_connector(
63 grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
64 const char* target, grpc_core::ChannelArgs* args) override;
65
66 grpc_core::ChannelArgs update_arguments(
67 grpc_core::ChannelArgs args) override {
68 return inner_creds_->update_arguments(std::move(args));
69 }
70
71 grpc_core::UniqueTypeName type() const override;
72
73 const grpc_channel_credentials* inner_creds() const {
74 return inner_creds_.get();
75 }
76 const grpc_call_credentials* call_creds() const { return call_creds_.get(); }
77 grpc_call_credentials* mutable_call_creds() { return call_creds_.get(); }
78
79 private:
80 int cmp_impl(const grpc_channel_credentials* other) const override {
81 auto* o = static_cast<const grpc_composite_channel_credentials*>(other);
82 int r = inner_creds_->cmp(o->inner_creds_.get());
83 if (r != 0) return r;
84 return call_creds_->cmp(o->call_creds_.get());
85 }
86
87 grpc_core::RefCountedPtr<grpc_channel_credentials> inner_creds_;
88 grpc_core::RefCountedPtr<grpc_call_credentials> call_creds_;
89 };
90
91 /* -- Composite call credentials. -- */
92
93 class grpc_composite_call_credentials : public grpc_call_credentials {
94 public:
95 using CallCredentialsList =
96 std::vector<grpc_core::RefCountedPtr<grpc_call_credentials>>;
97
98 grpc_composite_call_credentials(
99 grpc_core::RefCountedPtr<grpc_call_credentials> creds1,
100 grpc_core::RefCountedPtr<grpc_call_credentials> creds2);
101 ~grpc_composite_call_credentials() override = default;
102
103 grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>>
104 GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata,
105 const GetRequestMetadataArgs* args) override;
106
107 grpc_security_level min_security_level() const override {
108 return min_security_level_;
109 }
110
111 const CallCredentialsList& inner() const { return inner_; }
112 std::string debug_string() override;
113
114 static grpc_core::UniqueTypeName Type();
115
116 grpc_core::UniqueTypeName type() const override { return Type(); }
117
118 private:
119 int cmp_impl(const grpc_call_credentials* other) const override {
120 // TODO(yashykt): Check if we can do something better here
121 return grpc_core::QsortCompare(
122 static_cast<const grpc_call_credentials*>(this), other);
123 }
124
125 void push_to_inner(grpc_core::RefCountedPtr<grpc_call_credentials> creds,
126 bool is_composite);
127 grpc_security_level min_security_level_;
128 CallCredentialsList inner_;
129 };
130
131 #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_COMPOSITE_COMPOSITE_CREDENTIALS_H \
132 */