xref: /aosp_15_r20/external/grpc-grpc/src/cpp/server/secure_server_credentials.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_SRC_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H
20 #define GRPC_SRC_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H
21 
22 #include <memory>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/grpc_security.h>
26 #include <grpcpp/security/auth_metadata_processor.h>
27 #include <grpcpp/security/server_credentials.h>
28 
29 #include "src/cpp/server/thread_pool_interface.h"
30 
31 namespace grpc {
32 class AuthMetadataProcessorAsyncWrapper final {
33  public:
34   static void Destroy(void* wrapper);
35 
36   static void Process(void* wrapper, grpc_auth_context* context,
37                       const grpc_metadata* md, size_t num_md,
38                       grpc_process_auth_metadata_done_cb cb, void* user_data);
39 
AuthMetadataProcessorAsyncWrapper(const std::shared_ptr<AuthMetadataProcessor> & processor)40   explicit AuthMetadataProcessorAsyncWrapper(
41       const std::shared_ptr<AuthMetadataProcessor>& processor)
42       : processor_(processor) {
43     if (processor && processor->IsBlocking()) {
44       thread_pool_.reset(CreateDefaultThreadPool());
45     }
46   }
47 
48  private:
49   void InvokeProcessor(grpc_auth_context* context, const grpc_metadata* md,
50                        size_t num_md, grpc_process_auth_metadata_done_cb cb,
51                        void* user_data);
52   std::unique_ptr<ThreadPoolInterface> thread_pool_;
53   std::shared_ptr<AuthMetadataProcessor> processor_;
54 };
55 
56 // TODO(hork): Remove this class once we either (a) allow AuthMetadataProcessor
57 // to be used with any creds type as requested in #21589 or (b) find a way to
58 // remove AuthMetadataProcessor in favor of some new server-side interception
59 // API.
60 class SecureServerCredentials final : public ServerCredentials {
61  public:
62   explicit SecureServerCredentials(grpc_server_credentials* creds);
63 
64   void SetAuthMetadataProcessor(
65       const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) override;
66 
67  private:
68   std::unique_ptr<grpc::AuthMetadataProcessorAsyncWrapper> processor_;
69 };
70 
71 }  // namespace grpc
72 
73 #endif  // GRPC_SRC_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H
74