auth_context.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef GRPCPP_SECURITY_AUTH_CONTEXT_H
  19. #define GRPCPP_SECURITY_AUTH_CONTEXT_H
  20. #include <iterator>
  21. #include <vector>
  22. #include <grpcpp/support/config.h>
  23. #include <grpcpp/support/string_ref.h>
  24. struct grpc_auth_context;
  25. struct grpc_auth_property;
  26. struct grpc_auth_property_iterator;
  27. namespace grpc {
  28. class SecureAuthContext;
  29. typedef std::pair<string_ref, string_ref> AuthProperty;
  30. class AuthPropertyIterator {
  31. public:
  32. using iterator_category = std::forward_iterator_tag;
  33. using value_type = const AuthProperty;
  34. using pointer = void;
  35. using reference = void;
  36. using difference_type = std::ptrdiff_t;
  37. ~AuthPropertyIterator();
  38. AuthPropertyIterator& operator++();
  39. AuthPropertyIterator operator++(int);
  40. bool operator==(const AuthPropertyIterator& rhs) const;
  41. bool operator!=(const AuthPropertyIterator& rhs) const;
  42. AuthProperty operator*();
  43. protected:
  44. AuthPropertyIterator();
  45. AuthPropertyIterator(const grpc_auth_property* property,
  46. const grpc_auth_property_iterator* iter);
  47. private:
  48. friend class SecureAuthContext;
  49. const grpc_auth_property* property_;
  50. // The following items form a grpc_auth_property_iterator.
  51. const grpc_auth_context* ctx_;
  52. size_t index_;
  53. const char* name_;
  54. };
  55. /// Class encapsulating the Authentication Information.
  56. ///
  57. /// It includes the secure identity of the peer, the type of secure transport
  58. /// used as well as any other properties required by the authorization layer.
  59. class AuthContext {
  60. public:
  61. virtual ~AuthContext() {}
  62. /// Returns true if the peer is authenticated.
  63. virtual bool IsPeerAuthenticated() const = 0;
  64. /// A peer identity.
  65. ///
  66. /// It is, in general, comprised of one or more properties (in which case they
  67. /// have the same name).
  68. virtual std::vector<grpc::string_ref> GetPeerIdentity() const = 0;
  69. virtual TString GetPeerIdentityPropertyName() const = 0;
  70. /// Returns all the property values with the given name.
  71. virtual std::vector<grpc::string_ref> FindPropertyValues(
  72. const TString& name) const = 0;
  73. /// Iteration over all the properties.
  74. virtual AuthPropertyIterator begin() const = 0;
  75. virtual AuthPropertyIterator end() const = 0;
  76. /// Mutation functions: should only be used by an AuthMetadataProcessor.
  77. virtual void AddProperty(const TString& key, const string_ref& value) = 0;
  78. virtual bool SetPeerIdentityPropertyName(const TString& name) = 0;
  79. };
  80. } // namespace grpc
  81. #endif // GRPCPP_SECURITY_AUTH_CONTEXT_H