diff --git a/include/grpc++/server_context.h b/include/grpc++/server_context.h index a4ee986df17c854e87748626a560757201bb510d..2123d03291c67df1b76157624ecf8f3e45bd4c5f 100644 --- a/include/grpc++/server_context.h +++ b/include/grpc++/server_context.h @@ -99,9 +99,7 @@ class ServerContext { return client_metadata_; } - std::shared_ptr<const AuthContext> auth_context() const { - return auth_context_; - } + std::shared_ptr<const AuthContext> auth_context() const; private: friend class ::grpc::Server; @@ -147,7 +145,7 @@ class ServerContext { grpc_call* call_; CompletionQueue* cq_; bool sent_initial_metadata_; - std::shared_ptr<const AuthContext> auth_context_; + mutable std::shared_ptr<const AuthContext> auth_context_; std::multimap<grpc::string, grpc::string> client_metadata_; std::multimap<grpc::string, grpc::string> initial_metadata_; std::multimap<grpc::string, grpc::string> trailing_metadata_; diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index 1bb3a8bcc4cdfd97901dbc7ea92a4b774cb90566..0be77138d1660b0692a61bff2195a22210e94cd1 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -153,4 +153,11 @@ void ServerContext::set_call(grpc_call* call) { auth_context_ = CreateAuthContext(call); } +std::shared_ptr<const AuthContext> ServerContext::auth_context() const { + if (auth_context_.get() == nullptr) { + auth_context_ = CreateAuthContext(call_); + } + return auth_context_; +} + } // namespace grpc