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 <stddef.h>
23 
24 #include <memory>
25 #include <string>
26 
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_security.h>
29 #include <grpcpp/security/auth_metadata_processor.h>
30 #include <grpcpp/security/server_credentials.h>
31 
32 #include "src/cpp/server/thread_pool_interface.h"
33 
34 namespace grpc {
35 
36 class SecureServerCredentials;
37 
38 class AuthMetadataProcessorAyncWrapper final {
39  public:
40   static void Destroy(void* wrapper);
41 
42   static void Process(void* wrapper, grpc_auth_context* context,
43                       const grpc_metadata* md, size_t num_md,
44                       grpc_process_auth_metadata_done_cb cb, void* user_data);
45 
AuthMetadataProcessorAyncWrapper(const std::shared_ptr<AuthMetadataProcessor> & processor)46   explicit AuthMetadataProcessorAyncWrapper(
47       const std::shared_ptr<AuthMetadataProcessor>& processor)
48       : processor_(processor) {
49     if (processor && processor->IsBlocking()) {
50       thread_pool_.reset(CreateDefaultThreadPool());
51     }
52   }
53 
54  private:
55   void InvokeProcessor(grpc_auth_context* context, const grpc_metadata* md,
56                        size_t num_md, grpc_process_auth_metadata_done_cb cb,
57                        void* user_data);
58   std::unique_ptr<ThreadPoolInterface> thread_pool_;
59   std::shared_ptr<AuthMetadataProcessor> processor_;
60 };
61 
62 class SecureServerCredentials final : public ServerCredentials {
63  public:
SecureServerCredentials(grpc_server_credentials * creds)64   explicit SecureServerCredentials(grpc_server_credentials* creds)
65       : creds_(creds) {}
~SecureServerCredentials()66   ~SecureServerCredentials() override {
67     grpc_server_credentials_release(creds_);
68   }
69 
70   int AddPortToServer(const std::string& addr, grpc_server* server) override;
71 
72   void SetAuthMetadataProcessor(
73       const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) override;
74 
c_creds()75   grpc_server_credentials* c_creds() { return creds_; }
76 
77  private:
AsSecureServerCredentials()78   SecureServerCredentials* AsSecureServerCredentials() override { return this; }
79 
80   grpc_server_credentials* creds_;
81   std::unique_ptr<grpc::AuthMetadataProcessorAyncWrapper> processor_;
82 };
83 
84 }  // namespace grpc
85 
86 #endif  // GRPC_SRC_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H
87