secure_server_credentials.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_impl {
  26. class SecureServerCredentials;
  27. } // namespace grpc_impl
  28. namespace grpc {
  29. typedef ::grpc_impl::SecureServerCredentials SecureServerCredentials;
  30. class AuthMetadataProcessorAyncWrapper final {
  31. public:
  32. static void Destroy(void* wrapper);
  33. static void Process(void* wrapper, grpc_auth_context* context,
  34. const grpc_metadata* md, size_t num_md,
  35. grpc_process_auth_metadata_done_cb cb, void* user_data);
  36. AuthMetadataProcessorAyncWrapper(
  37. const std::shared_ptr<AuthMetadataProcessor>& processor)
  38. : processor_(processor) {
  39. if (processor && processor->IsBlocking()) {
  40. thread_pool_.reset(CreateDefaultThreadPool());
  41. }
  42. }
  43. private:
  44. void InvokeProcessor(grpc_auth_context* context, const grpc_metadata* md,
  45. size_t num_md, grpc_process_auth_metadata_done_cb cb,
  46. void* user_data);
  47. std::unique_ptr<ThreadPoolInterface> thread_pool_;
  48. std::shared_ptr<AuthMetadataProcessor> processor_;
  49. };
  50. } // namespace grpc
  51. namespace grpc_impl {
  52. class SecureServerCredentials final : public ServerCredentials {
  53. public:
  54. explicit SecureServerCredentials(grpc_server_credentials* creds)
  55. : creds_(creds) {}
  56. ~SecureServerCredentials() override {
  57. grpc_server_credentials_release(creds_);
  58. }
  59. int AddPortToServer(const grpc::string& addr, grpc_server* server) override;
  60. void SetAuthMetadataProcessor(
  61. const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) override;
  62. private:
  63. grpc_server_credentials* creds_;
  64. std::unique_ptr<grpc::AuthMetadataProcessorAyncWrapper> processor_;
  65. };
  66. } // namespace grpc_impl
  67. #endif // GRPC_INTERNAL_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H