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