| 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 |
|
| 20 |
#include <grpc/support/port_platform.h> |
| 21 |
|
| 22 |
#include <map> |
| 23 |
#include <memory> |
| 24 |
#include <string> |
| 25 |
#include <type_traits> |
| 26 |
|
| 27 |
#include "absl/strings/string_view.h" |
| 28 |
|
| 29 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 30 |
#include "src/core/lib/json/json.h" |
| 31 |
|
| 32 |
struct grpc_channel_credentials; |
| 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 <> |
| 47 |
class ChannelCredsFactory<grpc_channel_credentials> { |
| 48 |
public: |
| 49 |
virtual ~ChannelCredsFactory() {} |
| 50 |
virtual absl::string_view creds_type() const = 0; |
| 51 |
virtual bool IsValidConfig(const Json& config) const = 0; |
| 52 |
virtual RefCountedPtr<grpc_channel_credentials> CreateChannelCreds( |
| 53 |
const Json& config) const = 0; |
| 54 |
}; |
| 55 |
|
| 56 |
template <typename T = grpc_channel_credentials> |
| 57 |
class ChannelCredsRegistry { |
| 58 |
public: |
| 59 |
static_assert(std::is_base_of<grpc_channel_credentials, T>::value, |
| 60 |
"ChannelCredsRegistry must be instantiated with " |
| 61 |
"grpc_channel_credentials."); |
| 62 |
class Builder { |
| 63 |
public: |
| 64 |
void RegisterChannelCredsFactory( |
| 65 |
std::unique_ptr<ChannelCredsFactory<T>> factory) { |
| 66 |
factories_[factory->creds_type()] = std::move(factory); |
| 67 |
} |
| 68 |
ChannelCredsRegistry Build() { |
| 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>>> |
| 76 |
factories_; |
| 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 |
|
| 88 |
RefCountedPtr<T> CreateChannelCreds(const std::string& creds_type, |
| 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>>> |
| 98 |
factories_; |
| 99 |
}; |
| 100 |
|
| 101 |
} // namespace grpc_core |
| 102 |
|
| 103 |
#endif // GRPC_CORE_LIB_SECURITY_CREDENTIALS_CHANNEL_CREDS_REGISTRY_H |