3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H
20 #define GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H
22 #include <grpc/support/port_platform.h>
24 #include "src/core/lib/gprpp/arena.h"
25 #include "src/core/lib/gprpp/ref_counted.h"
26 #include "src/core/lib/gprpp/ref_counted_ptr.h"
27 #include "src/core/lib/iomgr/pollset.h"
28 #include "src/core/lib/security/credentials/credentials.h"
30 extern grpc_core::DebugOnlyTraceFlag grpc_trace_auth_context_refcount;
32 /* --- grpc_auth_context ---
34 High level authentication context object. Can optionally be chained. */
36 /* Property names are always NULL terminated. */
38 struct grpc_auth_property_array {
39 grpc_auth_property* array = nullptr;
44 void grpc_auth_property_reset(grpc_auth_property* property);
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
49 struct grpc_auth_context
50 : public grpc_core::RefCounted<grpc_auth_context,
51 grpc_core::NonPolymorphicRefCount> {
53 explicit grpc_auth_context(
54 grpc_core::RefCountedPtr<grpc_auth_context> chained)
55 : grpc_core::RefCounted<grpc_auth_context,
56 grpc_core::NonPolymorphicRefCount>(
57 &grpc_trace_auth_context_refcount),
58 chained_(std::move(chained)) {
59 if (chained_ != nullptr) {
60 peer_identity_property_name_ = chained_->peer_identity_property_name_;
64 ~grpc_auth_context() {
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]);
70 gpr_free(properties_.array);
74 const grpc_auth_context* chained() const { return chained_.get(); }
75 const grpc_auth_property_array& properties() const { return properties_; }
77 bool is_authenticated() const {
78 return peer_identity_property_name_ != nullptr;
80 const char* peer_identity_property_name() const {
81 return peer_identity_property_name_;
83 void set_peer_identity_property_name(const char* name) {
84 peer_identity_property_name_ = name;
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);
92 grpc_core::RefCountedPtr<grpc_auth_context> chained_;
93 grpc_auth_property_array properties_;
94 const char* peer_identity_property_name_ = nullptr;
97 /* --- grpc_security_context_extension ---
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. */
102 struct grpc_security_context_extension {
103 void* instance = nullptr;
104 void (*destroy)(void*) = nullptr;
107 /* --- grpc_client_security_context ---
109 Internal client-side security context. */
111 struct grpc_client_security_context {
112 explicit grpc_client_security_context(
113 grpc_core::RefCountedPtr<grpc_call_credentials> creds)
114 : creds(std::move(creds)) {}
115 ~grpc_client_security_context();
117 grpc_core::RefCountedPtr<grpc_call_credentials> creds;
118 grpc_core::RefCountedPtr<grpc_auth_context> auth_context;
119 grpc_security_context_extension extension;
122 grpc_client_security_context* grpc_client_security_context_create(
123 grpc_core::Arena* arena, grpc_call_credentials* creds);
124 void grpc_client_security_context_destroy(void* ctx);
126 /* --- grpc_server_security_context ---
128 Internal server-side security context. */
130 struct grpc_server_security_context {
131 grpc_server_security_context() = default;
132 ~grpc_server_security_context();
134 grpc_core::RefCountedPtr<grpc_auth_context> auth_context;
135 grpc_security_context_extension extension;
138 grpc_server_security_context* grpc_server_security_context_create(
139 grpc_core::Arena* arena);
140 void grpc_server_security_context_destroy(void* ctx);
142 /* --- Channel args for auth context --- */
143 #define GRPC_AUTH_CONTEXT_ARG "grpc.auth_context"
145 grpc_arg grpc_auth_context_to_arg(grpc_auth_context* c);
146 grpc_auth_context* grpc_auth_context_from_arg(const grpc_arg* arg);
147 grpc_auth_context* grpc_find_auth_context_in_args(
148 const grpc_channel_args* args);
150 #endif /* GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H */