GRPC Core  9.0.0
security_context.h
Go to the documentation of this file.
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 GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H
20 #define GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H
21 
23 
29 
31 
32 /* --- grpc_auth_context ---
33 
34  High level authentication context object. Can optionally be chained. */
35 
36 /* Property names are always NULL terminated. */
37 
40  size_t count = 0;
41  size_t capacity = 0;
42 };
43 
45 
46 // This type is forward declared as a C struct and we cannot define it as a
47 // class. Otherwise, compiler will complain about type mismatch due to
48 // -Wmismatched-tags.
50  : public grpc_core::RefCounted<grpc_auth_context,
51  grpc_core::NonPolymorphicRefCount> {
52  public:
56  grpc_core::NonPolymorphicRefCount>(
58  chained_(std::move(chained)) {
59  if (chained_ != nullptr) {
60  peer_identity_property_name_ = chained_->peer_identity_property_name_;
61  }
62  }
63 
65  chained_.reset(DEBUG_LOCATION, "chained");
66  if (properties_.array != nullptr) {
67  for (size_t i = 0; i < properties_.count; i++) {
68  grpc_auth_property_reset(&properties_.array[i]);
69  }
70  gpr_free(properties_.array);
71  }
72  }
73 
74  const grpc_auth_context* chained() const { return chained_.get(); }
75  const grpc_auth_property_array& properties() const { return properties_; }
76 
77  bool is_authenticated() const {
78  return peer_identity_property_name_ != nullptr;
79  }
80  const char* peer_identity_property_name() const {
81  return peer_identity_property_name_;
82  }
83  void set_peer_identity_property_name(const char* name) {
84  peer_identity_property_name_ = name;
85  }
86 
87  void ensure_capacity();
88  void add_property(const char* name, const char* value, size_t value_length);
89  void add_cstring_property(const char* name, const char* value);
90 
91  private:
93  grpc_auth_property_array properties_;
94  const char* peer_identity_property_name_ = nullptr;
95 };
96 
97 /* --- grpc_security_context_extension ---
98 
99  Extension to the security context that may be set in a filter and accessed
100  later by a higher level method on a grpc_call object. */
101 
103  void* instance = nullptr;
104  void (*destroy)(void*) = nullptr;
105 };
106 
107 /* --- grpc_client_security_context ---
108 
109  Internal client-side security context. */
110 
114  : creds(std::move(creds)) {}
116 
120 };
121 
123  grpc_core::Arena* arena, grpc_call_credentials* creds);
125 
126 /* --- grpc_server_security_context ---
127 
128  Internal server-side security context. */
129 
131  grpc_server_security_context() = default;
133 
136 };
137 
139  grpc_core::Arena* arena);
141 
142 /* --- Channel args for auth context --- */
143 #define GRPC_AUTH_CONTEXT_ARG "grpc.auth_context"
144 
148  const grpc_channel_args* args);
149 
150 #endif /* GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H */
Definition: security_context.h:49
grpc_auth_context(grpc_core::RefCountedPtr< grpc_auth_context > chained)
Definition: security_context.h:53
void grpc_server_security_context_destroy(void *ctx)
Definition: security_context.cc:130
Definition: arena.h:44
void grpc_auth_property_reset(grpc_auth_property *property)
Definition: security_context.cc:271
void ensure_capacity()
Definition: security_context.cc:222
GPRAPI void gpr_free(void *ptr)
free
Definition: alloc.cc:50
size_t count
Definition: security_context.h:40
An array of arguments that can be passed around.
Definition: grpc_types.h:132
bool is_authenticated() const
Definition: security_context.h:77
grpc_arg grpc_auth_context_to_arg(grpc_auth_context *c)
Definition: security_context.cc:297
Definition: security_context.h:38
size_t capacity
Definition: security_context.h:41
grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount
const grpc_auth_property_array & properties() const
Definition: security_context.h:75
A single argument...
Definition: grpc_types.h:103
Definition: credentials.h:225
~grpc_client_security_context()
Definition: security_context.cc:97
grpc_server_security_context * grpc_server_security_context_create(grpc_core::Arena *arena)
Definition: security_context.cc:125
~grpc_server_security_context()
Definition: security_context.cc:118
void grpc_client_security_context_destroy(void *ctx)
Definition: security_context.cc:110
grpc_auth_context * grpc_auth_context_from_arg(const grpc_arg *arg)
Definition: security_context.cc:302
#define DEBUG_LOCATION
Definition: debug_location.h:41
Round Robin Policy.
Definition: backend_metric.cc:24
Definition: trace.h:61
grpc_auth_context * grpc_find_auth_context_in_args(const grpc_channel_args *args)
Definition: security_context.cc:312
const char * peer_identity_property_name() const
Definition: security_context.h:80
void reset(T *value=nullptr)
Definition: ref_counted_ptr.h:109
void set_peer_identity_property_name(const char *name)
Definition: security_context.h:83
grpc_auth_property * array
Definition: security_context.h:39
Definition: security_context.h:130
grpc_client_security_context(grpc_core::RefCountedPtr< grpc_call_credentials > creds)
Definition: security_context.h:112
~grpc_auth_context()
Definition: security_context.h:64
value, if not NULL, is guaranteed to be NULL terminated.
Definition: grpc_security.h:43
void * instance
Definition: security_context.h:103
grpc_core::RefCountedPtr< grpc_auth_context > auth_context
Definition: security_context.h:134
T * get() const
Definition: ref_counted_ptr.h:144
Definition: ref_counted.h:248
grpc_client_security_context * grpc_client_security_context_create(grpc_core::Arena *arena, grpc_call_credentials *creds)
Definition: security_context.cc:104
void add_cstring_property(const char *name, const char *value)
Definition: security_context.cc:253
grpc_security_context_extension extension
Definition: security_context.h:119
const grpc_auth_context * chained() const
Definition: security_context.h:74
Definition: security_context.h:111
Definition: security_context.h:102
void add_property(const char *name, const char *value, size_t value_length)
Definition: security_context.cc:231
grpc_core::RefCountedPtr< grpc_auth_context > auth_context
Definition: security_context.h:118
grpc_security_context_extension extension
Definition: security_context.h:135
grpc_core::RefCountedPtr< grpc_call_credentials > creds
Definition: security_context.h:117
void(* destroy)(void *)
Definition: security_context.h:104