channel_creds_registry.h
Go to the documentation of this file.
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_CHANNEL_CREDS_REGISTRY_H
18 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_CHANNEL_CREDS_REGISTRY_H
19 
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <type_traits>
26 
27 #include "absl/strings/string_view.h"
28 
30 #include "src/core/lib/json/json.h"
31 
33 
34 namespace grpc_core {
35 
36 template <typename T = grpc_channel_credentials>
37 class ChannelCredsFactory final {
38  public:
39  virtual ~ChannelCredsFactory() {}
40  virtual absl::string_view creds_type() const = delete;
41  virtual bool IsValidConfig(const Json& config) const = delete;
42  virtual RefCountedPtr<T> CreateChannelCreds(const Json& config) const =
43  delete;
44 };
45 
46 template <>
48  public:
49  virtual ~ChannelCredsFactory() {}
50  virtual absl::string_view creds_type() const = 0;
51  virtual bool IsValidConfig(const Json& config) const = 0;
53  const Json& config) const = 0;
54 };
55 
56 template <typename T = grpc_channel_credentials>
58  public:
60  "ChannelCredsRegistry must be instantiated with "
61  "grpc_channel_credentials.");
62  class Builder {
63  public:
65  std::unique_ptr<ChannelCredsFactory<T>> factory) {
66  factories_[factory->creds_type()] = std::move(factory);
67  }
69  ChannelCredsRegistry<T> registry;
70  registry.factories_.swap(factories_);
71  return registry;
72  }
73 
74  private:
75  std::map<absl::string_view, std::unique_ptr<ChannelCredsFactory<T>>>
77  };
78 
79  bool IsSupported(const std::string& creds_type) const {
80  return factories_.find(creds_type) != factories_.end();
81  }
82 
83  bool IsValidConfig(const std::string& creds_type, const Json& config) const {
84  const auto iter = factories_.find(creds_type);
85  return iter != factories_.cend() && iter->second->IsValidConfig(config);
86  }
87 
89  const Json& config) const {
90  const auto iter = factories_.find(creds_type);
91  if (iter == factories_.cend()) return nullptr;
92  return iter->second->CreateChannelCreds(config);
93  }
94 
95  private:
96  ChannelCredsRegistry() = default;
97  std::map<absl::string_view, std::unique_ptr<ChannelCredsFactory<T>>>
99 };
100 
101 } // namespace grpc_core
102 
103 #endif // GRPC_CORE_LIB_SECURITY_CREDENTIALS_CHANNEL_CREDS_REGISTRY_H
grpc_core::ChannelCredsRegistry::CreateChannelCreds
RefCountedPtr< T > CreateChannelCreds(const std::string &creds_type, const Json &config) const
Definition: channel_creds_registry.h:88
grpc_core::ChannelCredsRegistry::ChannelCredsRegistry
ChannelCredsRegistry()=default
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::ChannelCredsRegistry::factories_
std::map< absl::string_view, std::unique_ptr< ChannelCredsFactory< T > > > factories_
Definition: channel_creds_registry.h:98
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::ChannelCredsFactory::~ChannelCredsFactory
virtual ~ChannelCredsFactory()
Definition: channel_creds_registry.h:39
grpc_core::ChannelCredsRegistry::Builder::Build
ChannelCredsRegistry Build()
Definition: channel_creds_registry.h:68
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
grpc_core::ChannelCredsRegistry::IsSupported
bool IsSupported(const std::string &creds_type) const
Definition: channel_creds_registry.h:79
grpc_core::ChannelCredsFactory::creds_type
virtual absl::string_view creds_type() const =delete
grpc_core::ChannelCredsFactory::IsValidConfig
virtual bool IsValidConfig(const Json &config) const =delete
grpc_core::ChannelCredsFactory
Definition: channel_creds_registry.h:37
json.h
grpc_core::ChannelCredsRegistry::Builder::RegisterChannelCredsFactory
void RegisterChannelCredsFactory(std::unique_ptr< ChannelCredsFactory< T >> factory)
Definition: channel_creds_registry.h:64
grpc_core::ChannelCredsRegistry::IsValidConfig
bool IsValidConfig(const std::string &creds_type, const Json &config) const
Definition: channel_creds_registry.h:83
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::ChannelCredsFactory::CreateChannelCreds
virtual RefCountedPtr< T > CreateChannelCreds(const Json &config) const =delete
grpc_core::ChannelCredsRegistry::Builder::factories_
std::map< absl::string_view, std::unique_ptr< ChannelCredsFactory< T > > > factories_
Definition: channel_creds_registry.h:76
grpc_core::ChannelCredsFactory< grpc_channel_credentials >::~ChannelCredsFactory
virtual ~ChannelCredsFactory()
Definition: channel_creds_registry.h:49
grpc_core::ChannelCredsRegistry
Definition: channel_creds_registry.h:57
ref_counted_ptr.h
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
iter
Definition: test_winkernel.cpp:47
grpc_core::ChannelCredsRegistry::Builder
Definition: channel_creds_registry.h:62
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52