| #ifndef THIRD_PARTY_MILOTIC_INTERNAL_CC_PROXY_FAKE_AUTH_CONTEXT_H_ |
| #define THIRD_PARTY_MILOTIC_INTERNAL_CC_PROXY_FAKE_AUTH_CONTEXT_H_ |
| |
| #include <string> |
| #include <vector> |
| |
| #include "absl/container/flat_hash_map.h" |
| #include "absl/log/log.h" |
| #include "grpcpp/security/auth_context.h" |
| #include "grpcpp/support/string_ref.h" |
| namespace milotic { |
| |
| class FakeAuthPropertyIterator : public grpc::AuthPropertyIterator { |
| public: |
| FakeAuthPropertyIterator() { LOG(FATAL) << "Iterator not supported"; } |
| }; |
| |
| class FakeAuthContext : public grpc::AuthContext { |
| public: |
| explicit FakeAuthContext(bool is_peer_authenticated) |
| : is_peer_authenticated_(is_peer_authenticated) {} |
| |
| /// Returns true if the peer is authenticated. |
| bool IsPeerAuthenticated() const override { return is_peer_authenticated_; } |
| |
| std::vector<grpc::string_ref> GetPeerIdentity() const override { |
| return FindPropertyValues(peer_identity_property_name_); |
| } |
| |
| std::string GetPeerIdentityPropertyName() const override { |
| return peer_identity_property_name_; |
| } |
| |
| std::vector<grpc::string_ref> FindPropertyValues( |
| const std::string& name) const override { |
| auto found = properties_.find(name); |
| if (found == properties_.end()) { |
| return {}; |
| } |
| return {found->second.begin(), found->second.end()}; |
| } |
| |
| grpc::AuthPropertyIterator begin() const override { |
| return FakeAuthPropertyIterator(); |
| } |
| |
| grpc::AuthPropertyIterator end() const override { |
| return FakeAuthPropertyIterator(); |
| } |
| |
| void AddProperty(const std::string& key, |
| const grpc::string_ref& value) override { |
| properties_[key].push_back(std::string(value.data(), value.size())); |
| } |
| |
| bool SetPeerIdentityPropertyName(const std::string& name) override { |
| peer_identity_property_name_ = name; |
| return true; |
| } |
| |
| private: |
| bool is_peer_authenticated_; |
| absl::flat_hash_map<std::string, std::vector<std::string>> properties_; |
| std::string peer_identity_property_name_; |
| }; |
| } // namespace milotic |
| |
| #endif // THIRD_PARTY_MILOTIC_INTERNAL_CC_PROXY_FAKE_AUTH_CONTEXT_H_ |