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