secure_server_credentials.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef GRPC_INTERNAL_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H
  19. #define GRPC_INTERNAL_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H
  20. #include <memory>
  21. #include <grpcpp/security/server_credentials.h>
  22. #include <grpcpp/security/tls_credentials_options.h>
  23. #include <grpc/grpc_security.h>
  24. #include "src/cpp/server/thread_pool_interface.h"
  25. namespace grpc {
  26. class SecureServerCredentials;
  27. class AuthMetadataProcessorAyncWrapper final {
  28. public:
  29. static void Destroy(void* wrapper);
  30. static void Process(void* wrapper, grpc_auth_context* context,
  31. const grpc_metadata* md, size_t num_md,
  32. grpc_process_auth_metadata_done_cb cb, void* user_data);
  33. AuthMetadataProcessorAyncWrapper(
  34. const std::shared_ptr<AuthMetadataProcessor>& processor)
  35. : processor_(processor) {
  36. if (processor && processor->IsBlocking()) {
  37. thread_pool_.reset(CreateDefaultThreadPool());
  38. }
  39. }
  40. private:
  41. void InvokeProcessor(grpc_auth_context* context, const grpc_metadata* md,
  42. size_t num_md, grpc_process_auth_metadata_done_cb cb,
  43. void* user_data);
  44. std::unique_ptr<ThreadPoolInterface> thread_pool_;
  45. std::shared_ptr<AuthMetadataProcessor> processor_;
  46. };
  47. class SecureServerCredentials final : public ServerCredentials {
  48. public:
  49. explicit SecureServerCredentials(grpc_server_credentials* creds)
  50. : creds_(creds) {}
  51. ~SecureServerCredentials() override {
  52. grpc_server_credentials_release(creds_);
  53. }
  54. int AddPortToServer(const std::string& addr, grpc_server* server) override;
  55. void SetAuthMetadataProcessor(
  56. const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) override;
  57. private:
  58. grpc_server_credentials* creds_;
  59. std::unique_ptr<grpc::AuthMetadataProcessorAyncWrapper> processor_;
  60. };
  61. } // namespace grpc
  62. #endif // GRPC_INTERNAL_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H